use*_*641 3 javascript ajax jquery datatables
为了在表格中显示我的数据,我使用了dataTable插件。由于我有大量数据,我使用服务器端处理。我的问题是,我获得的 JSON 中有一些数据字段,我想在将其显示在表中之前进行更改。
示例:我获取值,无论某些设备是否可用。这在数据库中写为“”、0 或 1,为了显示它,我想将这些值转换为“是”或“否”或“不适用”。
要初始化表,我使用以下代码:
table = $('#table').DataTable({
"ajax": "myurl",
"sPaginationType": "full_numbers",
"sAjaxDataProp":"",
"deferRender": true,
columns: [
{data: 'attributes.0.value'},
{data:'attributes.1.value'},
{data:'attributes.2.value'},
{data:'attributes.3.value'},
{data:'attributes.4.value'}],
});
Run Code Online (Sandbox Code Playgroud)
直接在数据数组中绑定 conerting 函数是行不通的(比如
{data: convert(attributes.0.value)},
Run Code Online (Sandbox Code Playgroud)
数据表插件有一些参数,我试过了,但我不确定它们是否能解决我的问题。这是插件文档中的一个示例:
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"data": function ( d ) {
d.extra_search = $('#extra').val();
}
}
});
Run Code Online (Sandbox Code Playgroud)
我可以使用 data 参数来解决我的问题(当我尝试这个时, d 总是空的)或者在我将它们集成到表中之前还有另一种可能性来更改我的值吗?
您可以在ajax.dataSrc函数中预处理 JSON ,但由于您确实只需要修改值的显示方式,我将进行列渲染:
columns: [
{ data: 'attributes.0.value',
render: function(data, type, row) {
switch(data) {
case '0' : return 'No'; break;
case '1' : return 'Yes'; break;
default : return 'N/A'; break;
}
},
{ data: 'attributes.1.value' },
{ data: 'attributes.2.value' },
{ data: 'attributes.3.value' },
{ data: 'attributes.4.value' }
]
Run Code Online (Sandbox Code Playgroud)
对render需要转换的每个列值使用上述方法。