2

I use dojo 1.9 in my aplication. When i pass the array (aID) to the xhr.post data, the array data will change and will convert in array, example:

aid = [3,4]

and xhr.post send aid =[[3],[4]]

why? I do not understand why this happens.

 // Create the grid
grid = new EnhancedGrid({
        id: 'grid',
        store: Mystore,
        structure: layout,
        //selectionMode: multiple,
        //keepSelection: true,
        escapeHTMLInData: false,
        width: '100%',
        autoHeight: true,
        rowSelector: gridLayout,
        noDataMessage: '<span class=\"dojoxGridNoData\">No Devices</span>',
        plugins: {
        pagination: {
        pageSizes: [],
        description: true,
        sizeSwitch: true,
        pageStepper: true,
        gotoButton: true,
        /*page step to be displayed*/
        maxPageStep: 4,
        /*position of the pagination bar*/
        position: "bottom"
        },
        indirectSelection: {headerSelector:true, width:gridLayout, styles:"text-align: center;"}
        }
        })
        grid.placeAt('gridContainer');
        grid.startup();

var button4 = new Button({ label:"Delete device"});
            //button4.startup();
            button4.placeAt("buttonD");
            button4.on("click", function(event) {
            var aID =[];
            var items = grid.selection.getSelected();
            dojo.forEach(items, function(selectedItem){                      
aID.push(grid.store.getValues(selectedItem,'id'));                                                             
                        });

            console.info(aID);
            xhr.post("/****/***/***/***/action",{
                            sync: true,
                            data: dojo.toJson({
                            action: 255,
                            targets: aID
                            }),
                            headers: { 'Content-Type': 'application/json','x-ds-session': cookie("token")},
                            handleAs: "json"
                            }).then(function(data1){


                            },function(err){
                                    if( (err.response.text).indexOf("Invalid session token") != -1 ){
                                                                    window.location.reload(true);
                            }
                            });
                        });
sona
  • 1,552
  • 3
  • 18
  • 37

1 Answers1

1

It is not a problem with xhr.post. In your code

dojo.forEach(items, function(selectedItem){                      
        aID.push(grid.store.getValues(selectedItem,'id'));                                                             
    });

you are using grid.store.getValues (note - plural) which returns an array. I think you need to be using grid.store.getValue or if you must use getValues to return multiple values, use grid.store.getValues(selectedItem,'id')[0]

Hrishikesh Kumar
  • 356
  • 2
  • 12
  • You could also use something like `aID.push.apply(aID, grid.store.getValues(selectedItem, 'id'));` if you want to add all values in the store to the array you're `POST`ing. See http://stackoverflow.com/questions/1374126/how-to-append-an-array-to-an-existing-javascript-array. – Thomas Upton Jul 23 '14 at 22:39