Page 1 of 1

setOnchange on form, issue with remove row

Posted: Fri Jun 21, 2019 5:31 am
by flattante
Hi all,
I've a strange behavoir with this piece of code (is an example, in production it doesn't show an alert but it set other values for other fields on the same row):
Code: Select all
$("form").setOnchange(modifiedForm);
function modifiedForm(fieldId, newVal, oldVal){
    console.log("fieldId: " + fieldId + " - newVal: " + newVal + " - oldVal: " + oldVal);
    if (oldVal != ''){
        rowID = (fieldId.substring(16,fieldId.length)).substring(0,(fieldId.substring(16,fieldId.length)).indexOf(']'));
        fieldTemp = ((fieldId.substring(16,fieldId.length)).substring(fieldId.substring(16,fieldId.length).indexOf('[')+1));
        field = fieldTemp.substring(0,fieldTemp.length-1);

        if (newVal!=oldVal){
			if (newVal > 500){
				alert("New value>500!");
			}
        }
    }
}
In brief, it works fine when I make changes but when I remove a row the content of fieldId contain the old index row.
Anyone can help me to understand why and how I can resolve it?!?

Thanks!

Re: setOnchange on form, issue with remove row

Posted: Fri Jun 21, 2019 5:33 am
by flattante
Ah, If I try this code inside the setonchange:
Code: Select all
var totalRows = $("#dati_prodotti").getNumberRows();
the totalRows show me the exact number of the rows but I need the specified modified row.

Thanks

Re: setOnchange on form, issue with remove row

Posted: Tue Jun 25, 2019 2:49 am
by amosbatto
I don't understand your question.

Is this what you want to do?
Code: Select all
$("form").setOnchange(modifiedForm);
function modifiedForm(fieldId, newVal, oldVal) {
   //check if changing a particular field in the grid.
   //change myGrid and myField to the IDs of your grid and your grid field.
   var aMatch = fieldId.match(/^\[myGrid\]\[(\d+)\]\[myField\]$/);
   
   if (aMatch && oldVal != '') {
        var rowNo = aMatch[1];

        if (newVal != oldVal) {
             var newValue = parseInt(newVal);

	     if (newValue > 500) {
		 alert("New value in row "+rowNo+" is over 500. Setting to maximum of 500");
                 $("#myGrid").setValue(500, rowNo, 3); //change 3 to the column number of grid that you want to change
	     }
        }
    }
}

Re: setOnchange on form, issue with remove row

Posted: Tue Jun 25, 2019 5:01 am
by flattante
Thanks for your reply,
no, the problem is that if you add more than one row and you exec the code you will have, modifying the 5th row, rowNo=5.
If you delete one row (from 1 to 4) and try to modiy the new 2nd row you have the same rowNo=5 when I'm expecting rowNo=2.
I hope I was clear.

Re: setOnchange on form, issue with remove row

Posted: Tue Jun 25, 2019 9:17 pm
by amosbatto
You can try using this:
Code: Select all
var formId = $("form").prop("id");
$("#"+formId).change( function(e) {
  var val = e.target.value;
  var aMatch = e.target.id.match( /^form\[clientList\]\[(\d+)\]\[(\w+)\]$/ );
  
  if (aMatch) {
    var rowNo = aMatch[1];
    var gridFieldId = aMatch[2];
    alert("Field "+gridFieldId+" in row "+rowNo+" was changed to '"+val+"'");
  }
});
Where "clientList" is the ID of the grid.

I don't know how to get the previous value, but this will get you the row number and ID of the field in the grid.