从JSON数据指定ID到数据表中的行(Assign ID to datatable row from

2019-09-30 05:24发布

我是新来的DataTable的jQuery插件。 我被困这个超过2天。 我有一个JSON数据,我仍然无法装载表,我也希望分配第一列是该行的ID

这里是HTML:

<table cellpadding="0" cellspacing="0" border="0" class="display"
    id="accDetailTable">
    <thead>
        <tr>
            <th>Currency</th>
            <th>Current/Savings Account No.</th>
            <th>Securities Account No.</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

和我的初始化

var oTable=$('#accDetailTable').dataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": contextPath + "/user/investorAjax?method=getInvestorAccDetailList",
            "iDeferLoading": 57,
    } );

从服务器返回jsonData:

    {"sEcho":1,"iColumns":4,"iTotalRecords":16,"iTotalDisplayRecords":16,
"aaData":
    [{"DT_RowId":2032,"currency":1,"currentAccNo":"aa","secureAccNo":"aa"},
    {"DT_RowId":2033,"currency":1,"currentAccNo":"111","secureAccNo":"111"},
    {"DT_RowId":2034,"currency":1,"currentAccNo":"a","secureAccNo":"aa"},
    ]}
}

但它总是命中:

数据表警告(表ID =“accDetailTable”):添加数据(大小未定义)不匹配已知的列数(3)

Answer 1:

你的数据表正在等待每行三个条目,你给它四强。 在您的表声明(HTML中的一部分)指定一个新的标题单元格<th>在你行的开头。 你会把IDS在里面。 那么你的数据表初始化后,您可以使用隐藏的第一列fnSetColumnVis(index, visibility)功能:

oTable.fnSetColumnVis(0, false); //It will hide your first column

这样做每一行都包含他的ID(DT_RowId),但不显示它。



Answer 2:

这是一种很容易。 我们只是做了微小的变化,因为你想第一列包含ID,您可能需要使用fnRender,请检查数据表的API ,我没加这部分代码:

var oTable=$('#accDetailTable').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": contextPath + "/user/investorAjax?method=getInvestorAccDetailList",
    "aoColumns":[
        {"mDataProp":"currency"},
        {"mDataProp":"currentAccNo"},
        {"mDataProp":"secureAccNo"}
    ]
});


文章来源: Assign ID to datatable row from json data