Dataprocessor problen when insert

I am adding a row in the grid with default data. User changes the default data. User selects another row. This results in server side call for insert operation. I am using dataprocessor. But the problem is for the same row the it is making insert opertion twice. Which results in error. Please find the code.



Please resolve this at earliest.



file dhtmlx.html





    

        dhtmlx test page

        



        

        

        

        

        

        

        

        

        

        

        

        

            

        

        

        

        

        

        

        

        

        

    

        

        

        

        

        

        

        

        

        

        

        



.even{

background-color:#E6E6FA;

}

.uneven{

background-color:#F0F8FF;

}









    

    



        

            

        

        

            

        

        

            

        

    


                


            




        


        



        


        

        

            

            

        

        
Choose skin to apply:

                

                    gray

                    mt

                    xp

                    clear

                    modern

                    light

                

            


                <input type=“Button” name=“resizeColumns” onclick=“resizeColumns()” value=“ResizeColumns”

                    title=“ResizeColumns” />

        


    


    


        

        

        

            

        

        


                Add Row at the top

            


                Delete Row by index

            




    


    


    <input type=“Button” name=“exportToCsv” onclick=“exportToCsv(‘csvTextArea’)” value=“exportToCsv”

        title=“exportToCsv” />


    

    


    







dhtmlx.js file



// global varables

var mygrid = null;

function intializeTheGrid() {

    dhtmlxError.catchError(“ALL”, error_handler);

    mygrid = new dhtmlXGridObject(“gridbox”);

    mygrid.setImagePath(“images/dhtmlxgrid/”);

    mygrid.setOnLoadingEnd(on_load_end);

    mygrid.init();

    

    //mygrid.preventIECaching(true);

}

function loadData() {

    mygrid.clearAll();

    mygrid.enableAlterCss(“even”, “uneven”);

    //mygrid.enableSmartRendering(true);

    mygrid.enablePaging(true, 10, 4, “pagingArea”, true, “recinfoArea”);

    mygrid.enableColumnAutoSize(true);

    //mygrid.enablePagingWT(true,10,null,“recinfoArea”);

    eventsCode(mygrid);

    var baseUrl = “http://localhost:10080/haviNonReceipe/”;

    var requestString1 = baseUrl + “drawQuantities.htm?action=listDrawQuantitiesAutoLoading” + “&uniqueParam=” + Date.parse(new Date());

    mygrid.setXMLAutoLoading(requestString1, 10);

    //mygrid.splitAt(2);

    var requestString = baseUrl + “drawQuantities.htm?action=listDrawQuantities” + “&uniqueParam=” + Date.parse(new Date());

    mygrid.loadXML(requestString, function () {

        //var totalCount = mygrid.getUserData("",“totalCount”);

        //alert(“totalCount:” + totalCount);

        //alert(“hi”);

        //mygrid.setSizes();

        

        resizeColumns();

    });

    

    setTheDataProcessor(mygrid, baseUrl);

    //mygrid.setOnCheckHandler(OnCheckHandler);

    //mygrid.setOnEditCellHandler(doOnCellEdit);

}



function insertRow(id){

    var myArray = new Array(mygrid.getColumnCount());

//100,CNR HEATHERTON & SPRINGVALE ROADS,Y,01/01/2006,N,Y,Robert Michalak,

//16/09/2006,30,02/07/2006,Y,N,Robert Michalak,14/06/2006,99,14/06/2006,Pre-determined,Y

    myArray[0] = id;

    myArray[1] = “CNR HEATHERTON & SPRINGVALE ROADS”;

    myArray[2] = “Y”;

    myArray[3] = “01/01/2006”;

    myArray[4] = “N”;

    myArray[5] = “Y”;

    myArray[6] = “Robert Michalak”;

    myArray[7] = “16/09/2006”;

    myArray[8] = 30;

    myArray[9] = “02/07/2006”;

    myArray[10] = “Y”;

    myArray[11] = “N”;

    myArray[12] = “Robert Michalak”;

    myArray[13] = “14/06/2006”;

    myArray[14] = 99;

    myArray[15] = “14/06/2006”;

    myArray[16] = “Pre-determined”;

    myArray[17] = false;

    

    mygrid.addRow(id, myArray, 0);

}

function setTheDataProcessor(grid, baseUrl){

    myDataProcessor = new dataProcessor(baseUrl + “drawQuantities.htm?action=dataProcessing”);

//use column IDs instead of column indexes naming request parameters

myDataProcessor.enableDataNames(true);

//verify if the value of 2nd column (zero-based numbering is 1) is not empty

// myDataProcessor.setVerificator(1)

//verify value of 4th column (zero-based numbering is 3) against checkIfNotZero verification function (see it below)

// myDataProcessor.setVerificator(3,checkIfNotZero)

//you can also specify update mode. Availabel modes are: update automaticaly (cell based, row based) and manual update

//cell (“cell”) based mode means that grid will attempt to update data on server after each editing of cell (on editor closed).

//row (“row”) based mode means that grid will attempt to update data on server when row selection changed or Enter key pressed

//manual (“off”) means that you need to run myDataProcessor.sendData() to begin update process (automatic update off).

myDataProcessor.setUpdateMode(“row”);//available values: cell (default), row, off

//set error handler (handler for action with type “error”)

myDataProcessor.defineAction(“update”, function my_handler(xml_node){

                                         alert(xml_node.getAttribute(“tid”));

                                     return true;

                                            }

                                );

     myDataProcessor.defineAction(“insert”, function my_handler(xml_node){

                                             //alert(xml_node.data);

                                         //alert(xml_node.getAttribute(“tid”));

                                         //alert(xml_node.getAttribute(“sid”));

                                         try{

                                         var oldRowId = xml_node.getAttribute(“sid”);

                                         var newRowId = xml_node.getAttribute(“tid”);

                                         var restId = mygrid.cells(oldRowId,0);

                                         restId.setValue(newRowId);

                                         }catch(e){ }

                                         //alert(mygrid.getRowId(xml_node.getAttribute(“tid”)));

                                     return true;

                                            }

                                );

     myDataProcessor.defineAction(“delete”, function my_handler(xml_node){

                                         alert(xml_node.getAttribute(“tid”));

                                     return true;

                                            }

                                );

myDataProcessor.defineAction(“error”,myErrorHandler);

//specify transaction method - POST or GET (default is GET)

myDataProcessor.setTransactionMode(“POST”);

//initialize data processor for the grid object (in our case - mygrid)

myDataProcessor.init(mygrid);

}



//============================================================================================

//Example of error handler. It gets tag object as incomming argument.

function myErrorHandler(obj){

alert(“Error occured.\n”+obj.firstChild.nodeValue);

myDataProcessor.stopOnError = true;

return false;

}



//Example of verification function. It verifies that value is not 0 (zero).

//If verification failed it should return false otherwise true.

//Verification fucntion specified in setVerificator method will always get two argumentrs: value to verify and column name (use it for message)

function checkIfNotZero(value,colName){

if(value.toString()._dhx_trim()==“0”){

showMessage(colName+ " should not be 0")

return false

}else

return true;

}



function resizeColumns(){    

    try{

    var columnCount = mygrid.getColumnCount();

    }catch(e){

        //alert(e.message);

    }

    for(var i = 0; i < columnCount ; i++){

        mygrid.adjustColumnSize(i);

    }

}

function on_load_end(grid) {

//alert(“Portion sucesfully loaded”);

}

function error_handler(type, name, param) {

    alert(“Seems that somehow next error occurs \n” + type + " : " + name + " : " + param);

}

function exportToCsv(textAreaID) {

    mygrid.setCSVDelimiter(" | ");    //chnage csv delimiter

    var csvNew = mygrid.serializeToCSV();

    var textArea = document.getElementById(textAreaID);

    textArea.value = csvNew;

}

function doOnCheck(rowId, cellInd, state) {

    // Event raises after state was changed. for checkbox and radio button cell

    alert("doOnCheck: " + rowId + “:” + cellInd + “:” + state);

    return true;

}

function doOnBeforeRowDeleted(rowId) {

    // Event raised right before row deleted (if returns false, deletion canceled)

    alert("doOnBeforeRowDeleted: " + rowId);

    return true;

}

function doOnRowAdded(rowId) {

    // Event raised right after row was added to grid

    alert("doOnRowAdded: " + rowId);

    return true;

}

function doOnEnter(rowId, cellInd) {

// Event raised immideatly after Enter pressed.

    alert("doOnEnter: " + rowId + “:” + cellInd);

    return true;

}

function doOnCellEdit(stage, rowId, cellInd, newvalue, oldValue) {

    // new value and old value only for stage 2

    // Event raises 1-3 times depending on cell type and settings

    alert("doOnCellEdit: " + stage + “:” + rowId + “:” + cellInd + “:” + newvalue + “:” + oldValue);

    if (stage == 0) { // before start

        return true;

    } else {

        if (stage == 1) { //1-editor opened

        } else {

            if (stage == 2) { //2-editor closed

            }

        }

    }

    return true;

}

function doOnCellChanged(rowID, cellInd, newvalue) {

    alert("doOnCellChanged: " + rowID + “:” + cellInd + “:” + newvalue);

    return true;

    // mygrid

}

function doOnScroll(scrollLeft, scrollTop) {

    //Event raised immideatly after scrolling occured

    alert("doOnScroll: " + scrollLeft + “:” + scrollTop);

    return true;

}

function doOnPageChanged(myCurrentPage, myStartRowInd, myPage) {

    alert("doOnPageChanged: " + myCurrentPage + “:” + myStartRowInd + " : " + myPage);

    return true;

}

function doOnRowSelect(rowID, cellInd) {

    //Event raised immideatly after row was clicked.

    alert("doOnRowSelect: " + rowID + “:” + cellInd);

    return true;

}

function doOnBeforeSelect(newSelectedRow, oldSelectedRow) {

    //event fired moment before row in grid get selection.

    alert("doOnBeforeSelect: " + newSelectedRow + “:” + oldSelectedRow);

    return true;

}

function doOnRowCreated(rowId, rowObject, relatedXml) {

    // event fired after row created in grid, and filled with data

    alert("doOnRowCreated: " + rowId + “:” + rowObject + " : " + relatedXml);

    return true;

}

function doOnBeforeSorting(colIndex, gridObject, sortingDirection) {

    // event called before sorting of data started

    alert("doOnBeforeSorting: " + colIndex + “:” + gridObject + " : " + sortingDirection);

    return true;

}

function eventsCode(grid) {

    //grid.setOnRowSelectHandler(doOnRowSelect);

    //grid.setOnEditCellHandler(doOnCellEdit);

    //grid.setOnEnterPressedHandler(doOnEnter);

    //grid.setOnCheckHandler(doOnCheck);

    //grid.setOnPageChanged(doOnPageChanged);

    //grid.setOnBeforeRowDeletedHandler(doOnBeforeRowDeleted); // set function called before row removed from grid

    //grid.setOnRowAddedHandler(doOnRowAdded); //set function called after row added to grid

    //grid.setOnBeforeSelect(doOnBeforeSelect); // set function called moment before row selected in grid

    //grid.setOnRowCreated(doOnRowCreated); // set function called after row created

    //grid.setOnCellChanged(doOnCellChanged); // set function called after value of cell changed by user actions

    //grid.setOnColumnSort(doOnBeforeSorting); // set function called before sorting of data started, didn’t occur while calling grid.sortRows

}



first time request for data

GET /haviNonReceipe/drawQuantities.htm?action=listDrawQuantities&uniqueParam=1196875038000&rowsLoaded=0&lastid=null&a_dhx_rSeed=1196875038557 HTTP/1.1

Accept: /

Accept-Language: en-us

Referer: localhost:8080/haviNonReceipe/dhtmlx.html

Content-Type: application/x-www-form-urlencoded

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)

Host: localhost:8080

Content-Length: 463

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: JSESSIONID=3EC699591578307980B6C6D774AFA2E5



gr_id=10000&restId=10000&restDescription=CNR%20HEATHERTON%20%26amp%3B%20SPRINGVALE%20ROADS&eligible=Y&eligibilityDate=01/01/2006&pmixViaPoller=N&jdeDC=Y&ropPlanner=Robert%20Michalak&dateOfCashlessReceipts=16/09/2006&noOfDaysPmixHist=30&dateOfSomeAttribute=02/07/2006&someAttribute=Y&anotherAttribute=N&dcPeople=Robert%20Michalak&someDate=14/06/2006&noOfDaysReceipts=99&someDate2=14/06/2006&someSelectionList=Pre-determined&complete=0&!nativeeditor_status=inserted



response:

HTTP/1.1 200 OK

Content-Type: text/xml;charset=UTF-8

Content-Length: 58

Date: Wed, 05 Dec 2007 17:17:24 GMT

Server: Apache-Coyote/1.1





You source code contains one potencial problematic place which can cause issue

function loadData() {
    … 
    setTheDataProcessor(mygrid, baseUrl);
}

function setTheDataProcessor(grid, baseUrl){
    …
    myDataProcessor.init(mygrid);
}

There is possiblity that loadData function called twice and in result two instances of dataProcessor will be attached to the same grid, which will result in sending two “insert” actions instead of one.
Please be sure that only one instance of dataProcessor attached to grid.