在带有AJAX数据源的DataTables中使用列名 [英] Using column names with DataTables with AJAX data source

查看:53
本文介绍了在带有AJAX数据源的DataTables中使用列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的系统升级为使用1.10而不是1.9的DataTables,并且试图找到一种使用JSON对象而不是列表来传递行内容的方法.具体来说,不是传回 [[''data','data','data'],['data','data','data']等格式的数据.] 我想要以 [[''colA':'data','colB':'data','colC':'data']] 的格式输入.

I'm trying to upgrade my system to use 1.10 instead of 1.9 of DataTables and I'm trying to find a way to pass back the row contents using a JSON object instead of a list. Specifically instead of passing back data in the format [['data','data','data'],['data','data','data'],etc..] I want to put it in the format [['colA':'data','colB':'data','colC':'data']].

现在,我有AJAX函数以该格式返回数据,并且尝试使用以下代码进行初始化:

Right now I've got my AJAX function returning the data in that format and I'm trying to initialize with this code:

$("table").DataTable({
    "columnDefs": [
        {"name": "wo_status", "title": "wo_status", "targets": 0},
        //repeat for each of my 20 or so fields
    ],
    "serverSide": true,
    "ajax": "url/to/ajax/function"
});

结果正确地从我的AJAX函数返回,但是DataTables试图在第0行中找到索引0,但是却找不到它,因为我的表单元格是通过其列名而不是数字索引来索引的.有谁知道如何告诉DataTables使用在 columnDefs 中指定的列名(或在我没有找到的其他选项中)而不是数字索引?

The results are coming back from my AJAX function correctly but DataTables is trying to find an index of 0 in row 0 and failing to find it because my table cells are indexed by their column name instead of a numerical index. Does anyone know how to tell DataTables to use the column names specified in columnDefs (or in some other option I haven't found) instead of numerical indices?

推荐答案

使用 columns.data 选项以指定属性名称,如下所示:

Use columns.data option to specify property names as shown below:

$("table").DataTable({
    "columns": [
        { "data": "colA", "name": "colA", "title": "colA" },
        { "data": "colB", "name": "colB", "title": "colB" },
        { "data": "colC", "name": "colC", "title": "colC" }
        //repeat for each of my 20 or so fields
    ],
    "serverSide": true,
    "ajax": "url/to/ajax/function"
});

这篇关于在带有AJAX数据源的DataTables中使用列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆