When I use jQuery AJAX to load data from SQL Server and use the “columns” attribute at the same time, the data does not show in the Handsontable.
If I don’t use the “columns” attribute, it works just fine. Also, If I set the “data” attribute explicitly when using the “columns” attribute it also works fine.
Example 1: If explicitly set the “data” attribute it works fine.
<script type="text/javascript">
$(document).ready(function () {
const vCURRENTPAGE = window.location.pathname;
const vDIVCONTAINER = document.querySelector('#DivHandsontable');
const vHOT = new Handsontable(vDIVCONTAINER, {
data: [ ['A1', 'B1', 'C1', 'D1', 'E1', 'F1'], ],
colHeaders: true,
rowHeaders: true,
autoWrapRow: true,
autoWrapCol: true,
height: 'auto',
headerClassName: 'htCenter',
columns: [{ headerClassName: 'htRight' }, { headerClassName: 'htLeft' }, {}, {}, {}, {}],
licenseKey: 'non-commercial-and-evaluation',
});
});
</script>
Example 2: However, If I set the “data” attribute using AJAX which gets data from SQL server, it doesn’t work. The Handsontable is blank. If I don’t use the “columns” attribute the data loads fine. When I do use the “columns” attribute the data does not load.
<script type="text/javascript">
$(document).ready(function () {
const vCURRENTPAGE = window.location.pathname;
const vDIVCONTAINER = document.querySelector('#DivHandsontable');
const vHOT = new Handsontable(vDIVCONTAINER, {
colHeaders: true,
rowHeaders: true,
autoWrapRow: true,
autoWrapCol: true,
height: 'auto',
headerClassName: 'htCenter',
columns: [{ headerClassName: 'htRight' }, { headerClassName: 'htLeft' }, {}, {}, {}, {}],
licenseKey: 'non-commercial-and-evaluation',
});
ExecuteAction_LoadSqlServerJson();
function ExecuteAction_LoadSqlServerJson() {
$.ajax({
type: "POST",
url: vCURRENTPAGE + "/LoadSqlServerJson",
data: '{MyRecordID: "1" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (MyResponse) {
var vMYDATA = JSON.parse(MyResponse.d);
vHOT.loadData(vMYDATA);
$('#DivMessages').html('AJAX success response: Data loaded from SQL Server.');
},
error: function (jqXHR, textStatus, errorThrown) {
$('#DivMessages').html('AJAX failure response: An error occurred.');
}
});
};
});
</script>
Extra Information
In case you need it, here’s the structure of my aspx page and the associated code behind page.
My main aspx page:
<%@ Page Language="VB" blah, blah, blah %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<title>Handsontable Question</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.css" />
</head>
<body>
<form id="form1" runat="server">
<div id="DivHandsontable"></div>
<div id="DivMessages" style="margin-top: 24px;"></div>
</form>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function () {
//... see the two examples I provide above ... //
});
</script>
</body>
</html>
My associated code behind page:
Imports System.Data.SqlClient
Partial Class column_property_with_sqlserver_data
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()>
Public Shared Function LoadSqlServerJson(ByVal MyRecordID As String) As String
Dim MySqlServerResult As String = ""
'... code to retrieve data from SQL server in JSON format and set result to the MySqlServerResult variable ... '
LoadSqlServerJson = MySqlServerResult
End Function
End Class