如何在Yii2中的foreach循环中注册JS文件?

时间:2018-02-16 09:29:54

标签: javascript jquery yii2

我正在尝试在身份证内显示QR码。现在我试图一次打印10张身份证,所以我把ID卡设计放在for-each循环中,但我不能在for-each循环中注册JavaScript代码。仅显示一个QR码。我的意思是JavaScript只在循环中注册一次。

我在for-each循环中注册了JavaScript,.js文件的名称是qr.js。我的身份证设计代码是:

<div id="printableContainer">

        <div class="aaa" style="width: 100%;font-size: 12px;padding: 5px 0px;">
            <?php
            foreach ($students as $stuKey => $student) {
                $patron = \app\models\Patrons::findOne(['patron_id' => $student->patron_id]);
                if ($patron) {
                    if ($patron->type == 1) {
                        $model = \app\models\std\Student::find()
                                ->joinWith([
                                    "fkStudent",

                                ])
                                ->where(["student.id" => $patron->patron_id])
                                ->one();

                        if ($model !== null) {
                            $patrons_details = $model;
                        }
    //                    $patrons_details = null;
                    }
                } else {
                    return "No Patron found by this name";
                }
                ?>


                <?php if ($i % 6 != 0) {
                    ?>        
                    <div id='data' sid="<?= $patrons_details['student']->id; ?>" >
                    </div>
                    <?php
                    $this->registerJs($this->render("qr.js"), 5);
                    ?>

                    <div class="  w3-margin-top" style="float: left;margin-left:5px;margin-right:5px;margin-top:5px;margin-bottom:25px;">
                        <div class="id-card-holder" style="border: 1px solid;">
                            <div class="id-card">
                                <div class="row row1">
                                    <div class="header">
                                        <div class="col-md-3 " style="float:left;padding: 0px;">
                                            <img  src="<?= Yii::getAlias('@web') . "/images/" . $college->logo ?>" style="width:50px; height: 42px;"/>

                                        </div>
                                        <div class="col-md-9" style="text-align: center;padding: 0px;line-height: 1">
                                            <!--<span style="font-size: 10px; color: red !important;font-weight: bold !important"><?= $college->affiliated_to ?></span><br>--> 
                                            <span style='font-size: 10px;color:black !important; font-weight: bold !important'><?= $college->college_name ?></span><br> 
                                            <span style="font-size: 9px; color: blue !important;font-weight: bold !important"><?= $college->address ?></span><br>
                                        </div>
                                    </div>
                                </div>

                                <div class="row row1" style="background-color:#e48b8b;margin-right: -4px !important;margin-left: -4px !important;margin-top:5px ">
                                    <h6 style="margin-top: 3px;margin-bottom: 3px;">Student ID Card</h6>

                                </div>
                                <div class="row row1">
                                    <div class="col-md-6 " style="float:left" >
                                        <div class="photo" >
                                            <img style="width:77px;height:77px;padding-top: 3px;border: 1px solid;" class="" src="<?= \yii\helpers\Url::to(["/image/index", 'name' => $patrons_details['fkStudent']->img, 'fc' => 'student', 'student' => $patrons_details['fkStudent']->id]) ?>" >
                                        </div>
                                    </div>

                                    <div class="col-md-6" >
                                        <div class="qrphoto" >
                                            <div id="qrcode"></div>

                                        </div>
                                    </div>
                                </div>  
                                <h5 style="margin-top:4px !important;
                                    margin-bottom:0px !important;"><b><?= $patrons_details['fkStudent']->first_name . ' ' . $patrons_details['fkStudent']->middle_name . ' ' . $patrons_details['fkStudent']->last_name ?></b></h5>
                                <div class="row row1">

                                    <div class="col-md-12" style="text-align: justify;padding: 0px;line-height: 0.74">

                                        <div class="" style="line-height: 1 !important;padding: 1px 0px 0px 3px !important;margin-top: 6px;">

                                            <?php if ($patron->renual_status == 0) { ?>
                                                <span style="font-size: 10px; color: #6d6969; font-weight: bold;">Card No. :-</span> <span style="font-size: 10px; color :#3e3b3b !important;font-weight: bold;"><?= $patron->patron_id ?></span><br>
                                            <?php } else { ?>
                                                <span style="font-size: 10px; color: #6d6969; font-weight: bold;">Card No. :-</span>  <span style="font-size: 10px;color :#3e3b3b !important; font-weight: bold;"><?= $patron->patron_id ?>-<?= $patron->renual_status ?></span><br>
                                                <?php
                                            }
                                            ?>
                                            <span style="font-size: 10px; color: #6d6969; font-weight: bold;"> Address :-</span><span style="font-size: 10px; color: #3e3b3b; font-weight: bold;"> <?= $patrons_details['fkStudent']->address1 ?></span><br/>
                                            <span style="font-size: 10px; color: #6d6969; font-weight: bold;"> Program :-</span><span style="font-size: 10px; color: #3e3b3b !important; font-weight: bold;"> <?= $patrons_details["fkProgram"]->code . ',' . $patrons_details["fkPeriod"]->code ?></span><br>
                                            <span style="font-size: 10px; color: #6d6969; font-weight: bold;"> Roll No :-</span><span style=" font-size: 10px; color:#3e3b3b !important; font-weight: bold;"> <?= $patrons_details->roll_no; ?> </span><br/>
                                            <span style="font-size: 10px; color: #6d6969; font-weight: bold;"> Phone no.:-</span><span style=" font-size: 10px; color:#3e3b3b  !important; font-weight: bold;"><?= $patrons_details['fkStudent']->phone; ?></span><br>
                                            <span style="font-size: 10px; color: #6d6969; font-weight: bold;"> Validity:-</span> <span style="font-size: 10px; color: #3e3b3b !important; font-weight: bold;"> <?= date('Y/m/d', $patron->valid_till); ?></span>

                                        </div>
                                    </div>                     
                                </div>

                                <hr style="margin-bottom:5px">
                                <div class="row row1">
                                    <div class="col-md-12">
                                        <p>If found please return to office | Tel: <?= $college->phone1 ?> <?php
                                            if (!empty($college->phone)) {
                                                echo ', ' . $college->phone2;
                                            }
                                            ?></p>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                    <?php
                } else {
                    echo '</div><div class="w3-padding-16" style="width: 100%;font-size: 12px;padding: 5px 0px;">';
                }
            }
            ?>
        </div>
    </div>
    <?php
    $this->registerJs($this->render("print.js"), 5);
    ?>

现在在qr.js文件中我的js文件是:

$(document).ready(function () {
    var qrcode = new QRCode("qrcode");
    var f = $('#data').attr('sid');
    qrcode.makeCode(f);
});

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

id="data"更改为class="data"并仅将qr.js添加一次

$(document).ready(function () {
    $(".data").each(function(i,e){
        var qrcode = new QRCode("qrcode");
        var f = $(e).attr('sid');
        qrcode.makeCode(f);
    })
});