I need a JavaScript variable in the form:
var myColumns = [{
name : "Name",
type : "string",
visible : true,
filterType : "search",
width : 200
},
{...}
]
As per this Dynamic Table JavaScript spreadsheet API.
As part of an AJAX call I am making, I am returning a JSON of the form:
{
"columnData": "data as per var myColumns above",
"cellData": {
"cell1": "value1",
"cell2": "value2"
}
}
My issue is that when I get "columnData" from the JSON that the ajax call has returned and set it as a variable, JavaScript interprets it as a string, not an array.
var myColumns = result.columnData;
var myData = result.cellData;
However, if I were to define a new variable and copy paste the contents of the "columnData", JavaScript sees this as an array (rightly so, and the behaviour I was expecting).
So how to I formulate my JSON in such a way that I can extract an array from it, to satisfy the column definition?
You may be saying to yourself, "just set the column definition as a JSON and parse it". However I need to pass some functions in the column definition so I cannot do that in JSON (if my understanding is correct).
Edit: The reason I want columnData to be an array of definitions is because I want to be able to return something like this:
[{
name : "Configuration ID",
type : "string",
visible : true,
filterType : "search",
width : 70,
field : "configuration_id"
editor : editableText
}]
Where editableText is a JavaScript variable:
var editableText = $("<div/>").dynamicTableEditor({
editHandler: function(aData, aContext) {
$("#save-data").html("Saving note: <strong>" + aData + "</strong>");
executeAsync();
}
});
And so if I do:
var columnDefinition = results.columnData
console.log(columnsDefinition);
The output would be:
[{
name : "Configuration ID",
type : "string",
visible : true,
filterType : "search",
width : 70,
field : "configuration_id"
editor : editableText = $("<div/>").dynamicTableEditor({
editHandler: function(aData, aContext) {
$("#save-data").html("Saving note: <strong>" + aData + "</strong>");
executeAsync();
}
})
}]