数据表阿贾克斯需要返回的数据源明确JSON集合名称? [英] DataTables ajax requires explicit json collection name for the returned datasource?

查看:76
本文介绍了数据表阿贾克斯需要返回的数据源明确JSON集合名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jQuery实现数据表的Ajax功能,当最近遇到了一个问题。直到我居然给我的JSON对象集合一个明确的名字,我无法得到任何显示。不应该有一个默认的数据源,如果不返回任何命名?

I recently ran into a problem when implementing the ajax functionality of jquery DataTables. Until I actually gave my json object collection an explicit name I couldn't get anything to display. Shouldn't there be a default data source if nothing named is returned?

客户端控制设置(包括数据提供给动态锚隐藏字段:

Client Side control setup (includes hidden field that supplies data to dynamic anchor:

 $('#accountRequestStatus').dataTable(
      {
          "destroy": true,  // within a method that will be called multiple times with new/different data
          "processing": true,
          "ajax":
              {
                  "type": "GET",
                  "url": "@Url.Action("SomeServerMethod", "SomeController")",
                  "data": { methodParam1: 12341, methodParam2: 123423, requestType: 4123421 }
              }
          , "paging": false
          , "columns": [
                { "data": "DataElement1" },
                { "data": "DataElement2", "title": "Col1" },
                { "data": "DataElement3", "title": "Col2" },
                { "data": "DataElement4", "title": "Col3" },
                { "data": "DataElement5", "title": "Col4" },
          ]
          , "columnDefs": [                            
              {
                  "targets": 0, // hiding first column, userId
                  "visible": false,
                  "searchable": false,
                  "sortable": false
              },
              {
                  "targets": 5,  // creates action link using the hidden data for that row in column [userId]
                  "render": function (data, type, row) {                          
                      return "<a href='@Url.Action("ServerMethod", "Controller")?someParam=" + row["DataElement1"] + "'>Details</a>"
                  },
                  "searchable": false,
                  "sortable": false
              }
          ]
      });

下面是我的服务器端code返回JSON集合的一个片段。结果
tableRows是要显示的包含数据模型的集合

Here's a snippet of my server side code that returns the json collection.
tableRows is a collection of models containing the data to be displayed.

 var json = this.Json(new { data = tableRows });
            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            return json;

正如我以前说过,Ajax调用返回的数据,但不会显示,直到我把收集的名称。也许我错过了文档中的这一必要步骤,但就不是很有意义的控制要连接到一个返回集合作为默认的数据源,而不是需要的姓名?搞清楚名字的东西等同于约2+小时messin的各地尝试不同的东西。这就是我要说的。

As I said before, the ajax call returned data but wouldn't display until I gave the collection a name. Maybe I missed this required step in the documentation, but wouldn't it make sense for the control to wire up to a single returned collection as the default data source and not require the name? Figuring out the name thing equated to about 2+ hours of messin' around trying different things. That's all I'm saying.

也许这会帮助别人太...

Maybe this'll help someone else too...

推荐答案

数据表确实实际上有一个 < STRONG> DATASRC 财产!数据表将寻找是数据或JSON的 aaData 部分。这就是为什么你终于得到了它与工作新的{数据= tableRows} 。也就是说,的如果没有指定 DATASRC是!如果你的JSON从概念不同,您必须指定 DATASRC

dataTables does actually have a dataSrc property! dataTables will look for either a data or an aaData section in the JSON. Thats why you finally got it to work with new { data=tableRows }. That is, if dataSrc is not specified! If your JSON differs from that concept you must specify dataSrc :

如果您返回一个未命名的数组/集合 [{...},{...}]

If you return a not named array / collection [{...},{...}] :

ajax:  {
   url: "@Url.Action("SomeServerMethod", "SomeController")",
   dataSrc: ""
}

如果您返回从数据命名为不同的JSON数组 aaData ,如客户

ajax:  {
   url: "@Url.Action("SomeServerMethod", "SomeController")",
   dataSrc: "customers"
}

如果内容嵌套像 {A:{B:[{...},{...}]}}

ajax:  {
   url: "@Url.Action("SomeServerMethod", "SomeController")",
   dataSrc: "a.b"
}

如果你有非常复杂的JSON或需要操作JSON以任何方式,像樱桃从内容选择 - DATASRC ,也可以是功能:

If you have really complex JSON or need to manipulate the JSON in any way, like cherry picking from the content - dataSrc can also be a function :

ajax:  {
   url: "@Url.Action("SomeServerMethod", "SomeController")",
   dataSrc: function(json) {
       //do what ever you want
       //return an array containing JSON / object literals
   } 
}

希望以上清除的东西了!

Hope the above clears things up!

这篇关于数据表阿贾克斯需要返回的数据源明确JSON集合名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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