jcnreds jcnreds - 3 months ago 37
JSON Question

Handsontable : How to choose which data to load?

I have a json :

[{"name":"AA","age":"19","sex":"1","add":"yy","phone":"12345"},
{"name":"BB","age":"20","sex":"0","add":"yy","phone":"43224"},
{"name":"CC","age":"11","sex":"1","add":"yy","phone":"67863"},]


I want handsontable to load only name, age, but I don't know how to that :(

I think I should make a new object which only has name and age => load to handsontable. Can I do that?

Can anyone help me? Thank you very much :D

Answer

There are several ways to do this. You could do it the way you initially suggested and modify your JSON object to only return those fields. Or, you could do it like this:

var data = [
 {"name":"AA","age":"19","sex":"1","add":"yy","phone":"12345"},
 {"name":"BB","age":"20","sex":"0","add":"yy","phone":"43224"},
 {"name":"CC","age":"11","sex":"1","add":"yy","phone":"67863"}
];

var hot = new Handsontable(container, {
  ...
  data: data,
  colHeaders: ['Name', 'Age'],
    columns: [
      {data: 'name'},
      {data: 'age'}
    ],
  ...
});

Using the colHeaders and columns options as in my example above gives you more flexibility with your data on the front end. :)

Working fiddle

Comments