简体   繁体   English

DataTables Ajax不起作用

[英]DataTables ajax don't work

I have This table which i'm trying to add ajax by adding this code : 我有表,我正在尝试通过添加以下代码来添加ajax:

$('#example').DataTable({
    'serverSide': true,
    "bPaginate": false,
    "info": false,
    "iDisplayLength":20,
    "bLengthChange":false,
    'ajax': {
        type: 'GET',
        'url': 'https://api.myjson.com/bins/ftw5f',
        'data': function(data) {
            return data;
        }
    },
    "columns": [{
        "data": 'Name'
    }, {
        "data": 'Position'
    }, {
        "data": 'Office'
    },
        {
            "data": 'Age'
        },
        {
            "data": 'Start date'
        },
        {
            "data": 'Salary'
        },

    ],

    initComplete: function () { // the filters });

When it's hard coded - like in fiddle - it's working - but when i add the ajax - and remove all the hard coded tr's - the filters don't work.... i checked the console for errors - but there are none.... thanks. 当它进行硬编码时(如在小提琴中一样),它可以工作,但是当我添加ajax并删除所有硬编码的tr时,过滤器不起作用。...我在控制台上检查了错误-但没有。 .. 谢谢。

Your columns declaration is incorrect, the right side should be the key whose value should appear in that column not a label for that column. 您的columns声明不正确,右侧应为键,其值应出现在该列中,而不是该列的标签

If your data is like: 如果您的数据是这样的:

{
  name: '',
  position: '',
  office: '',
  age: '',
  start_date: '',
  salary: '',
}

Then columns should look like: 然后,列应如下所示:

"columns": [{
    "data": 'name'
  }, {
    "data": 'position'
  }, {
    "data": 'office'
  }, {
    "data": 'age'
  }, {
    "data": 'start_date'
  }, {
    "data": 'salary'
  },
],

See the docs for more info: https://datatables.net/manual/ajax#Column-data-points 有关更多信息,请参阅文档: https : //datatables.net/manual/ajax#Column-data-points

I believe your problem is with the property 'serverSide': true . 我相信您的问题出在属性'serverSide': true Once you set this, the search is no longer happening on the client, but on the server, hence you need to write server-side code to handle the search and return a subset of results. 设置此选项后,搜索不再在客户端上进行,而是在服务器上进行,因此您需要编写服务器端代码来处理搜索并返回结果的子集。 If you want to get data from an ajax source, and still process on the client, just delete the serverSide flag. 如果要从ajax源获取数据并仍在客户端上处理,只需删除serverSide标志。

See working fiddle here 这里查看工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM