【问题标题】:How to set the value for corresponding value in jquery data table如何在jquery数据表中设置对应值的值
【发布时间】:2013-01-28 15:40:27
【问题描述】:

我创建了 mvc 格式的数据表。但是在加载脚本时,它与表数据不匹配,并且不绑定在我的表行中。我的代码如下。

$.ajax({ 
    // my passing value
    success: function (data) { // i get the data properly 
        $('#Table').dataTable().fnClearTable();
        $.each(data, function (k, v) {
            var ID= v.ID;
            var name= v.name;
            var batch= v.batch;

            $('#Table').dataTable().fnAddData([
                ID,
                name,
                batch
            ]);
        });
    }
});

我的桌子:

<table id="Table" width="100%">
    <thead>
        <tr>
            <th>EMP_ID</th>
            <th>EMP_name</th>
            <th>EMP_batch</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

数据表:http://www.datatables.net/

【问题讨论】:

  • 你确定$.each(data, function (k, v) { 真的迭代了一些东西吗?添加console.log / alert 以确认...

标签: jquery ajax model-view-controller datatables


【解决方案1】:

问题的关键在于 AJAX 请求的响应。确保它是由对象数组组成的正确 JSON 对象。像这样:

[
    {
        "ID": 123,
        "name": "slim shady",
        "batch": "smack my batch up"
    },
    {
        "ID": 321,
        "name": "eddie vedder",
        "batch": "yellow ledbetter"
    }
]

您的 JavaScript 工作正常:http://jsfiddle.net/upSvJ/

【讨论】:

  • 谢谢。但是我的内容准备好从我的控制器如何绑定?
猜你喜欢
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多