导出 DataTables 返回 [object Object] 值

huangapple go评论53阅读模式
英文:

Export DataTables return [object Object] value

问题

我想将我的数据表导出为CSV文件。不幸的是,订单历史在CSV文件中返回了[object Object]值。我尝试过查看这个论坛https://datatables.net/forums/discussion/50304/datatable-is-showing-object-object-in-columns#Comment_133450,但我不确定如何在我的代码中应用。

在下面的CSV文件中,订单历史会输出[object Object]。

导出 DataTables 返回 [object Object] 值

至于数据表,它返回了我想要的值。

导出 DataTables 返回 [object Object] 值

这是我的代码:LIVE JS BIN DEMO

Server_Processing.php中的JSON

{
  "draw": 1,
  "recordsTotal": 238633,
  "recordsFiltered": 183514,
  "data": [
    [
      "6789",
      "北城"
    ],
    [
      "5325",
      "南城"
    ]
  ]
}

对于fetch_total.php中的ajax调用,输出console.log(result)为

{"data":[[6]]}
{"data":[[1]]}

基本上我已经将ID_No的值传递给了ajax调用,它会将可读的值返回给单元格。

我尝试过使用JSON.stringify对additionalData进行处理,但在导出为CSV文件时仍然返回[object Object]值。

任何帮助将不胜感激。

英文:

I want to export my datatable as CSV file. Unfortunately the Order History returns [object Object] value in CSV File. I have tried by looking this forum https://datatables.net/forums/discussion/50304/datatable-is-showing-object-object-in-columns#Comment_133450 but I'm not sure how should apply on my code.

Below the csv file, that give output [object Object] for Order History.

导出 DataTables 返回 [object Object] 值

As for the datatables, it return the value that I wanted.

导出 DataTables 返回 [object Object] 值

Here's my code: LIVE JS BIN DEMO

The Server_Processing.php JSON

 {
  "draw": 1,
  "recordsTotal": 238633,
  "recordsFiltered": 183514,
  "data": [
    [
      "6789",
      "North City"
    ],
    [
      "5325",
      "South City"
    ]
  ]
}

Output console.log(result) for fetch_total.php ajax call

{"data":[[6]]}
{"data":[[1]]}

Basically I've pass the ID_No value to ajax call, and it will return the readable value to the cell.

I've tried by using JSON.stringify to the additionalData, it still return [object Object] value when I export as CSV file.

Any help would be greatly appreciated

答案1

得分: 0

解决方案

修复这个问题的最简单方法是在你的DataTable的buttons选项中添加一个exportOptions部分。

所以,不要这样做:

buttons: [ 'csv' ],

你可以这样做:

buttons: [
  {
    extend: 'csv',
    exportOptions: {
      format: {
        body: function ( inner, rowidx, colidx, node ) {
          return node.innerText;
        }
      }
    }
  }
],

你可以为其他按钮(如Excel)重复{ extend: ... }部分。

这个逻辑确保你获取DOM节点(HTML表格单元格)中加载的值,而不是数据表中存储的值(一个对象)。


解释

在你的逻辑中,你使用了这个:

"createdCell": function(cell, cellData, rowData, rowIndex, colIndex) { ... }

这在这里有文档说明。

关键点是:

> cell: 已创建的TD节点。

换句话说,这个<td>元素是你在网页上看到的显示内容 - HTML。它_不是_存储在底层DataTable中的数据(这是用于创建该<td>元素的createdCell函数的内容)。

因此,当你尝试导出数据时,DataTables将使用存储在其内部数据结构中的数据,而不是使用你添加到HTML表格中的数据。

我的exportOptions函数通过告诉DataTables直接查看HTML表格中的数据(node.innerText)而不是使用其内部数据来解决这个问题。


这是一个非常基本的解决方案。它查看HTML表格中的_每一个_<td>单元格,而不仅仅是第三列。在你的情况下,这可能没问题。但如果有其他情况下,你不想使用单元格内容,你可能需要进行细化处理。

另外,我同意@mark_b在问题中的评论:

> 你为了填充"Order History"列中的每一行数据都在单独的Ajax调用吗?肯定有更有效的方法可以在单个请求中获取所有所需的数据。

英文:

Solution

The simplest way to fix this is to add an exportOptions section to your DataTable buttons option.

So, instead of this:

buttons: [ 'csv' ],

you can use this:

buttons: [
  {
    extend: 'csv',
    exportOptions: {
      format: {
        body: function ( inner, rowidx, colidx, node ) {
          return node.innerText;
        }
      }
    }
  }
],

And you can repeat the { extend: ... } section for additional buttons (e.g. Excel).

This logic ensures you take the value loaded into the DOM node (the HTML table cell) instead of the value stored in the DataTable (an object).


Explanation

In your logic, you are using this:

"createdCell": function(cell, cellData, rowData, rowIndex, colIndex) { ... }

This is documented here.

The key point is:

> cell: The TD node that has been created.

In other words, this <td> element is what you see displayed in the web page - the HTML. It is not what is stored in the underlying DataTable (which is the createdCell function used to create the contents of that <td> element).

So, when you try to export your data, DataTables will use the data it has stored in its internal data structures. It does not use the data you added to the HTML table.

My exportOptions function solves that by telling DataTables to look at the data in the HTML table directly (node.innerText) instead of using its internal data.


This is a very basic solution. It looks in every <td> cell in the HTML table, not just the third column. In your case, this is probably fine. But you may need to refine this, if there are other cases where you do not want to use the cell contents.

Also, I agree with the comments made in the question by @mark_b:

> You're making a separate Ajax call for each row of your data in order to populate the Order History column? Surely there is a more efficient way to get all the data you need in a single request?

huangapple
  • 本文由 发表于 2023年2月16日 17:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定