Page 1 of 1
How to remove blank grid rows
Posted: Fri Sep 13, 2019 5:59 pm
by Buomque
Hi,
Is there a way to use Javascripts to remove blank grid rows or remove grid rows based on value of a grid dropdown?
Thank you,
Re: How to remove blank grid rows
Posted: Fri Sep 13, 2019 9:03 pm
by amosbatto
You can use
grid.removeRow().
If you want to remove blank rows when submitting the Dynaform, add this JavaScript code to your Dynaform:
Code: Select all//set to the column number of the grid field to check if blank; counting starts counting from 0:
var colNoCheckIfBlank = 3;
var formId = $("form").prop("id");
$("#"+formId).setOnSubmit( function() {
var oGrid = $("#myGrid"); //change "myGrid" to the grid's ID
var aGridVals = oGrid.getValue();
var aRowsToDelete = [];
for (i = aGridVals.length; i > 0; i--) {
//subtract 1 because array counts from 0, not from 1
if (aGridVals[i-1][colNoCheckIfBlank] == "") {
oGrid.deleteRow(i);
}
}
});
Re: How to remove blank grid rows
Posted: Fri Sep 20, 2019 4:12 pm
by Buomque
Code works like a charm.
Thank you Amos!
Re: How to remove blank grid rows
Posted: Thu Apr 09, 2020 1:28 pm
by Iykedigit
Hi. I've a grid that is populated when I select the desired number of rows I want from a drop down. However, when the forum moves to the next task, the data in the last row in the grid is cleared. So if for instance, I select that I want 3 rows, when I proceed to the next task the third row is empty. Plz help. See code below.
function gridsome(){
//get the no of accounts required
var accountNumber = $("#no_of_accounts").getValue();
var accountNumber = accountNumber + 1;
var gridRow = $("#accountDetailsGrid").getNumberRows() + 1;
if (accountNumber > gridRow){
for (var i = gridRow; i < accountNumber; i++){
$("#accountDetailsGrid").addRow();
}
}else{
for (var i = gridRow; i > accountNumber; i--){
$("#accountDetailsGrid").deleteRow();
}
}
}
$("#no_of_accounts").setOnchange(gridsome);