How can I update datatables data using the checkbox?
How can I update datatables data using the checkbox?
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";
}
<?php
>
```
?>
<
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);
}
```
Answers
Hi @Jiwon ,
Are you using Editor? This allows you to quickly setup editing on the client-side.
Cheers,
Colin
@colin, Thank you for your answer.
But I'm working this in the server-side.