【问题标题】:How do I use the defer render functionality with my datatable如何在我的数据表中使用延迟渲染功能
【发布时间】:2016-11-21 13:23:01
【问题描述】:

我有一个希望使用延迟渲染的数据表,我不确定问题是什么,我的控制器方法返回一个 json 对象数组。请参阅下面的代码。

**数据表设置**

页面加载时调用。

        var $dtTable = $("#tblPlayer");
        $dtTable.dataTable({
            bFilter: false,
            pageLength: 10,
            paging: true,
            autoWidth: true,
            columns:
            [
                null,
                { "orderDataType": "dom-text", type: "string" },
                { "orderDataType": "dom-text", type: "string" },
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-text-numeric" },
                null,
                null,
                null
            ],
            "ajax": "Player/GetSetPlayers",
            "deferRender": true
        });

控制器方法

    public object[] GetSetPlayers()
    {
        var players = GetPlayers();

        _players = new object[players.Count];

        for (var i = 0; i < players.Count; i++)
        {
            _players[i] = players[i];
        }
        return _players;
    }

GetSetPlayers() 返回一个 json 对象数组,下面的结果是索引 0 和 1 将包含什么的示例。

回应

[  
   {  
      "product":25000,
      "rank":1,
      "dirty_money":25000,
      "id":"b4b41b18edbb49b9ae80be5e768b6b80",
      "name":"Dan",
      "ban_status":0,
      "edit":"<a href='/support/player_gamedata/b4b41b18edbb49b9ae80be5e768b6b80/game' class='btn'><i class='icon-folder-close'></i></a>",
      "credit":30,
      "clean_money":20000,
      "ban":"<a href='/support/ban_player/by_id/b4b41b18edbb49b9ae80be5e768b6b80/' class='btn'><i class='icon-remove'></i></a>",
      "supplies":25000
   },
   {  
      "product":25000,
      "rank":1,
      "dirty_money":25000,
      "id":"3cac6e366170458686021eaa77ac4d6d",
      "name":"Dan",
      "ban_status":0,
      "edit":"<a href='/support/player_gamedata/3cac6e366170458686021eaa77ac4d6d/game' class='btn'><i class='icon-folder-close'></i></a>",
      "credit":30,
      "clean_money":20000,
      "ban":"<a href='/support/ban_player/by_id/3cac6e366170458686021eaa77ac4d6d/' class='btn'><i class='icon-remove'></i></a>",
      "supplies":25000
   }
]

【问题讨论】:

    标签: c# jquery json datatable deferred-rendering


    【解决方案1】:

    我认为您需要在需要从返回的数据中填充的每一列上指定一个“数据”属性。否则,它不知道 json 对象上的哪个属性进入每一列。如果您将数据定义为数组数组而不是对象数组,则不需要。

    此外,您没有指定不需要的“dataSrc”选项,但如果未设置,我相信它期望返回的 JSON 为以下形式:

    {
      data: [ {...}, {...} ]
    }
    

    因此,如果您可以将原始响应添加到 ajax 请求中,将会很有帮助。

    EDIT1:

    好的,请仔细检查,是的,它确实需要一个对象数组分配给 JSON 对象的“数据”属性,因此您可以执行类似的操作来解决此问题,而无需修改服务器上的任何内容。只需将您的 ajax 选项更改为:

    "ajax": {
      "url": "Player/GetSetPlayers",
      "dataSrc": function ( json ) {
          // We need override the built in dataSrc function with one that will 
          // just return the object array instead of looking for a "data" 
          // attribute on the "json" object.  Note if you ever want to add 
          // serverside sorting/filtering/paging you will need to move your table 
          // data to an attribute within the JSON object.
          return json;
       }
    }
    

    因此,一旦您完成上述两个修复,您可能会很好。

    【讨论】:

    • @ife labolz 您是否尝试在将要包含数据的每个列上设置“数据”属性来运行它?它只是“数据:'jsonAttrName',”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多