javascript - 如何使列过滤在数据表服务器端处理中起作用?
问题描述
我在 OctoberCMS 中使用 Datatables,并且正在尝试使列过滤起作用。我正在使用该$_REQUEST
变量,但我不知道如何正确使用它。拜托,有人帮助我......我被困在这里了。T_T
下面的一些代码显然被省略了。
php块和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部分
$(document).ready(function() {
var tableId = "datalist";
$('#' + tableId + ' thead tr').clone(true).appendTo('#' + tableId + ' thead');
$('#' + tableId + ' thead tr:eq(1) th').not('th.chk').each(function(i){
var title = $(this).text();
$(this).html( '<input type="text" class="filter-input" placeholder="'+title+'" data-column="'+i+'"/>' );
});
var table = $('#' + tableId).DataTable({
processing: true,
serverSide: true,
ajax: {
url: '/ajax',
type: 'POST',
data: {
tb: '{{ tableName|raw() }}',
fd: '{{ fields|raw() }}'
},
error: function(e) {
console.log(e);
}
},
columnDefs: [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
select: {
'style': 'multi'
},
paging: true,
pageLength: 50,
});
$('.filter-input').on( 'keyup click', function () {
var i = $(this).attr('data-column');
var v = $(this).val();
table.column(i).search(v).draw();
});
});
服务器端 php 文件
function onStart()
{
$table = $_POST['tb'];
$length = $_POST['length'];
$start = $_POST['start'];
$fields = explode("|", $_POST['fd']);
$searchVar = $_POST['search']['value'];
$requestData= $_REQUEST;
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($_POST['draw']),
"recordsTotal" => DB::table($table)->count(),
"recordsFiltered" => DB::table($table)->count(),
"data" => $data,
);
echo json_encode($output);
}
我也在下面试过这个。但它记录了无限循环......X(请有人帮助我......!_!
for ($j = 0; $j < $length; $j++) {
for ($i = 0; $i < count($fields); $i++) {
if ($_POST["columns"][$i]["search"]["value"] != '') {
trace_log($_POST["columns"][$i]);
if (in_array($_POST["columns"][$i]["search"]["value"], $data[$j]) != false) {
trace_log($j);
}
}
}
}
解决方案
推荐阅读
- arrays - How to print contents of an array containing a class and how to access certain value of the array in Swift
- curl - Filemaker 18 Curl 不发布文件内容
- java - 当我尝试加载 FXML 文件时,出现 java.io.IOException 错误
- material-ui - 如何在自己的 Material-UI 项目中拥有默认的 MuiTheme 文件
- c++ - 验证我对 CMakeLists.txt 文件的理解
- javascript - 在使用 javascript 的不同页面上时,如何在单独的 header.html 中更改活动导航 li?
- reactjs - 在 useEffect 清理函数中取消所有订阅和异步任务
- javascript - 如何在此输入中仅输入数字和减号
- java - 有没有办法继续重复数组列表。像 arraylist 有 12,但它会在使用相同的列表时继续计数
- swift - 如何为形状添加自定义视图修改器