1

My problem is in my current code it doesn't work

current output: http://jsfiddle.net/4GP2h/50/

user3817023
  • 59
  • 1
  • 2
  • 10

1 Answers1

2

There are many things going on here:

You're calling it with single parameter, like

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

Which should be something like localStorage.setItem('info',data).

So that you can access the data upon page load using localStorage.getItem() like localStorage.setItem('info').

(I had no previous experience with datatables, So i went through the examples and API documentation)

Once you have the data, you need to specify the data and columns options while initializing the table (this example demonstrates how to initialize dataTable with data from a js source.) as follows:

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

// rest of the options
});

As you can see in the documentation, the data option accepts a 2D array; So instead of saving a single record in an array, you've to create an array of arrays of all the records and save it in localStorage

The fiddle below lets you save one record (I'm wrapping it in another array for now) in localStorage, retrieve and display it next time you visit.

Updated Fiddle

As mentioned by Anthony in comments, it's better to save it once in unload event rather than trying to update localStorage every time a record is created.

Community
  • 1
  • 1
T J
  • 42,762
  • 13
  • 83
  • 138
  • i found a bug in ur code it only store 1 row. If i add 2 rows and refresh the page it only store the last row i added. Can u fix it? – user3817023 Jul 12 '14 at 10:39
  • i want to store all the current contents of the table before it the browser was refresh or closed. – user3817023 Jul 12 '14 at 11:07
  • @user3817023 it's not a bug, that's what i mentioned in answer that you've to create an array of arrays; i fixed the problems in your code and gave you something to build upon (*i'm new to datatable than you are and actually spent few hours reading documentation and fiddling until the power gone*) it's easy to do the rest... don't expect copy pastable answers, those won't do you any good in long term since you won't learn anything. The reason i left all those links and explained how i got till here is because somebody will learn something and know how to get to a solution next time... :) – T J Jul 12 '14 at 14:28
  • im confused with your code why you make another variable called data? in my code i created a var data in save function already and you declare a variable data again? – user3817023 Jul 12 '14 at 21:12
  • @user3817023 javascript has function level scope. the one in your function is inside that functions scope, so that's not a problem. I used the same name for better understanding that we're getting the same data on load which we saved previously. Even if it's the same variable what's the problem..? at page load we can use it to store data from localStorage and afterwards we can use it to store data saved by user... What's the problem with it..? i don't understand your confusion. – T J Jul 13 '14 at 05:08