I have a model that, when a new row is added, sets 2 default values through the model Actions. I also need to be able to duplicate a record in the model, but I can’t use the “clone” button option because I need to do multiple things in the process. I have a button that changes a value in the current record (deactivate), and then runs a custom snippet to add a new row and use the values from the pre-existing row (see code below). The problem I’m seeing is that the 2 default values established in the model Actions seem to override the JS and the values I need. Can the code below (I got it from an older post) be modified to override the defaults? Are there other options I’m missing?
var params = argumentsa0], row = params.item.row,
model = params.model,
$ = skuid.$;
var newRow = model.createRow();
if (row) {
$.each(row,function(fieldId,val) {
if ((fieldId != ‘attributes’) && (val != null) && (fieldId != ‘Id’)) {
var modelField = model.getField(fieldId);
if ((typeof val === ‘object’)
|| (modelField && modelField.createable)) {
model.updateRow(newRow,fieldId,val);
}
}
});
}
Scott, your approach makes sense, here’s a slightly more optimized approach using a multi-value variant of model.updateRow():
var params = argumentst0], <br> row = params.item.row,<br> model = params.model,<br> $ = skuid.$;<br>var newRow = model.createRow();<br>if (row) {<br> var rowUpdates = {};<br> $.each(row,function(fieldId,val) {<br> if ((fieldId != 'attributes') && (val != null) && (fieldId != 'Id')) {<br> var modelField = model.getField(fieldId);<br> if ((typeof val === 'object') <br> || (modelField && modelField.createable)) {<br> rowUpdatesefieldId] = val;<br> }<br> }<br> });<br> model.updateRow(newRow,rowUpdates);<br>}
Thanks for the response Zach… but it is still using the model Action defaults over the Snippet… I’m a complete novice at JS, so I have no idea how to get around this…
Anyone have any thoughts?
Scott;
It seems to me that you should be able to accomplish what you are after without any custom Javascript.
1. Instead of using Model Actions to prepopulate your values on “initial row creation” you could add a global action to the table which uses actions to create the new row and add default values. This will isolate that prepopulation to a specific action and not trigger it simply with any row creation in the model.
2. The Clone action can also be an action - in this case probably a row action that first changes the value in the existing row, and then adds a new row with the default values from the current row.
Having the two actions more explicitly separated like this will most likely help with the collisions you were experiencing.
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.