DataStore query

993
5
Jump to solution
06-09-2014 12:36 PM
ChrisHavel
New Contributor
I am updating a grid with the code below using a DataStore.  This data store has a ton of records in it and I do need all those records for another purpose.  BUT I would also like to take this query and do some sort of DISTINCT by the 'Genus' and 'Species' to also pass to a Grid with just the unique Genus  and Species....

Can I grab the return from variable "data" below and get the distinct records?

          function updateGrid(featureSet) {         var data = arrayUtils.map(featureSet.features, function (entry, i) {           return {             GENUS: entry.attributes.GENUS,             id: entry.attributes.OBJECTID,             COMMON_NAME: entry.attributes.COMMON_NAME,             SPECIES: entry.attributes.SPECIES           };         });          // If you use a store...   dataStore = new Memory({       "data": data,       "idProperty": "id"   });   gridNoColumnSets3.set("store", dataStore);         gridNoColumnSets3.startup();      }
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
Here's an example that shows the filtered results of unique surnames and names

<!DOCTYPE html> <html> <head>      <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">      <script>dojoConfig = { async: true, parseOnLoad: true }</script>     <script src="http://js.arcgis.com/3.9/"></script>      <script>         require(["dojo/_base/array", "dojo/_base/lang", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],         function (array, lang, dom, domConst, on) {              var arrSalary = [               { surname: "Washington", name: "Paul", sightings: 200 },               { surname: "Gordon", name: "Amie", sightings: 350 },               { surname: "Gordon", name: "Sofie", sightings: 100 },               { surname: "Jaysons", name: "Josh", sightings: 2500 },               { surname: "Washington", name: "George", sightings: 10 },               { surname: "Jaysons", name: "Amber", sightings: 320 },               { surname: "Jaysons", name: "Amber", sightings: 3200 },               { surname: "Hill", name: "Strawberry", sightings: 290 },               { surname: "Washington", name: "Paul", sightings: 200 },               { surname: "Hill", name: "Master", sightings: 205 }             ];              on(dom.byId("button"), "click", function () {                 var uniqueName = [];                 var raisedSalaries = array.filter(arrSalary, function (item) {                     var IsNameUnique = true;                     array.forEach(uniqueName, function (value) {                         var test = item.surname + item.name;                         if (value == (item.surname + item.name)) {                             IsNameUnique = false;                         }                     });                       if (IsNameUnique) {                         uniqueName.push(item.surname + item.name);                     }                     return IsNameUnique;                 });                  array.forEach(raisedSalaries, function (item, i) {                     var li = domConst.create("li");                     li.innerHTML = i + 1 + ". " + item.surname + ", " + item.name + ". Sightings: " + item.sightings;                     dom.byId("filteredSalary-items").appendChild(li);                 });                  array.forEach(arrSalary, function (item, i) {                     var li = domConst.create("li");                     li.innerHTML = i + 1 + ". " + item.surname + ", " + item.name + ". Sightings: " + item.sightings;                     dom.byId("unFilteredSalary-items").appendChild(li);                 });             });         });     </script> </head> <body class="claro">     <button id="button" type="button">Unique</button>     <br />     <div style="width: 300px; float: left; margin-top: 10px;">         Original list:         <ul id="filteredSalary-items"></ul>     </div>     <div style="width: 300px; float: left; margin-top: 10px;">         Filted list:         <ul id="unFilteredSalary-items"></ul>     </div> </body> </html>

View solution in original post

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor
There isn't any method within the dojo array function to return unique values. Maybe it will get added in 2.0.

You could try other methods, like this or this.
0 Kudos
ChrisHavel
New Contributor
hmmmm....thats reaching my level of knowledge...
So I would grab the variable/array Data and then process that into the new code?


   function updateGrid(featureSet) {
        var data = arrayUtils.map(featureSet.features, function (entry, i) {
          return {
            GENUS: entry.attributes.GENUS,
            id: entry.attributes.OBJECTID,
            COMMON_NAME: entry.attributes.COMMON_NAME,
            SPECIES: entry.attributes.SPECIES
          };
        });

  
               var unique2 = function(data){
  var test = {};
  var result = [];
  for (var i=0,len=arr.length; i < len; i++){
  if (!test[arr]){ // value not seen yet?
  test[arr] = true;
  result.push(arr);
  }
  }
  return result;
  };

        // If you use a store...
  dataStore = new Memory({
      "data": result,
      "idProperty": "id"
  });

 gridNoColumnSets3.set("store", dataStore);
        gridNoColumnSets3.startup();

    }

0 Kudos
KenBuja
MVP Esteemed Contributor
Here's an example that shows the filtered results of unique surnames and names

<!DOCTYPE html> <html> <head>      <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">      <script>dojoConfig = { async: true, parseOnLoad: true }</script>     <script src="http://js.arcgis.com/3.9/"></script>      <script>         require(["dojo/_base/array", "dojo/_base/lang", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],         function (array, lang, dom, domConst, on) {              var arrSalary = [               { surname: "Washington", name: "Paul", sightings: 200 },               { surname: "Gordon", name: "Amie", sightings: 350 },               { surname: "Gordon", name: "Sofie", sightings: 100 },               { surname: "Jaysons", name: "Josh", sightings: 2500 },               { surname: "Washington", name: "George", sightings: 10 },               { surname: "Jaysons", name: "Amber", sightings: 320 },               { surname: "Jaysons", name: "Amber", sightings: 3200 },               { surname: "Hill", name: "Strawberry", sightings: 290 },               { surname: "Washington", name: "Paul", sightings: 200 },               { surname: "Hill", name: "Master", sightings: 205 }             ];              on(dom.byId("button"), "click", function () {                 var uniqueName = [];                 var raisedSalaries = array.filter(arrSalary, function (item) {                     var IsNameUnique = true;                     array.forEach(uniqueName, function (value) {                         var test = item.surname + item.name;                         if (value == (item.surname + item.name)) {                             IsNameUnique = false;                         }                     });                       if (IsNameUnique) {                         uniqueName.push(item.surname + item.name);                     }                     return IsNameUnique;                 });                  array.forEach(raisedSalaries, function (item, i) {                     var li = domConst.create("li");                     li.innerHTML = i + 1 + ". " + item.surname + ", " + item.name + ". Sightings: " + item.sightings;                     dom.byId("filteredSalary-items").appendChild(li);                 });                  array.forEach(arrSalary, function (item, i) {                     var li = domConst.create("li");                     li.innerHTML = i + 1 + ". " + item.surname + ", " + item.name + ". Sightings: " + item.sightings;                     dom.byId("unFilteredSalary-items").appendChild(li);                 });             });         });     </script> </head> <body class="claro">     <button id="button" type="button">Unique</button>     <br />     <div style="width: 300px; float: left; margin-top: 10px;">         Original list:         <ul id="filteredSalary-items"></ul>     </div>     <div style="width: 300px; float: left; margin-top: 10px;">         Filted list:         <ul id="unFilteredSalary-items"></ul>     </div> </body> </html>
0 Kudos
ChrisHavel
New Contributor
Thank you so much Ken....I am sure I can get this going now with all your help...gonna give this a try later as time permits...
0 Kudos
ChrisHavel
New Contributor
Was very confused for a bit because I could not get the Lists to populate while in my application.
I added the below code from another site (function print_r(arr,level)) and was able to confirm that I was in fact getting a return..

I HOPE THIS helps others out on Filtering data until Dojo catches up

CHEERS

SOLUTION:
I created a new DataGrid from the original code
Used KENS bit of code to get the unique records (THANKS KEN!!!!!)
....noting 'data' as the original array
passed the new array into a new DataStore the same way I did in the first example and it worked fine.
Assigned the DataStore to the new Grid....

<div id="grid55" class="gridclassH" ></div>



// SNIP

        var uniqueName = [];
        var FilteredData= array.filter(data, function (item) {
            var IsNameUnique = true;
            array.forEach(uniqueName, function (value) {
                var test = item.GENUS + item.SPECIES;
                if (value == (item.GENUS + item.SPECIES)) {
                    IsNameUnique = false;
                }
            });

            if (IsNameUnique) {
               uniqueName.push(item.GENUS + item.SPECIES);
            }
            return IsNameUnique;
       });

alert(print_r(FilteredData));  //call it like this
alert(print_r(data));  //call it like this

        // If you use a store...
  dataStore55 = new Memory({
      "data":FilteredData,
      "idProperty": "id"
  });

 grid55.set("store", dataStore55);
        grid55.startup();




function print_r(arr,level) {

 var dumped_text = "";
 if(!level) level = 0;

 //The padding given at the beginning of the line.
 var level_padding = "";
 for(var j=0;j<level+1;j++) level_padding += "    ";

 if(typeof(arr) == 'object') { //Array/Hashes/Objects
  for(var item in arr) {
   var value = arr[item];

   if(typeof(value) == 'object') { //If it is an array,
    dumped_text += level_padding + "'" + item + "' ...\n";
    dumped_text += print_r(value,level+1);
   } else {
    dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
   }
  }
 } else { //Stings/Chars/Numbers etc.
  dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
 }
 return dumped_text;
}
0 Kudos