Adding Dictionary/Map array as data

Tags: #<Tag:0x00007efc70985968>

The data I have stored on my database is stored in an array of key-value pairs, where the key is the column header and the value is the value for the row at current array index. I’m getting the key-value array as a json object and I’m trying to add it to the data field. Currently the data all populates but not in the correct columns.

How do I make sure that the Key-value matches the column header? I can’t hard code it because I have different column headers for different data sets. I don’t completely understand the constructor properties of the table and what datatypes do what when you create one.

Thanks for any help you can give!

Hey @cep79

Here’s a little JSON parse demo that I’ve prepared https://jsfiddle.net/zxrmet9j/ Please describe the strcture of your JSON file and which options are the one to build

  • data
  • row headers
  • column header

Hey thanks for the help!

I’m sending an AJAX request to the server for 2 things:
An array of strings that represent the Column Headers
Example:
colHeaders = [“Sample ID” , “Sample Type” , “Project Name” … ]

And an array of key-value pairs for JSON:
Example:
{
[ “SampleID” : “1131893” , “Sample Type” : “Clinical” , “Project Name” : “Test Project” ] ,
[ “SampleID” : “1131894” , “Sample Type” : “Clinical” , “Project Name” : “Test Project” ] ,
[ “SampleID” : “1131893” , “Sample Type” : “Clinical” , “Project Name” : “Test Project” ] ,
}

Currently it’s populating the column headers properly and reading all the values into the spreadsheet properly. The problem is that the values and the headers aren’t aligned, so project name will be under sampleID and so on. I have a feeling that I can make sure they’re in the correct order by making sure the colHeaders and Values are in the same order, but that seems like it could lead to potential bugs down the line. I’d like to explicitly assign the value to be in the column where the key matches the column header.

I’m also writing the server methods that returns this data, so I can modify things on that end and change up the data being pulled from the database if needed. And I need to also model whether or not a column is editable and if the column is populated with drop down menus. So the column header structure might turn into something like this:

colHeaders = { “SampleID” : [ “Read Only” ] , “Sample Type” : [ “Clinical” , “Research” , “Biodefense” ] , “Project Name” : [] }

(Where the null values mean normal editable columns, “Read Only” means non-editable, and a list of Strings means “Drop down menu with these options”)

I need to be able to model different spreadsheet styles for different projects depending on the user. So I can’t just hard code all of this in.

Do you use columns?

sample https://jsfiddle.net/a3y5bsc2/ once you parse the JSON you can refer to object keys via data in the columns objects.

Is it possible to put all of that in a JSON object and then just assign it to columns? Because I can create various config json objects server side and then return them to the javascript after the AJAX call to generate the spreadsheet.

Ex:
config = [
{ data : ‘Sample ID’ ,
readonly : true } ,
{ data: ‘Sample Type’ ,
type: ‘dropdown’ ,
source : [‘Clinical’ , ‘Research’ , ‘Biodefense’ ] },
{ data: ‘Project Name’ }
]

columns : config

Thanks for the help!

It worked! I just made the column json object on the back end and added it to my config. When I made the AJAX call to get the data for the user I pulled the config for them and added it to the column value. Formatted the whole table correctly and the data is in the correct columns now.

Thanks for your help!

I’m glad to hear that. Feel free to open a new ticket when needed.