简体   繁体   English

将JSON数据表数据以JSON格式发布到服务器

[英]Posting jquery datatable data to server in JSON format

I know this question has been asked numerous times on SO and on the datatables site, but i cannot get it to work using the solutions provided. 我知道这个问题已经在SO和datatables网站上被问过很多次了,但是我无法使用提供的解决方案来解决这个问题。

I am building my datatable on the client side and now want to post the client data to my wep api controller in json format. 我正在客户端建立数据表,现在想将客户端数据以json格式发布到我的wep api控制器。

The client side is working correctly when it comes to adding/deleting rows. 客户端在添加/删除行时工作正常。 But when i try create my JSON using: 但是当我尝试使用以下方法创建JSON时:

    var table = $('#detailTable').DataTable();
var details = JSON.stringify(table.rows().data().toArray());

I am getting the following result: 我得到以下结果:

[["11046","ABC","bis","123","123",15129]]

so basically i am missing column names in my JSON object, thus the web api is failing to pick it up and convert it to: 所以基本上我在JSON对象中缺少列名,因此Web api无法拾取它并将其转换为:

List<ReceiptEntryViewModel> ReceiptDetails

So how can i get the datatables to generate a JSON in the following format: 因此,如何获取数据表以以下格式生成JSON:

[["ItemId":"11046","ItemCode":"ABC","ItemName":"bis","Price":"123","Quantity":"123","Total":15129]]

If you just want to collect the column names and use them as property names in the JSON, then you can build an array of column names this way : 如果您只想收集列名并将其用作JSON中的属性名,则可以通过以下方式构建列名数组:

var fieldNames =  [], json = []
table.settings().columns()[0].forEach(function(index) {
  fieldNames.push($(table.column(index).header()).text())  
})

I am using text() to filter out any HTML. 我正在使用text()过滤掉任何HTML。 Then do as above but iterate over each row instead, and construct an object literal (JSON item) for each row using the collected fieldNames : 然后执行上述操作,但是遍历每行,并使用收集的fieldNames为每行构造一个对象文字(JSON项):

table.rows().data().toArray().forEach(function(row) {
  var item = {}
  row.forEach(function(content, index) {
     item[fieldNames[index]] = content
  })
  json.push(item)
})  

Now you have a valid JSON you can stringify and send to the server. 现在,您可以将有效的JSON进行stringify并发送到服务器。

Small sample demo -> http://jsfiddle.net/5j9wgorj/ 小样本演示-> http://jsfiddle.net/5j9wgorj/

Note that this is just an example. 请注意,这仅是示例。 If you have larger datasets you should avoid forEach and use for -loops instead. 如果数据集较大,则应避免forEach ,而应使用for -loops。 Also there should be no need for blindly rebuilding an array of header names since you, the developer, know both column names and what you want the properties to be named as beforehand. 同样,也不需要盲目地重建标题名称的数组,因为您(开发人员)既知道列名称,又希望您将属性预先命名为什么。


Update with the solution it ended up to be. 更新最终解决方案。 The contortions with building a JSON can be avoided if the dataTable itself is prepared for using JSON. 如果dataTable本身已准备好使用JSON,则可以避免构建JSON的扭曲。 Define a columns section : 定义columns部分:

var table = $('#example').DataTable({
  columns : [
    {  data : 'myField' }
  ]
})

And insert new rows as JSON / literals : 并插入新行作为JSON /文字:

table.row.add({ myField: 'some value' }).draw()

Now 现在

JSON.stringify( table.rows().data().toArray() )

Will return a valid JSON string that can be passed to the serverscript. 将返回可以传递到服务器脚本的有效JSON字符串。

demo -> http://jsfiddle.net/d07f6uvf/ 演示-> http://jsfiddle.net/d07f6uvf/

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

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