如何使用一个按钮Codeigniter更新多行复选框 [英] How to update multple rows checkbox with one button Codeigniter

查看:57
本文介绍了如何使用一个按钮Codeigniter更新多行复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,你们可以帮助我解决此复选框问题吗?如果我检查该列并按update,则希望更新在当前时间进行。

Hey guys can you guys help me with this checkbox problem? If I check the column and press update, I want the update to happen at current time.

这是一个示例

我想要我是否选中复选框并按更新按钮和该列将会在当前时间更新

I want if I check the checkbox and press the update button and the column will update at the current time

这是我的观点:

<div class="container">      
  <div class="row">
    <div class="col-md-10">
      <div class="panel panel-info">
        <div class="panel-heading">Setting Jam & Tanggal Mesin</div>
        <div class="panel-body">
              <table id="devid_auto" class="table table-bordered dt-responsive" cellspacing="0" width="100%">
                <thead>
                  <tr>
                    <th width="1%" align="center"><input type="checkbox" onClick="toggle(this)" id="checkAll" name="checkAll" /></th>
                    <th width="10%" align="center">Nama Mesin</th>
                    <th width="5%" align="center">IP Address</th>
                    <th width="10%" align="center">SN</th>
                    <th width="5%" align="center">Ethernet Port</th>
                    <th width="10%" align="center">Last Update</th>
                  </tr>
                </thead>
                <tbody>
                <?php 

                foreach($data as $d){

                ?>
                  <tr>
                    <td><input class="childbox" type="checkbox" name="msg[]" id="id" value="<?php echo $d['devid_auto'] ?>" /></td>
                    <td class="data-check"  ><?php echo $d['device_name']; ?></td>
                    <td class="data-check"  ><?php echo $d['ip_address']; ?></td>
                    <td class="data-check"  ><?php echo $d['sn']; ?></td>
                    <td class="data-check"  ><?php echo $d['ethernet_port']; ?></td>
                    <td class="data-check"  ><?php echo $d['lastupdate_date']; ?></td>
                  </tr>
                <?php } ?>
                </tbody>                    
              </table>

              <div class="form-group">
                <label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">Tanggal & Waktu Sekarang</label>
                <div class="col-sm-3">
                  <input type="" class="form-control" id="" value="<?php date_default_timezone_set("Asia/Jakarta"); echo date(" Y-m-d H:i:s ");?>""  readonly>
                </div>  
              </div>
        </div>
        <div class="panel-footer text-right">
          <button allign="center" class="btn btn-danger" onclick="update()">Update Date and time</button>
        </div>
      </div>
      <script src="<?php echo base_url() ?>assets/baru/jquery-3.2.1.min.js"></script>
      <script src="<?php echo base_url() ?>assets/baru/jquery.dataTables.min.js"></script>
      <script src="<?php echo base_url() ?>assets/baru/dataTables.bootstrap4.min.js"></script>
      <script type="text/javascript">
          $(document).ready( function () 
          {

              $('#devid_auto').DataTable();

              $("input[name='checkAll']").click(function() {
                    var checked = $(this).attr("checked");
                    $("#devid_auto tr td input:checkbox").attr("checked", checked); });

          });

          function toggle(id) 
          { 
            checkboxes = document.getElementsByName('msg[]'); 
            for(var i=0, n=checkboxes.length;i<n;i++) { 
              checkboxes[i].checked = id.checked; 
            } 
          }

          function update(id)
          {
              var list_id = [];
              $("#id:checked").each(function() {
                      list_id.push(parseInt(this.value));
              });
          console.info(JSON.stringify(list_id));

              if(list_id.length > 0)
              {
                  if(confirm('Are you sure update this '+list_id.length+' data?'))
                  {
                      $.ajax({
                          type: 'POST',
                          data: {'devid_auto': list_id},
                          url: '<?php echo site_url('setting/mesin_update')?>',
                          success: function(result)
                          {
                              var hasil = result.replace(/\s/g,'');
                              if(hasil == 'y')
                              {
                                alert("Data Berhasil di Update");
                                location.reload();
                              }
                              else
                              {
                                  alert('Failed.');
                              }

                          },
                          error: function (jqXHR, textStatus, errorThrown)
                          {
                              alert('Error update data');
                          }
                      });
                  }
              }
              else
              {
                  alert('no data selected');
              }

          }
            </script>

    </div><!-- end cols-->
  </div><!--end row-->
</div> <!-- end container-->    

这是我的控制者:

public function mesin_update()
    {
        $initiated_date =  date('Y-m-d H:i:s');
        $data = array(
                'lastupdate_date' => $initiated_date,
            );
        $devids = $this->input->post('devid_auto', True);
        foreach($devids as $devid){
           $this->Jam_tgl_model->jam_tanggal_update(array('devid_auto' => $devid ), $data);
        }
        echo 'y';
    }

这是我的模型:

public function jam_tanggal_update($where, $data)
{
    $this->db->update($this->table, $data, $where);
    return $this->db->affected_rows();
}

我已经尝试过使用我的代码,但列最后更新日期没有任何更改

I have tried it with my code but nothing has changed on column last update date

请帮助修复我的代码,以便我可以完成它

please help fix my code so i can finish it

(新编辑):
-结果如下:
-我检查了这一行
-并按更新按钮,您可以更改脚本功能更新(id)吗?
-这是点击更新按钮后的结果

推荐答案

in contorller

$devids = $this->input->post('devid_auto', True);
foreach($devids as $devid){
   $this->Jam_tgl_model->jam_tanggal_update(array('devid_auto' => $devid), $data);
}




注意:您也可以使用ci $ this -> db-> update_batch()

NOTE: also you can use ci $this->db->update_batch()

这篇关于如何使用一个按钮Codeigniter更新多行复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆