setting multiple rows
setting multiple rows
I have several days trying to figure out how to set multiple rows programmatically.
The scenario is as follows:
I'm working with 2 tables
By selecting a row with the mouse in table nr.1 I get the row's Id, for further use. This is the piece of code:
// Get the selected Row Id and save it in solId to use below
var solId;
$('#levantamientos').on( 'click', 'tr', function () {
var rowId = table.row( this ).id();
var largo = rowId.length;
solId = rowId.slice(4,largo);
console.log( 'Solicitud: ' + solId );
} );
this works pretty good!
Then with a Button event, I go edit the table Nr2. and find out which rows have this index as reference, this, I do as follows:
var dtable= $("#mtto_correctivo_data").dataTable();
var nodes = dtable.fnGetNodes();
// Preprocesar string
var comString = "td:nth-child(2):contains(" + solId + ")";
var rowsSel = $(nodes).filter(function() {
return $(this).find(comString).length ;
});
rowsSel.css('background-color', 'yellow');
You can see that I mark the found rows with yellow background, this also works pretty good!
Then, of course I want to update a field named "status" with a new value!. Here I get a lot of problems, I have tried different methods. By end of the day, I'm trying following:
encontrados = rowsSel.length;
for (i=0; i< encontrados ; i++) {
console.log(rowsSel[i]);
var rowId = rowsSel[i].id;
var largo = rowId.length;
console.log(largo);
rowId = rowId.slice(4,largo); // Get the Id of the to change row
console.log('RowId: ' + rowId + ' Counter: ' + i);
editor.edit( table.row().index(rowId-1),
false
)
editor.set('estatus','COT') // Update status
editor.submit();
}
You can see a lot of console.log outputs for monitoring the process.
The point is, this part is working bad. It is working since it modifies some rows, but not the expected ones. sometimes, despite of that the output of console.log show everything is ok, it affect all rows!
For sure, I'm doing something wrong! but I don't know what!, please help me on this!
On the other side, I was reading in the manual, about a better and more elegant way to do this, something like this:
editor.field('reference.id').multiGet('id');
editor.field('status').multiSet('new_status');
I tried several times, but nothing happens! no errors are shown, no changes in the DOM and not in the Data Base!
What is the correct way to use these API's ?
Thanks in advance Allan!
Replies
Don't run
edit()
in afor
loop. It is asynchronous - i.e. it will still be running when the next loop executes and that is not supported by Editor.Instead, since you are setting the field to a common value, why not just use the multi-row editing feature.
Something like:
where
ids
is an array of the row ids you want to edit.Allan
Thanks for the help Allan, I came forward, however it works perfect the first time, may be a second time, but if I do this 3 o more times, one after the other, it starts to fail, the console output shows that the id array is properly constructed, the rows are correctly market in yellow background but editor do not update all the rows !
I'm not doing this very fast, I do first, go to other menu option in my system, where the rows with changed status appears.
This is the part of the code I have changed:
Can you show me the content's of
rowsIdArray
please? I suspect that it needs a#
in front of each id in order to actually make them ID selectors.Allan
rowsIdArray is a simple array with only the Id numbers, something like this: [5,3,2,0]
How should it looks like?
a) [#5, #3, #2, #0]
b) [#row_5, #row_3, #row_2, #row_0]
It should be an array of strings that can be used as ID selectors. Probably
b)
from your above list (assuming there is arow_
prefix on the id).Allan
Yes!, it is the option b).
Thanks a lot Allan, it works perfect!