使用数据表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
时可以按预期工作,但是此过滤器同时过滤 status
和complete
列。我想搜索单词 active
/ inactive
和complete
/ incomplete
而不是1
和{{1} }。
我正在使用数据表columns.render选项根据行结果在这些列中呈现自定义输出。
我的DataTable代码是;
0
$('#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
我的前端HTML表如下:
"data": [
[
"Joe",
"me@example.com ",
"1",
"1",
],
[
"Jim",
"you@example.com ",
"1",
"0",
],
[
"Sara",
"him@example.com ",
"0",
"0",
],
]
当前,过滤器似乎是在过滤db +------+-----------------+----------+------------+
| name | email | status |complete |
+------+-----------------+----------+------------+
| Joe | me@example.com | active |complete |
+------+-----------------+----------+------------+
| Jim | you@example.com | active |incomplete |
+------+-----------------+----------+------------+
| Sara | him@example.com | inactive |incomplete |
+------+-----------------+----------+------------+
值,而不是单元格文本。
如何搜索单词而不是int
和0
。
答案 0 :(得分:0)
以下内容似乎正常。键入“不活动”将仅显示Sara。你在做什么不同的事情?
编辑:我已经更新了代码片段以匹配您的最新代码。似乎您正在传递数据的数组数组,因此令您惊讶的是您的表甚至正在初始化,因为在这种情况下data
选项无效(data
如何选择{ {1}}属性(如果它不存在于您的结果集中吗?应该是数组索引)。将数据选项更新为数组索引后,该表将正确搜索呈现的表。搜索“未完成”将返回Jim / Sara,而搜索“未激活”将返回Sara。
"name"
$(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",
],
]
}