doukang7858 2018-01-19 16:50 采纳率: 0%
浏览 144

MDL复选框功能MaterialCheckbox.check()不适用于ajax内容

I am working with MDL Checkbox where i need to delete all values at once. As i did some research and looks like old method is deprecated : Deprecations for MDL Data Table Checkbox and mdl-data-table--selectable can't be used anymore.

HTML Code:

  <div class="load_customer">
      <table class="mdl-data-table mdl-js-data-table  mdl-shadow--2dp mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-cell--7-col-phone">
      <thead>
        <tr>
        <th>
            <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" id="table-header" for="table-input">
                <input type="checkbox" id="table-input" class="mdl-checkbox__input" />
              </label>
        </th>
        <th>Sr. No</th>
         <th>Name</th>
         <th>Gender</th>
         <th>Date of Birth</th>
        <th>Anniversary</th>
       <th>Address</th>
       <th>Phone</th>
        </tr>
      </thead>
      <tbody>
      <?php
      $i=1;
      $select = $pdo->query("SELECT * FROM `accounts` ORDER BY `id` DESC");
      while($fetch = $select->fetch()){
      ?>
        <tr>
            <td>
             <label class="mdl-checkbox child_checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" data="<?php echo $fetch['id'];?>" for="row[<?php echo $i;?>]">
                <input type="checkbox" id="row[<?php echo $i;?>]" class="mdl-checkbox__input" />
            </label>
            </td>
            <td><?php echo $i;?></td>
          <td><?php echo $fetch['name'];?> </td>
          <td><?php echo $fetch['gender'];?></td>
          <td><?php echo $fetch['dob'];?></td>
          <td><?php echo $fetch['anniversary'];?></td>
          <td><?php echo $fetch['address'];?></td>
          <td><?php echo $fetch['phone'];?></td>
        </tr>
       <?php
       $i++;
        }
       ?>
      </tbody>
    </table>
    </div>

So after adding it manually it works fine until i load table with ajax. Then it all stops working. I am able to check main checkbox #table-header, but function which checks all checkboxes (MaterialCheckbox.check()) does not works anymore. I checked console & there are no errors in console.

Javascript Code

var arr = [];
$(document).on("click","#table-header input",function() {

    var all_elements = document.querySelectorAll('.child_checkbox');

        for(var i=0, n=all_elements.length; i<n; i++){
            var element = all_elements[i];

            if($('#table-header input').is(":checked")) {

                element.MaterialCheckbox.check();

                var ids = element.getAttribute("data");
                arr.push(ids);

        }
          else {
                element.MaterialCheckbox.uncheck();

                var removeItem = element.getAttribute("data");
                arr = $.grep(arr, function(value) {
                return value != removeItem;
                });
          }
        }

    });

I am using componentHandler.upgradeAllRegistered() and Checkbox is working fine, but it does not loop through all checkboxes.

$.post("load_customer.php",function(data){
            $(".load_customer").html(data);
            componentHandler.upgradeAllRegistered();            
    });

Please help me with this.Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源