Capturing Field Update Event for a JSP

Hi,
I am working on a project where I need to do some task if any field is updated on a jsp.
One way could be take a hidden variable for every field which stores the previous value and then by comparing the previous value and the current value I can know if any field has been updated or not.
But keeping track of 20 fields on JSP like this is not neat. Also a part of JSP displays some fields that are displayed dynamically i.e. they come through a query .The number of rows returned by Select query determines the number of fields rendered on page,So when the page renders I will not know how many fields are there on JSP.

Other way that i thought of was

when the page has rendered create change event for every VISIBLE INPUT element except button in the DOM using JQUERY and if any one of the change event fires I set a variable which tells that any field has been updated.

Can anyone suggest some other approach that I can use?
Note that I need to do something in java if ANY field on the jsp is updated.

Thanks,
Ankur

Replies

  • ankur8819
    ankur8819
    I was able to do it .Pasting the Js code below just for reference.
    Steps :-
    Call the saveInitialValues() function in document.ready() function.This will return an array containing values of all input variables other than buttons,hidden etc.

    On Post of form call saveFinalValues() function It will create another array containing values of all input variables other than buttons,hidden etc on jsp.

    Then call checkfieldupdated().This function returns 1 if any change has been made on JSP and 0 if not.

    Let me know if anyone has issue understanding the logic below.


    var initialValues;
    var finalValues

    function compareArrays(obj1,obj2)
    {
    if(obj1==null)
    obj1len=0;
    else
    obj1len=obj1.length ;

    if(obj2==null)
    obj2len=0;
    else
    obj2len=obj2.length
    if( obj1len==0 && obj2len==0){return true;}
    if (obj1len != obj2len) { return false; }
    var a = obj1.sort(),
    b = obj2.sort();

    for (var i = 0;i< b.length; i++) {
    if (a !== b) {
    return false;
    }
    }
    return true;
    }

    function getAllInputFields(form) {
    var tempArray=new Array();
    var allfields=$('#'+form+' :input:not😀hidden,:submit,😛assword,:button)');
    allfields.each(function(){


    if($(this).get(0).type=='select-one')
    {
    tempArray.push($(this).val());
    }
    else if($(this).get(0).type=='select-multiple')
    {
    for(var j=0;j {
    $(this).get(0).options[j].selected=true;
    }

    tempArray.push($(this).val());
    }
    else if($(this).get(0).type=='radio')
    {
    if($(this).is(':checked'))
    tempArray.push($(this).val());
    }
    else
    {
    tempArray.push($(this).val());
    }

    });
    return tempArray;

    }


    function saveInitialValues()
    {
    initialValues=getAllInputFields('WISCTaskInstanceDetailsForm');


    }


    function saveFinalValues()
    {
    finalValues=getAllInputFields('WISCTaskInstanceDetailsForm');
    }


    function checkfieldupdated()
    {
    if(initialValues.length!=finalValues.length)
    return 1;
    else
    {
    for(var i =0 ;i< initialValues.length;i++)
    {

    if(typeof initialValues=='string')
    {
    if(initialValues!=finalValues)
    {
    return 1;
    }
    }
    else if(typeof initialValues=='object')
    {
    if(!compareArrays(initialValues,finalValues))
    {
    //alert('Field Updated');
    return 1;
    }
    }

    }
    }
    return 0;
    }
  • ankur8819
    ankur8819
    the smiley is actually "colon"password
    ankur8819
    I was able to do it .Pasting the Js code below just for reference.
    Steps :-
    Call the saveInitialValues() function in document.ready() function.This will return an array containing values of all input variables other than buttons,hidden etc.

    On Post of form call saveFinalValues() function It will create another array containing values of all input variables other than buttons,hidden etc on jsp.

    Then call checkfieldupdated().This function returns 1 if any change has been made on JSP and 0 if not.

    Let me know if anyone has issue understanding the logic below.


    var initialValues;
    var finalValues

    function compareArrays(obj1,obj2)
    {
    if(obj1==null)
    obj1len=0;
    else
    obj1len=obj1.length ;

    if(obj2==null)
    obj2len=0;
    else
    obj2len=obj2.length
    if( obj1len==0 && obj2len==0){return true;}
    if (obj1len != obj2len) { return false; }
    var a = obj1.sort(),
    b = obj2.sort();

    for (var i = 0;i< b.length; i++) {
    if (a !== b) {
    return false;
    }
    }
    return true;
    }

    function getAllInputFields(form) {
    var tempArray=new Array();
    var allfields=$('#'+form+' :input:not😀hidden,:submit,😛assword,:button)');
    allfields.each(function(){


    if($(this).get(0).type=='select-one')
    {
    tempArray.push($(this).val());
    }
    else if($(this).get(0).type=='select-multiple')
    {
    for(var j=0;j {
    $(this).get(0).options[j].selected=true;
    }

    tempArray.push($(this).val());
    }
    else if($(this).get(0).type=='radio')
    {
    if($(this).is(':checked'))
    tempArray.push($(this).val());
    }
    else
    {
    tempArray.push($(this).val());
    }

    });
    return tempArray;

    }


    function saveInitialValues()
    {
    initialValues=getAllInputFields('WISCTaskInstanceDetailsForm');


    }


    function saveFinalValues()
    {
    finalValues=getAllInputFields('WISCTaskInstanceDetailsForm');
    }


    function checkfieldupdated()
    {
    if(initialValues.length!=finalValues.length)
    return 1;
    else
    {
    for(var i =0 ;i< initialValues.length;i++)
    {

    if(typeof initialValues=='string')
    {
    if(initialValues!=finalValues)
    {
    return 1;
    }
    }
    else if(typeof initialValues=='object')
    {
    if(!compareArrays(initialValues,finalValues))
    {
    //alert('Field Updated');
    return 1;
    }
    }

    }
    }
    return 0;
    }

You are reading an archived discussion.

Related Posts

I'm have Sennheiser HD 201 headphones and I rarely use them; because most of the times [sarcasm] our server plays the music for me (and makes me dance) [/sarcasm]. But...
This is a story shared by CEan Praveenscience aka Praveen Kumar. One day in school [h=6]in London, a teacher said to a class of 5-year-olds… I'll give 10 pounds to...
1. Crazy Engineer's Forum attained PR5 from PR3, in Next update the domain itself is scheduled for PR6 2. Crazy Engineer's Facebook fan page also gets to PR5 3. Grasshopper...
https://www.theengineer.co.uk/Journals/1/Files/2011/11/1/TE_2011_SHORTLIST.pdf?cmpid=TE01
A more holistic approach may be needed to decide on large scale biofuel use. APJ Kalam wanted to promote Jatropha as a source for biodiesel. The great advantage of this...