【问题标题】:JQuery DataTables Plugin Empty TableJQuery DataTables 插件空表
【发布时间】:2018-04-06 16:31:48
【问题描述】:

我有在 HTML 中构建表格的代码。我们使用字段来应用自定义搜索并更新表格。我想将分页和列排序/过滤添加到表中,因此我尝试使用 datatables.net 中的插件将其转换为 jQuery 数据表。

我尝试创建表,然后使用 .DataTable() 方法对其进行初始化,但无济于事(不添加额外功能)。

我尝试使用数据和列属性进行初始化,但我的表只是空的。

我已尝试遵循以下文档:

我的 jQuery 数据表在开发者控制台中显示一个没有错误的空表。

表:

<table id="productsearchresults">
    <thead>
        <tr id="header">
            <th>
                Name
            </th>
            <th>
                Description
            </th>
            <th>
                Unit Price
            </th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

相关的javascript:

var recordData = j$.map(data.records, function(item) {
    return {
        name: item.Name,
        description: item.Product2.Description,
        unitprice: item.UnitPrice
    };
});

var columns = [
    {data:'name'},
    {data:'description'},
    {data:'unitprice'}
];

console.log(recordData);
console.log(columns);

j$('#productsearchresults').DataTable({
    data: recordData,
    columns: columns
});

控制台输出:

【问题讨论】:

    标签: jquery datatables-1.10


    【解决方案1】:

    您的代码对我来说看起来不错。我尝试使用与您相同的代码来复制该问题,但它对我有用。

    JS

    var recordData = [
        {name:'Item A', description:'Description 1', unitprice:'12.36'},
        {name:'Item B', description:'Description 2', unitprice:'63.98'},
        {name:'Item C', description:'Description 3', unitprice:'1258.99'}];
    
    var columns = [
        {data:'name'},
        {data:'description'},
        {data:'unitprice'}
    ];
    
      $('#productsearchresults').DataTable({
        data: recordData,
        columns: columns
      });
    

    在这里查看:https://codepen.io/anon/pen/OvaJVW?editors=1010

    你能确定你在 datatables js 之前包含了 jQuery 吗?

    【讨论】:

    • 我做到了,我一直在页面上使用 jquery 来处理各种其他的东西。
    • 我复制了你的代码,我得到了一个没有数据的程式化表格 :( 我将它复制到我的空白测试页中,以帮助确保没有冲突或任何东西。
    • 我能够通过使用所有库的最新版本让您在我的测试页面中工作,我将检查我的脚本以确保它们是最新的跨度>
    • 我的 jquery 没有使用最新版本
    猜你喜欢
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多