从json数据为数据表行分配ID

时间:2013-04-09 03:53:50

标签: javascript datatables

我是datatable jquery插件的新手。 我被困在这两天以上了。我有一个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"},
    ]}
}

但它总是命中:

  

DataTables warning(table id ='accDetailTable'):添加的数据(未定义大小)与已知列数不匹配(3)

2 个答案:

答案 0 :(得分:0)

这很容易。让我们做一个微小的改动,因为你想要第一列包含id,你可能需要使用fnRender,请check the api of datatables,我没有添加这部分代码:

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

答案 1 :(得分:0)

你的数据表正在等待每行三个条目,你给它四个。在您的表声明中(在html部分中)在行的开头指定一个新的标题单元格<th>。你会把ID放进去。然后在dataTables初始化之后,您可以使用fnSetColumnVis(index, visibility)函数隐藏第一列:

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

每行都包含他的id(DT_RowId)但不显示它。