AnsweredAssumed Answered

dGrid, sorting Memory vs. sorting Grid

Question asked by schlot on Jul 2, 2014
Latest reply on Jul 3, 2014 by schlot
I have a grid I'm creating that needed to be sorted, and also be able to save the grid to a CSV file and retain the sort I have on the grid.  The grid is created  in the results handler of queryTask.  At this point, I simply run sort on the field I want and the grid looks fine.  But that's a sort on the grid, not on the underlying data.

  grid = new (declare([Grid, ColumnHider, DijitRegistry, ColumnResizer]))({             id:"myGrid',              columns: gridcolumns,             store: currentMemory         });         grid.startup();         grid.sort('NAME');


Next I have a button that will allow the user to save this information to a CSV.  It's getting created into the same container the grid is, so I can pass the argument of the grid name to it. 

        var saveButton = new Button({         label: "Save List",         onClick: function(){        saveGridCSV(grid);         }     }, "btnSave");      registry.byId("gridContainer").addChild(saveButton);       registry.byId("gridContainer").addChild(grid); 


Within my saveGridCSV, I'm going back to the store of the grid and using it to get the values formatted for the CSV. 
//functions for saving to Excel   function saveGridCSV(gridData){     var fName;      if (gridData) {       var gridLength = gridData.store.data.length;       dataArray.length = 0;        fieldNames.length = 0;       var fieldValue = "";          var gridColumns = gridData.columns;       var fieldHeaders = ["ID", "County", "Acres Treated", "Pct Area Treated", "Pct Change 2002-2007"];       for (var col in gridColumns) {        fieldNames.push(col);       }   var lastCol = fieldNames.length -1;   var lastField = fieldNames[lastCol];   dataArray.push(fieldHeaders.toString()+ " \n");  //  dataArray.push(fieldNames.toString()+ " \n");       var gridStore = gridData.store;     //steps through each record, pushes the values into an array that is formatted for CSV compatibility         for (var i = 0; i < gridLength; i++) {             var gridRow = gridStore.data[i];             arrayUtils.forEach(fieldNames, function(fieldName){                 fieldValue = gridRow[fieldName];                 var stringValue = String(fieldValue);                 if (stringValue.indexOf(",") > 0) { //removes any commas from data                                         stringValue = stringValue.replace(/,/g, " ");                 }                 if (stringValue.indexOf('\'') != -1 || stringValue.indexOf('\"') != -1) { //removes any slashes from data                     //  console.log ("Data has a slash in it. Not sure of this section of the code!");                     if (stringValue.indexOf('\"') != -1) {                         stringValue = stringValue.replace("\"", "\"\"");                     }                     stringValue = "\"" + stringValue + "\"";                 }                 if (fieldName == lastField) {                     dataArray.push(stringValue + " \n");//adds a new line when value is from the last field                 } else {                     dataArray.push(stringValue);//otherwise just adds the value                 }             });         }//one row pushed to array              var data = dataArray.join();         var inputData = data.replace(/\n,/g, "\n");//some clean up to get rid of the leading commas in the data               submitCSVprint(gridData.id, inputData);     } else {  //nothing to save        alert ("Error saving data.");     }   }


This is working well, except that I still want the data sorted on my NAME field and now it's not.  The sorting I performed early was on the grid, not on the underlying Memory.

My question is, how do I sort the Memory instead so the CSV output matches what the user sees in the grid?

Outcomes