nested array

nested array

jdrmjdrm Posts: 4Questions: 0Answers: 0
edited May 2013 in Editor
I have a function like this to populate my columns on my datatable:
[code]
getColumns: function(){
var columns = [];
_.each(this.model.attributes.attributes, function(item, index) {
columns.push({
sTitle: item.name,
mData: "attributes." + index + ".value"
});
});
return columns;
}
[/code]

As you can Imagine my attributes are in an array so my structure is something like this:

{"id": "-1","attributes": [{"value": "Luis"},{"value": "Torrico"}]}


I have a similar function for the fields to be used on the editor (currently evaluating for purchase):
[code]
getFields: function(){
var fields = [];
_.each(this.model.attributes.attributes, function(item, index) {
fields.push({
label: item.name,
name: "attributes." + index + ".value"
});
});
return fields;
}
[/code]

Everything works fine but when using the editor and creating a new row the data gets structured like this:

{"id": "-1","attributes":{ "0":{ "value":"Luis" },"1":{ "value":"Torrico"} } }

So it is not actually an array of attributes but an object.

I already tried using "attributes[" + index + "].value" as de mData/name prop but it doesn't seems to be working.

Any way to have the editor data to be structured like an array to avoid having to proccess the data before sending it to the backend that expects an array?

Replies

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Hi,

    I'm afraid that you are correct, to have a real array of data, it would indeed need to be post processed - Editor is assigning the values as it it were an object.

    Having said that, how are you submitting this to the server? If its just as HTTP variables, it should work absolutely fine, an object structured in this way and an array should look the same from the server point of view (it will have an index array, but depending on the server, it should still read it as an array).

    Regards,
    Allan
  • jdrmjdrm Posts: 4Questions: 0Answers: 0
    edited May 2013
    I am actually overriding ajax to integrate with backbone.js and then communicating with a Play Framework 2 backend that doesn't seems to be able to deserialize that JSON to a case class.
    Anyway hopefully this post processing may not be that resource and time consuming so I guess I can survive with this.

    Integration of backbone.js with datatables and editor seems to be an interesting topic so I'll put the common portions of my code on a github repo once I finish with some details so I can get some additional feedback.
  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Ah I see - yes. The onSubmit (or similar) events might be what you want to process the data then. I'll take a look and see how easy it might be to offer an option to use real arrays optionally.

    Regarding Backbone.js - you might be interested in some of the changes going into DataTables for v1.10 (which is still in development). In 1.9- DataTables takes a clone of the data fed to it - this is no longer the case in 1.10, so your original data source object is retained. Combined with the new invalidate method, it should make working with Backbone much easier!

    Regards,
    Allan
This discussion has been closed.