1. #1
    Ext JS Premium Member funkadelic's Avatar
    Join Date
    Mar 2008
    Location
    Mountain View, CA
    Posts
    44
    Vote Rating
    0
    funkadelic is on a distinguished road

      0  

    Default filtering a jsonstore

    filtering a jsonstore


    hi,

    i have a grouping store that I'd like to filter by a particular field in the JSON data, but am not sure how the filtering mechanism works

    here's my GroupingStore:
    Code:
      var store = new Ext.data.GroupingStore({
        url: "/data/list.js",
        reader: new Ext.data.JsonReader({
          root: "rows",
          totalProperty: 'results',
          fields:[
            {name: 'title'},
            {name: 'filename'},
            {name: 'status'},
            {name: 'type'},
            {name: 'size'}
          ]
        }),
        sortInfo: {
          field: 'title', 
          direction: "ASC"
        },
        groupField: 'status'
      });
    I am grouping by the status field, but that field is sometimes null. How would I filter out the nulls so that those rows don't appear in the grid? Do i do it on the GroupingStore or the JsonReader?

    thanks for any tips!

  2. #2
    Ext JS Premium Member funkadelic's Avatar
    Join Date
    Mar 2008
    Location
    Mountain View, CA
    Posts
    44
    Vote Rating
    0
    funkadelic is on a distinguished road

      0  

    Default


    just a quick follow-up. i saw that there is a filterBy method on GroupingStore, so i tried the following, expecting to see some output in the firebug console
    Code:
    var store = new Ext.data.GroupingStore({
      url: "/data/list.js",
      reader: new Ext.data.JsonReader({
        root: "rows",
        totalProperty: 'results',
        fields:[
          {name: 'title'},
          {name: 'filename'},
          {name: 'status'},
          {name: 'type'},
          {name: 'size'}
        ]
      }),
      sortInfo: {
        field: 'title', 
        direction: "ASC"
      },
      groupField: 'status',
      filterBy: function(a,b){
        console.log("a:" +a)
        console.log("b:" +b)
      }
    });
    i was expecting to see the arguments passed to filterBy in the firebug console, but didn't see any output. am i going down the right path?

    thanks

  3. #3
    Ext JS Premium Member funkadelic's Avatar
    Join Date
    Mar 2008
    Location
    Mountain View, CA
    Posts
    44
    Vote Rating
    0
    funkadelic is on a distinguished road

      0  

    Smile


    i figured this one out, after stumbling through a few mistakes, so I'm posting here for the archive in the hope that this will save someone some headache down the line

    1) i erred and misread the API docs thinking the filterBy() was a config option, when it is actually a method
    2) then i erred by calling filterBy() before store.load() was called
    3) i then called store.filterBy() after store.load(), but forgot that store.load() is asynchronous, so filterBy() is called before the actual store is loaded
    4) Finally calling filterBy() in the callback to store.load() got me to where I needed to be

    here's the complete code
    Code:
    var store = new Ext.data.GroupingStore({
      url: "/data/list.js",
      reader: new Ext.data.JsonReader({
        root: "rows",
        totalProperty: 'results',
        fields:[
          {name: 'title'},
          {name: 'filename'},
          {name: 'status'},
          {name: 'type'},
          {name: 'size'}
        ]
      }),
      sortInfo: {
        field: 'title', 
        direction: "ASC"
      },
      groupField: 'status'
    });
    
    //load the store 
    store.load({
      // the filterBy needs to happen in this callback since load() is asynchronous
      callback: function() {
        store.filterBy(function(record){
          if (record.get("status") != "") {
            return true;
          }
          return false;
        });
      }
    });

  4. #4
    Sencha User
    Join Date
    Oct 2012
    Posts
    10
    Vote Rating
    0
    dkahl is on a distinguished road

      0  

    Default


    And your solution(4) works great for a plain json store without grouping! I didn't know that the filterBy() need to go go INSIDE the load(). It actually works in that location .

    Thanks very much for these appends!
    dkahl

Thread Participants: 1