nested array
nested array
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?
[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?
This discussion has been closed.
Replies
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
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.
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