Jquery DataTable 数据绑定

话不多说,直接代码:

第一种方法:

var table;

$(document).ready(function(){

    table =$("#data1").DataTable({

            "columnDefs"[{

                        "visible": false,
                        "targets": 0    

                 }],

            ajax: "/Staff/Search",

            "select": true,
            lengthMenu: [[15, 30, 50, -1], [15, 30, 50, "所有"]],
            pageLength: 15,

           "columns": [

           { "data": "id", "title": "主键" },

            { "data": "name", "title": "姓名" },

            {
                    "title": "性别", "render": function (data, type, row, meta) {
                        var result = "";
                        if (row.gender == "F") {
                            result = "女";
                        }
                        else {
                            result = "男";
                        }
                        return result;
                    }
             },

            {
                    "title": "出生日期", "render": function (data, type, row, meta) {
                        var result = row.birthday;
                        if (result.length == 8) {
                            result = result.substring(0, 4) + "-" + result.substring(4, 6) + "-" + result.substring(6, 8)
                        }
                        return result;
                    }
            }]

});

//以下代码是在保存时或者需刷新dataTable时调用

table.ajax.reload(null, false);

第二种方法:

去掉第一种方法中的ajax属性,

添加方法

function loaddata()

{

     $.get("/Staff/Search", {}).then(function (data) {
       table.clear();
      table.rows.add(data);
       table.draw();
   }).catch(function () {
          $.alert("未能加载正确的数据");
   });

}

//以下代码是在保存时或者需刷新dataTable时调用

loaddata();


你可能感兴趣的:(Jquery DataTable 数据绑定)