0

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();
    }
 })
}]
Max B
  • 15
  • 1
  • 6
  • I don't think it's clear what problem is preventing you from just setting `columnData: JSON.parse(string)` – Mark Jul 30 '18 at 16:48
  • Because the string is not a JSON so there would be some parse error presumably. And its not a JSON because I want to be able to pass functions in it. – Max B Jul 30 '18 at 16:50
  • You can't pass functions in json and you need the json for the ajax. Please provide a [mcve] – charlietfl Jul 30 '18 at 16:52
  • Not 100% sure what you're trying to do, but what about something like `var myColumns = result.columnData.split(' ');` ? Makes an array split by the space delimiter. Unless the issue is that not all elements in the array are to be strings... – Isaac Abramowitz Jul 30 '18 at 17:01
  • @IsaacAbramowitz Exactly. Some elements are boolean, other strings. Also see my edit. I am trying to pass a variable name. Is this possible? – Max B Jul 30 '18 at 17:10
  • hmm I'm not sure, maybe an associative array? Not sure if that can be converted to JSON though – Isaac Abramowitz Jul 30 '18 at 17:15
  • See this question for details of associative arrays in JSON: https://stackoverflow.com/questions/4425289/javascript-associative-array-to-json – Isaac Abramowitz Jul 30 '18 at 17:16

0 Answers0