【发布时间】:2019-04-17 16:46:47
【问题描述】:
使用 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 |
+------+-----------------+----------+----------+
我正在使用this script 从数据库中检索数据。
我的数据表过滤器在搜索 0 和 1 时按预期工作,但这会过滤 both status 和 complete 列。我想搜索 单词 active / inactive 和 complete / incomplete 而不是 1 和 0。
我正在使用 datatables columns.render 选项根据行结果在这些列中呈现自定义输出。
我的数据表代码是;
$('#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`;
}
}
}, ]
});
results.php 文件返回以下内容;
"data": [
[
"Joe",
"me@example.com ",
"1",
"1",
],
[
"Jim",
"you@example.com ",
"1",
"0",
],
[
"Sara",
"him@example.com ",
"0",
"0",
],
]
我的前端 HTML 表格是这样的;
+------+-----------------+----------+------------+
| name | email | status |complete |
+------+-----------------+----------+------------+
| Joe | me@example.com | active |complete |
+------+-----------------+----------+------------+
| Jim | you@example.com | active |incomplete |
+------+-----------------+----------+------------+
| Sara | him@example.com | inactive |incomplete |
+------+-----------------+----------+------------+
目前过滤器似乎过滤了 db int 值而不是单元格文本。
如何搜索单词而不是 0 和 1。
【问题讨论】:
标签: javascript jquery mysql datatables