Saving data with column key

Tags: #<Tag:0x00007efc64f3ea00>

Hi.

When saving handsontable data to my database, I am looking for a way to include a column key, so if a new column is added at a later date, the current saved data can be loaded back into the correct columns.

Currently, I am using the following:

$.ajax({
	type: "POST",
	url: "http://www.mydomain.co.uk/?ACT=52",
	data: { type: tableType, data: JSON.stringify(hot.getData()) },
	success: function(response) {
		console.log(response);
	}
});

This saves something similar to:

[[null,null,null,null,null,null,null,null,null,null]]

I am hoping for something more like:

[[column_name:null,column_name:null,column_name:null,column_name:null,column_name:null,column_name:null,column_name:null,column_name:null,column_name:null,column_name:null]]

Hi @chrisdavieswebdesign

When you are using objects as data you may prefer to use getSourceData() method. Here’s a comparison https://jsfiddle.net/handsoncode/nLyh9ow3/

Hi @aleksandra_budnik,

Thank you. Though as I am not setting source data, other than what has previously been saved from the handsontable (extracted using ‘hot.getData()’), this doesn’t seem to do the job.

I guess, for this to work, I need a way to extract the contents of the table as an object? As the data output in your example as ‘dataAsObject’ is what I am looking for.

Hi @chrisdavieswebdesign

I just wanted to ask if there’s anything else that I can help you with?

Hi @aleksandra_budnik,

Yes, please. I was hoping you could explain how I can extract the contents of the table as an object?

Hi @chrisdavieswebdesign,

I prepared a simple demo with inline comments, where I described step by step the general vision, how you could use objects as a dataset: https://jsfiddle.net/swistach/nq9sdcke/

Thank you!