簡體   English   中英

如何在表格中添加數據

[英]How to add data in table

我的問題是我當前的code不起作用

當前輸出: http : //jsfiddle.net/4GP2h/50/

這里發生了很多事情:

您正在用單個參數調用它,例如

localStorage.setItem(data) //Uncaught TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 1 present. 

應該是localStorage.setItem('info',data)

這樣您就可以在使用諸如localStorage.setItem('info')類的localStorage.setItem('info') localStorage.getItem()訪問頁面加載時的數據。

我有沒有以往的經驗數據表 ,所以我通過去實例API文檔

擁有數據后,需要在初始化表時指定datacolumns選項( 示例演示如何使用來自js源的數據初始化dataTable ),如下所示:

$('#myTable').dataTable({
  "data": dataSet,
    "columns": [{
    "title": "Name"
  }, {
    "title": "Age"
  }, {
    "title": "Gender"
  }, {
    "title": "Action"
  }]

// rest of the options
});

如您在文檔中所見, data選項接受2D數組; 因此,您必須創建一個包含所有記錄的數組的數組並將其保存在localStorage ,而不是將單個記錄保存在數組中

下面的小提琴使您可以將一個記錄( 我現在將其包裝在另一個數組中 )保存在localStorage ,以備下次訪問時檢索並顯示它。

更新小提琴

正如Anthony在評論中提到的那樣,最好在卸載事件中將其保存一次,而不是每次創建記錄時都嘗試更新localStorage

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM