Search code examples
javascriptdojodojox.grid.datagrid

Dojo Parser assigning arrays of values rather than value


I was trying to iterate through the selected values of the dojo datagrid using the following code and getting the selected rows and sending it to the Spring MVC so that i can map to a List of Objects , but dojo is sending all the values in an array object ,can somebpdy let me know whether iam doing anthing wrong

Getting the selected values from the grid:

 function getUpdatedRowsFromDataGrid(){
     var items = grid.selection.getSelected();   
     window.arr = [];  
        if(items.length) {
                 // Iterate through the list of selected items.
            // The current item is available in the variable
            // "selectedItem" within the following function:
            var spocUpdatedValues = [];
            dojo.forEach(items, function(selectedItem){
                if(selectedItem !== null){
                    // Iterate through the list of attributes of each item.
                    // The current attribute is available in the variable
                    // "attribute" within the following function:
                      var obj = new Object();
                    dojo.forEach(grid.store.getAttributes(selectedItem), function(attribute){
                        // Get the value of the current attribute:
                        var value = grid.store.getValues(selectedItem, attribute);                      
                        // Now, you can do something with this attribute/value pair.
                        // Our short example shows the attribute together
                        // with the value in an alert box, but we are sure, that
                        // you'll find a more ambitious usage in your own code:
                        switch (attribute)
                        {
                           case "spocId":
                                 obj.spocId= value;
                                  break;  
                           case "spocEmployeeFirstName":
                                 obj.spocEmployeeFirstNameValue = value;
                                   break;
                           case "spocEmployeeLastName":
                                 obj.spocEmployeeLastNameValue = value;
                                  break;                   
                            case "spocPhoneNmbr":
                                obj.spocPhoneNmbrValue = value;
                                  break;
                            case "spocStatusCD":
                                 obj.spocStatusCDValue = value;
                                  break;
                            case "spocLocationCD":
                                obj.spocLocationCDValue = value;
                                  break;
                            default: 
                               break;
                        }

                        //obj = {"spocId":spocValue,"spocEmployeeFirstName":spocEmployeeFirstNameValue,"spocEmployeeLastName":spocEmployeeLastNameValue,"spocPhoneNmbr":spocPhoneNmbrValue,"spocStatusCD":spocStatusCDValue,"spocLocationCD":spocLocationCDValue}
//                         alert('attribute: ' + attribute + ', value: ' + value);

                    });  
                     spocUpdatedValues.push({"spocId": obj.spocId,"spocEmployeeFirstNameValue": obj.spocEmployeeFirstNameValue,"spocEmployeeLastNameValue":obj.spocEmployeeLastNameValue,"spocPhoneNmbrValue": obj.spocPhoneNmbrValue,"spocStatusCDValue":  obj.spocStatusCDValue,"spocLocationCDValue": obj.spocLocationCDValue}) 

                } // end if
            })
        }  
    return spocUpdatedValues;
 }  

Sending it to UI

function sendText(){
        my_form=document.createElement('FORM');
        my_form.name='myForm';
        my_form.method='POST';
        my_form.action='/mmworkloadmanager/wlm/updatespoc.html';

        my_tb=document.createElement('INPUT');
        my_tb.type='HIDDEN';
        my_tb.name='spocUpdatedValues';
        my_tb.value=dojo.toJson(getUpdatedRowsFromDataGrid());

        my_form.appendChild(my_tb);

        document.body.appendChild(my_form);

        my_form.submit();
}
o/p 

[{"spocId":[60],"spocEmployeeFirstNameValue":["Joy                      "],"spocEmployeeLastNameValue":["M                                  "],"spocPhoneNmbrValue":["9727664994"],"spocStatusCDValue":["A"],"spocLocationCDValue":["50  "]},{"spocId":[61],"spocEmployeeFirstNameValue":["PJ                       "],"spocEmployeeLastNameValue":["J                                  "],"spocPhoneNmbrValue":["9729969171"],"spocStatusCDValue":["C"],"spocLocationCDValue":["56  "]}]

sending every value as an array , can some body suggest whether i need to modify any part of code to avoid the array.

Solution

  • I think the problem here is that you are putting an array in the value variable.

    Because grid.store.getValues(selectedItem, attribute) will return a value but as an array so you whould have to parse this value as a string instead, try the following:

    var value = grid.store.getValues(selectedItem, attribute)[0];
    

    Or :

    Use the JavaScript Array join() Method like this:

     var value = grid.store.getValues(selectedItem, attribute).join();
    

    I think that should do it.