简体   繁体   English

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

[英]How to register JS file inside foreach loop in Yii2?

I am trying to display QR code inside the ID card. 我正在尝试在身份证内显示QR码。 Now I am trying to print 10 ID cards at single time, so I had put the ID card design inside a for-each loop but I cannot register the JavaScript code inside the for-each loop. 现在,我试图一次打印10张ID卡,因此我已经将ID卡设计放入了for-each循环中,但是无法在for-each循环中注册JavaScript代码。 Only one QR code is display. 仅显示一个QR码。 I mean JavaScript is registered just once inside a loop. 我的意思是JavaScript在循环内只注册了一次。

I have registered JavaScript inside for-each loop and name of .js file is qr.js . 我已经在for-each循环中注册了JavaScript, .js文件的名称是qr.js My Code for ID card design is: 我的ID卡设计代码为:

<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);
    ?>

Now in qr.js file my js file is: 现在在qr.js文件中,我的js文件是:

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

How can i solve this problem? 我怎么解决这个问题?

Change id="data" to class="data" and add qr.js only once 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);
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM