Dynamically adding / remove columns

Dynamically adding / remove columns

badbytebadbyte Posts: 33Questions: 7Answers: 0

In my database I have a model that has a jobject as a string stored as one of manny attributes. And that JObject does not always have the same properties.
One of the many above mentioned attributes is an attribute named 'groupName'. That attribute groups all the entries together, where the JObject all have the same properties under the specific groupName.
Now, I'm able to filter, actually search, for a specific groupName. But how can I parse the JObject, and add the properties of that JObject onto the columns?

Replies

  • badbytebadbyte Posts: 33Questions: 7Answers: 0
    edited September 2019

    Now, I know that there are threads around already cover such similar cases. My data that I'm feeding datatables looks something like this:
    +-------------------------------------------------------------------------------------------------------------- +
    |attrib1|attrib2|json-object|groupName|.|.|. etc |
    +---------------------------------------------------------------------------------------------------------------+

    Loading the data directly into datatable would handle the json-object as a string, which I don't want to have. I would like to add to the columns the keys of the json object.
    Is there a way of how to do that?

  • allanallan Posts: 61,652Questions: 1Answers: 10,094 Site admin

    Can you show me an example of the JSON data please?

    Allan

  • badbytebadbyte Posts: 33Questions: 7Answers: 0
    {comments: "",
    comments:null,
    dateOnDevelopment:"2019-07-31T09:40:10.86",
    dateOnProduction:"2019-07-31T09:40:10.86",
    errorFlag:false,
    jObject:"{"prop1":"val","prop2":"val","prop3":"val"....}"
    groupName:"foobar-group"
    id:4121
    ruleLength:17
    state:2
    

    What you are seeing above is one db entry, where those rows whose values of 'groupName' have the same properties of jObject.
    Here I'd like to add the properties of jObject to the table header when I chose a groupName (listed in a dropdown menu).
    Then filter and rebuild the body accordingly.

  • allanallan Posts: 61,652Questions: 1Answers: 10,094 Site admin

    Assuming you have loaded your JSON before the DataTable initialisation you could loop over the jOjbect (convert it to be an object first - it looks like a string above) and assign the column titles and properties using columns.title and columns.data.

    You cannot dynamically add and remove columns though once a DataTable has been initialised. If you wanted to handle such a case, then you'd need to destroy the existing table and build a new one with the new columns.

    Allan

  • badbytebadbyte Posts: 33Questions: 7Answers: 0

    Maybe adding / removing columns is the wrong word for what I'm trying to achieve here.
    What I did in the mean time is that I have filtered out the group of rows and manually built the table header by adding the attributes of the jObject, and subsequently added in each row the values for each jObject.
    What I'd need to do now, is to reorder one column all the way to the back.

    Now since jObject has dynamic content, the only way I solved it was to get the keys of jObject and did: keys.push(keys.shift())

This discussion has been closed.