weixin_33724570 2017-04-26 14:26 采纳率: 0%
浏览 202

DataTables Ajax不起作用

I have This table which i'm trying to add ajax by adding this code :

$('#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.

  • 写回答

2条回答 默认 最新

  • H_MZ 2017-04-26 15:00
    关注

    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.

    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

    评论

报告相同问题?