很多时候,我们显示的数据都是从后返回的,数据格式也是约定好的。但是后台人员为了节省资源,提高性能,读取数据时会把空数据不输出。这种情况下,使用dataTable插件就会出现错误提示,如(DataTables warning: table id=dataList – Requested unknown parameter ‘nowSn’ for row 0. For more information about this error, please see https://datatables.net/tn/4)。所以要求我们在绑定前先做一下数据判定,数据为null或undefined时补完全。
- for (var i = 0; i < tableData.length; i++) {
- if (tableData[i]['avatar'] == null) {
- tableData[i]['avatar'] = "";
- }
- if (tableData[i]['userName'] == null) {
- tableData[i]['userName'] = "";
- }
- if (tableData[i]['email'] == null) {
- tableData[i]['email'] = "";
- }
- if (tableData[i]['phone'] == null) {
- tableData[i]['phone'] = "";
- }
- if (tableData[i]['nowSn'] == null) {
- tableData[i]['nowSn'] = "";
- }
- }
或者是
- {
- "data": "companyName",
- "render": function(data, type, row, meta) {
- return (row.companyName === undefined) ? '' : row.companyName;
- }
- }
转载请注明:TUTERM.COM » 表格插件dataTable使用篇