The*_*eek 5 javascript mysql jquery datatables
使用DataTables 1.10.19
我有一个MySQL DB,结构如下;
+------+-----------------+----------+----------+
| name | email | status | complete |
+------+-----------------+----------+----------+
| Joe | me@example.com | 1 |1 |
+------+-----------------+----------+----------+
| Jim | you@example.com | 1 |0 |
+------+-----------------+----------+----------+
| Sara | him@example.com | 0 |0 |
+------+-----------------+----------+----------+
Run Code Online (Sandbox Code Playgroud)
我正在使用此脚本从数据库中检索数据.
我的数据表过滤器工作正常搜索时0和1,但这种过滤器都 status和complete列.我想搜索词 active/inactive和complete/ incomplete,而不是 1和0.
我正在使用datatables columns.render选项根据行结果在这些列中呈现自定义输出.
我的DataTable代码是;
$('#example').dataTable({
"ajaxSource": "results.php", // output below
"columns": [{
"data": "name"
}, {
"data": "email"
}, {
"data": "status",
"render": function(data, type, row, meta) {
// row[2] returns an int of 0 or 1 from the db. inactive/active
if (row[2] == 0) {
return `inactive`;
}
if (row[2] == 1) {
return `active`;
}
}
}, {
"data": "complete",
"render": function(data, type, row, meta) {
// row[2] returns an int of 0 or 1 from the db. incomplete/complete
if (row[3] == 0) {
return `incomplete`;
}
if (row[3] == 1) {
return `complete`;
}
}
}, ]
});
Run Code Online (Sandbox Code Playgroud)
该results.php文件返回以下内容;
"data": [
[
"Joe",
"me@example.com ",
"1",
"1",
],
[
"Jim",
"you@example.com ",
"1",
"0",
],
[
"Sara",
"him@example.com ",
"0",
"0",
],
]
Run Code Online (Sandbox Code Playgroud)
我的前端HTML表看起来像这样;
+------+-----------------+----------+------------+
| name | email | status |complete |
+------+-----------------+----------+------------+
| Joe | me@example.com | active |complete |
+------+-----------------+----------+------------+
| Jim | you@example.com | active |incomplete |
+------+-----------------+----------+------------+
| Sara | him@example.com | inactive |incomplete |
+------+-----------------+----------+------------+
Run Code Online (Sandbox Code Playgroud)
目前,过滤器似乎过滤了db int值而不是单元格文本.
如何搜索单词而不是0和1.
下面的似乎工作得很好。输入“Inactive”仅显示 Sara 结果。你在做什么不同的事情?
编辑:我已经更新了代码片段以匹配您的最新代码。看来您正在为您的数据传递一个数组数组,所以我很惊讶您的表甚至正在初始化,因为选项data在这种情况下无效(如果结果集中不存在,如何data选择该属性"name"?应该是数组索引)。将数据选项更新到数组索引后,该表将正确搜索渲染的表。搜索“不完整”会返回 Jim/Sara,而搜索“Inactive”则会返回 Sara。
$(document).ready(function() {
var data = getDummyData();
$('#example').dataTable({
"data": data,
"columns": [{
"data": 0
},
{
"data": 1
},
{
"data": 2,
"render": function(data, type, row, meta) {
// row[2] returns an int of 0 or 1 from the db. inactive/active
if (data == 0) {
return `inactive`;
}
if (data == 1) {
return `active`;
}
}
},
{
"data": 3,
"render": function(data, type, row, meta) {
// row[2] returns an int of 0 or 1 from the db. incomplete/complete
if (data == 0) {
return `incomplete`;
}
if (data == 1) {
return `complete`;
}
}
},
]
});
});
function getDummyData() {
return [
[
"Joe",
"me@example.com ",
"1",
"1",
],
[
"Jim",
"you@example.com ",
"1",
"0",
],
[
"Sara",
"him@example.com ",
"0",
"0",
],
]
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<table id="example">
<thead>
<tr>
<th>name</th>
<th>email</th>
<th>status</th>
<th>complete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
280 次 |
| 最近记录: |