{hero}

buttons.exportData()

自版本: Buttons 1.0.0 起

從 DataTable 中獲取適合導出的資料。
請注意 - 此屬性需要 DataTables 的 Buttons 擴充套件。

描述

通常會希望使用 Buttons 從 DataTable 中獲取資料,以便可以以某種形式匯出(複製到剪貼簿、儲存到 Excel 等)。由於許多外掛按鈕類型可能需要此操作,Buttons 為 DataTables 提供此方法,可以快速輕鬆地從 DataTable 中獲取適合匯出的資料形式。

本質上,此方法將返回一個物件,其中包含表格的標頭、頁尾和主體資訊。然後由調用者決定如何匯出該資料 - 例如,對於 CSV 檔案,您可以使用逗號連接每一行的項目,然後使用換行符連接各行。

從 Buttons 1.5.0 開始,此方法匯出的資料將自動嘗試判斷表格中是否有任何列被選取。如果有的話,匯出將限制為這些列。如果沒有選取任何列,則將匯出完整資料集。

如果這不是您想要的行为,請將 modifier 物件的 selected 選項設定為 null。然後,它將包含匯出中的所有列,無論是否選取任何列。同樣地,如果您想強制匯出僅包含選取的列,即使沒有選取任何列(即不會匯出任何內容),請將此參數設定為 true

類型

function buttons.exportData( [ options ] )

描述

從 DataTable 中獲取適合匯出的資料,並儲存到檔案或複製到剪貼簿。

參數
傳回值

一個具有三個參數的物件

  • header - 所選欄的標頭資料陣列
  • footer - 所選欄的頁尾資料陣列
  • body - 陣列的陣列,其中每個內部陣列代表一列,其項目為所選欄的儲存格。

範例

獲取表格中用於匯出的所有資料

var table = new DataTable('#myTable');

var data = table.buttons.exportData();
// Do something with the 'data' variable

僅匯出選取的列(使用Select 擴充套件)

var table = new DataTable('#myTable');

var data = table.buttons.exportData({
	modifier: {
		selected: true
	}
});
// Do something with the 'data' variable

僅獲取可見欄的資料

var table = new DataTable('#myTable');

var data = table.buttons.exportData({
	columns: ':visible'
});
// Do something with the 'data' variable

格式化標頭儲存格 - 新增欄索引

var table = new DataTable('#myTable');

var data = table.buttons.exportData({
	format: {
		header: function (data, columnIdx) {
			return columnIdx + ': ' + data;
		}
	}
});
// Do something with the 'data' variable