代码之家  ›  专栏  ›  技术社区  ›  Friency Fernandez

laravel-在提交时获取数据表中的所有选中项

  •  1
  • Friency Fernandez  · 技术社区  · 7 年前

    我用foreach获取检查项目

    foreach($request->input('ItemNo') as $key => $item_no)
    

    这是我的桌子

    <table id="supplies_t" class="table table-striped">
      <thead>
        <tr>
          <th><input class="main_chkbx" name="main_chkbx" type="checkbox"></th>
          <th>Item Name</th>
          <th>Description</th>
          <th>Unit</th>
        </tr>
      </thead>
      <tbody>
        @if(isset($supplies))
          @foreach($supplies as $supply)
            <tr id="{{ $supply->ItemName }}">
              <td><input name="ItemNo[]" class="ind_chkbx" value="SUPPLY-{{ $supply->SupplyNo }}" type="checkbox"></td>
              <td>{{ $supply->ItemName }}</td>
              <td>{{ $supply->Description }}</td>
              <td>{{ $supply->UnitName }}</td>
            </tr>
          @endforeach
        @endif
      </tbody>
    </table>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Gyrocode.com    7 年前

    出于性能原因,jQuery DataTables从DOM中删除不可见的行。提交表单时,仅将可见复选框的数据发送到服务器。

    解决方案是使用 $()

    解决方案1。提交表单

    你需要转换元素 <input type="checkbox"> <input type="hidden"> 提交表格时。

    var table = $('#example').DataTable({
       // ... skipped ...
    });
    
    $('form').on('submit', function(e){
       var $form = $(this);
    
       // Iterate over all checkboxes in the table
       table.$('input[type="checkbox"]').each(function(){
          // If checkbox doesn't exist in DOM
          if(!$.contains(document, this)){
             // If checkbox is checked
             if(this.checked){
                // Create a hidden element 
                $form.append(
                   $('<input>')
                      .attr('type', 'hidden')
                      .attr('name', this.name)
                      .val(this.value)
                );
             }
          } 
       });          
    });
    

    var table = $('#example').DataTable({
       // ... skipped ...
    });
    
    $('#btn-submit').on('click', function(e){
       e.preventDefault();
    
       $.ajax({
          url: '/path/to/your/script.php',
          data: table.$('input[type="checkbox"]').serialize();
       }).done(function(data){
          console.log('Response', data);
       });
    });
    

    看见 jQuery DataTables: How to submit all pages form data