drnf593779 2019-07-31 04:53
浏览 127

如何使用复选框更新数据表数据?

I'm using Datatables in OctoberCMS and I want to update a particular field's data through checkboxes. I've found a example(https://www.gyrocode.com/projects/jquery-datatables-checkboxes/examples/server-side/server-side-processing/) and it's using a form but I don't know how to update datatables using a form.

Should I use a form and submit the checked data? If I do, how can I return the changed values into the datatables?

Is there no way to update values through the datatables api or function something?

php block and html

<?php
use Jiwon\Byapps\Models\AppsData;

function onStart()
{
    $this['tableName'] = 'BYAPPS_apps_data';
    $this['fields'] = "idx|app_id|app_name|app_ver|byapps_ver|app_process|script_popup|custom_etc|apps_type|start_time|end_time";
}
?>
<table id="datalist" class="table table-striped mb-3" style="width:100%;">
        <thead>
            <tr>
                <th></th>
                <th>app id</th>
                <th>app name</th>
                <th>ver</th>
                <th>BV</th>
                <th>process</th>
                <th>SCRIPT</th>
                <th>custom</th>
                <th>OS</th>
                <th>start</th>
                <th>end</th>
            </tr>
        </thead>

javascript part

$(document).ready(function() {
  var tableId = "datalist";
  var table = $('#' + tableId).DataTable({
      processing: true,
      serverSide: true,
      ajax: {
          url: '/ajax?tb={{ tableName|raw() }}&fd={{ fields|raw() }}',
          type: 'GET',
          error: function(e) {
            console.log(e);
          }
      },
      columnDefs: [
       {
         'targets': 0,
         'checkboxes': {
                'selectRow': true
          }
       }
     ],
     select: {
         'style': 'multi'
      },
      paging: true,
      pageLength: 50,

server-side php file

function onStart()
{
   $table = $_GET['tb'];
   $length = $_GET['length'];
   $start = $_GET['start'];
   $fields = explode("|", $_GET['fd']);
   $searchVar = $_GET['search']['value'];

   if ($searchVar != '') {
     $result = DB::table($table)
               ->where('app_name', 'like', '%'.$searchVar.'%')
               ->orWhere('app_id', 'like', '%'.$searchVar.'%')
               ->orderBy('idx', 'desc')
               ->get();
   } else {
     $result = DB::table($table)
               ->skip($start)
               ->limit($length)
               ->orderBy('idx', 'desc')
               ->get();
   }

   $data = array();

   foreach($result as $row) {
      $sub_array = array();

      for ($i = 0; $i < count($fields); $i++) {

         if (strpos($fields[$i], 'time')) {
           $sub_array[] = gmdate("Y-m-d", $row->{$fields[$i]});
         } else if ($fields[$i] == 'app_process') {
           $arrProcess = [
               1 => 'ready', 2 => 'go', 3 => 'ing', 4 => 'denial',
               5 => 'retry', 6 => 'reexam', 7 => 'complete', 8 => 'stopped',
               9 => 'expired', 10 => 'valid'
            ];

           foreach ($arrProcess as $key=>$val) {
             if($row->{$fields[$i]} == $key) {
                $sub_array[] = $arrProcess[$key];
             }
           }
         } else if ($fields[$i] == 'script_popup') {
             if ($row->{$fields[$i]} == 'Y') {
               $sub_array[] = 'installed';
             } else {
               $sub_array[] = '-';
             }
         } else if ($fields[$i] == 'custom_etc') {
            if ($row->{$fields[$i]} != '') {
              $sub_array[] .= $row->{$fields[$i]}." custom";
            } else {
              $sub_array[] = '-';
            }
         } else {
            $sub_array[] = $row->{$fields[$i]};
         }
      }
      $data[] = $sub_array;
   }

   $output = array(
     "draw" => intval($_GET['draw']),
     "recordsTotal" => DB::table($table)->count(),
     "recordsFiltered" => DB::table($table)->count(),
     "data" => $data,
   );

   echo json_encode($output);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?