简体   繁体   English

如何使用jQuery更新数据表中的所有多行?

[英]how update all multirows in datatable using jQuery?

I have a datatable of salaries with checkboxes, and a select to fill with chantiers, and a button updateAll, I want when I check the lines of salaries and choose a chantier in select and click on button updateAll, chantier of salaries check modify by value select in select.我有一个带复选框的工资数据表,一个选择来填充 chantiers,还有一个按钮 updateAll,我想要当我检查工资行并在选择中选择一个 chantier 并单击按钮 updateAll,工资检查 chantier 按值修改在选择中选择。 NB: without changing the value of select, ie without touching <option value="1">azilal</option> not <option value="azilal">azilal</option> in my example it changes azilal by 2 but I want to change azilal by tizgui, it's just an example注意:不改变选择的值,即不接触<option value="1">azilal</option> not <option value="azilal">azilal</option>在我的例子中它改变 azilal 2 但我想要通过 tizgui 更改 azilal,这只是一个示例

datatable数据表

  <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
              <select class="form-control" id="chantier">
                      <option selected disabled>Select Location</option>
                      <option value="1">azilal</option>
                      <option value="2">asfalou</option>
                      <option value="3">tizgui</option>
              </select>
           <button class="btn btn-theme update-all" data-url="">Update All</button>
              <table id="example" class="table table-striped table-bordered" style="width:100%">
                 <thead>
                  <tr>
                    <th><input type="checkbox" id="check_all"></th>
                    <th>nom prenom</th>
                    <th>cin</th>
                    <th>matricule</th>
                    <th>chantier</th>
                  </tr>
                 </thead>
                <tbody>
                  <tr id="1">
                    <td><input type="checkbox" class="checkbox" name="customer_id[]" value="1" /></td>
                    <td>MARZOUK NAJIB</td>
                    <td>Pa130191</td>
                    <td>2925019599</td>
                    <td value="1">azilal</td>
                  </tr>
                   <tr id="2">
                    <td><input type="checkbox" class="checkbox" name="customer_id[]" value="2" /></td>
                    <td>achraf bourki</td>
                    <td>pa5000</td>
                    <td>202020</td>
                    <td value="2">tizgui</td>
                  </tr>
                </tbody>
</table>

jQuery jQuery

   <script type="text/javascript">
    $(document).ready(function () {
        $('#check_all').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".checkbox").prop('checked', true);  
         } else {  
            $(".checkbox").prop('checked',false);  
         }  
        });
         $('.checkbox').on('click',function(){
            if($('.checkbox:checked').length == $('.checkbox').length){
                $('#check_all').prop('checked',true);
            }else{
                $('#check_all').prop('checked',false);
            }
         });
        $('.update-all').on('click', function(e) {

          if(confirm("Are you sure you want to update this?"))
  {
   var id = [];
   var chantier;
   $(':checkbox:checked').each(function(i){
    id[i] = $(this).val();
   });
   if(id.length === 0){
    alert("Please Select atleast one checkbox");
   }else{
    let chantier = $('#chantier').val();
      for(var i=0; i<id.length; i++)
      {
        $('tr#'+id[i]).find('td:last-child').html(chantier);
      }  
   }}
  else{
   return false;
  }
    });
    });
</script>
  <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>

Inside javascript, you are selecting value of selected option of #chantier instead of it's text.在 javascript 中,您正在选择#chantier的选定选项的而不是它的文本。
All you need to do is change this line as below:您需要做的就是更改此行,如下所示:

Old line:旧线路:

let chantier = $('#chantier').val();

Replace with below:替换为以下:

let chantier = $('#chantier option:selected').html();

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

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