Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By ekaboom
#788193
What's the best way to get javascript to apply to all rows?

So here's my example of setting a grid textbox from a variable (not sure if this is the best way to do it but it seems to work.
$("#gridVar001").setValue($("#country").getValue(), 1, 1);
What's the best way to change the row "1" so it applies to all of the rows added and not just the first one?

Thanks
By zainab
#788195
One way is to use the below Javascript code:
Code: Select all
$("#gridVar001").setValue($("#country").getValue(), 1, 1);

$("#gridVar001").onAddRow(function(row, grid, index) {
  var totalRows = $("#gridVar001").getNumberRows();
  for (var i = 2; i <= totalRows; i++) {
     $("#gridVar001").setValue($("#country").getValue(), i, 1);
   
    }  
});
By default the grid always has one row in dynaform, therefore for the first row set the value and then call the function upon adding more rows.
By ekaboom
#788201
That worked Great! Thanks so much.

Quick follow up question. I changed your code so it wouldn't overwrite any changes in the rows every time you added a new one:
Code: Select all
$("#gridVar001").setValue($("#country").getValue(), 1, 1);

$("#gridVar001").onAddRow(function(row, grid, index) {
  var totalRows = $("#gridVar001").getNumberRows();
  for (var i = 2; i <= totalRows; i++) {
    if ($("#gridVar001").getValue(i, 1) == ""){
     $("#gridVar001").setValue($("#country").getValue(), i, 1);
    }  
  }
});

The only issue with this copy is that this script still overwrites the first row field, even if it it already has something else saved. Any Suggestion for a clean way to make it so that the first row value can be checked to see if it's blank before setting it to #country so if it has data already filled in, that it won't get overwritten? I tried multiple things but none of them seemed to work.
By ekaboom
#788204
Ok, I think I got it to work but if anyone has any suggestions for improvements I'd love to hear them.
Code: Select all
if ($("#gridVar001").getValue(1, 1) == ""){  
    $("#gridVar001").setValue($("#country").getValue(), 1, 1);
    };

$("#gridVar001").onAddRow(function(row, grid, index) {
  var totalRows = $("#gridVar001").getNumberRows();
  for (var i = 2; i <= totalRows; i++) {
    if ($("#gridVar001").getValue(i, 1) == ""){
     $("#gridVar001").setValue($("#country").getValue(), i, 1);
    }  
  }
});
Thanks Again
By zainab
#788224
Your method looks great!

Another way of assigning the value on adding a new row without affecting the previous values is to use the following piece of code:
Code: Select all
$("#grid1").setValue($("#drop").getValue(), 1, 1);
    
var i = 2;

$("#grid1").onAddRow(function(row, grid, index) {
 
     $("#grid1").setValue($("#drop").getValue(), i, 1);
     i = i + 1;
  
});
Simply remove the for loop, only set the values for the current row.
By ekaboom
#788280
Yes, I love your new code. It's much cleaner!
Code: Select all
if ($("#gridVar001").getValue(1, 1) == ""){  
    $("#gridVar001").setValue($("#country").getValue(), 1, 1);
    };

var i = 2;
$("#gridVar001").onAddRow(function(row, grid, index) {
     $("#gridVar001").setValue($("#country").getValue(), i, 1);
     i = i + 1;
});
I've taken your new code and combined it with the code that checks to see if the first row of data has anything in it first.

In the rapidly evolving world of online sports be[…]

STEPN integrates social networking and games that […]

Cenforce 150 is a medication used to cope with a c[…]

What's SAP FICO?

Trustworthy and skill-building, each of these actu[…]