Skip to content

Instantly share code, notes, and snippets.

@itanex
Created July 25, 2018 03:34
Saving handsontable data
(function ($) {
if ($) {
var hst = (function () {
var $container = $("#hst");
$container.handsontable({
startRows: 8,
startCols: 6,
rowHeaders: true,
colHeaders: true,
minSpareRows: 1,
contextMenu: true
});
hst = $container.data('handsontable');
var $dataLocker = $("#dataLocker").val();
if ($dataLocker.length > 0) {
var data = JSON.parse($dataLocker);
hst.loadData(data);
}
return hst;
})();
// Enable a click handler for the button to save data and submit the form
$("#saveContent").click(function (e) {
// Save the table data before sending the form
$("#dataLocker").val(JSON.stringify(hst.getData()));
$("body > form").submit();
});
}
})(jQuery);
using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json;
namespace HandsonDemo
{
public partial class _Default : System.Web.UI.Page
{
protected HtmlInputHidden dataLocker;
private List<List<int?>> Data = new List<List<int?>>
{
new List<int?> { 1, 2, 3, 4, 5, 6 },
new List<int?> { 7, 8, 9, 10, 11, 12 },
new List<int?> { 13, 14, 15, 16, 17, 18 },
new List<int?> { 19, 20, 21, 22, 23, 24 },
};
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
// if we are posting data to the server deserialize it so that we can manipulate it
Data = JsonConvert.DeserializeObject<List<List<int?>>>(dataLocker.Value);
}
// store the data object to be rendered to the handsontable
dataLocker.Value = JsonConvert.SerializeObject(Data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment