21

I have one question. Is it possible to get all records which are loaded in a store when the filters are being added to store? For example, if I load into the store 34 records and then apply filters and there is only 15 left, could I get those 34 records without clearing filters?

Dave
  • 21,524
  • 28
  • 141
  • 221
kuldarim
  • 1,096
  • 8
  • 21
  • 44

6 Answers6

41

Edit: This was originally answered for Ext 4.2, where snapshot was public and documented. It is gone nowadays. So here's an update for Ext 5 and 6.

Ext 5 / 6

One liner:

var allRecords = (store.getData().getSource() || store.getData()).getRange();

Decomposition:

var data = store.getData();
// get unfiltered collection (will be null if the store has never been filtered)
var snapshot = data.getSource();
// pick the one that exists
var unfilteredCollection = snapshot || data;
// get items in an array
var allRecords = unfilteredCollection.getRange();

Store#getData gives you the store's collection.

Collection#getSource gives you the store's "source", that is the unfiltered collection -- but only if the collection has already been filtered, otherwise it returns null.

In both cases, you'll get a Ext.util.Collection. Use getRange to get an actual array of items.

Ext 5 getUnfiltered method

A getUnfiltered method was introduced at some point in Ext 5 (5.0.1 as far as I can tell, but docs for Ext 5 are offline at the moment...). It was not present in the first versions of Ext 5, and it was gone by Ext 6. So, well... Don't use it! Unless you want to tie your code to Ext 5 for no reasons, use the above method.

Ext 4

(original answer)

The whole loaded dataset is stored in the snapshot property of the store.

It is only created when needed though. That means that the property won't be available before some filters have been applied to the store. So to get the information you want in a safe way, use:

var allRecords = store.snapshot || store.data;

Ext 4 / 5 / 6

(and probably future versions)

You can use query or queryBy.

This seems to be the more forward compatible approach since, contrary to the previous methods, this API hasn't changed across versions.

Unfortunately, that will traverse the collection and incurs extra processing cost... Which may or may not be negligible depending of the size of your collection.

var allRecords = store
  .queryBy(function() { return true; }) // returns a collection
  .getRange(); // returns array of items
rixo
  • 23,815
  • 4
  • 63
  • 68
  • I don't understand why its so hard for Sencha to put a getAllRecords() method on the store. This seems non-intuitive – Oliver Watkins Nov 18 '14 at 14:49
  • Yes... I think that's the kind of issue they're trying to address with the addition of chained stores in Ext 5. – rixo Nov 19 '14 at 00:55
  • I would recommend to use `store.query()`, because that is a public and documented API, while snapshot is not. – Alexander Jun 01 '17 at 10:30
  • @Alexander It was... when it existed. I've updated my answer for newer versions. Thanks for the notification. – rixo Jun 01 '17 at 13:44
6

Perhaps a more forward compatible approach (i.e. ExtJS version >= 5) is the following:

var allRecords = store.getData().getSource().getRange();

Based on the documentaion, this should work for versions >= 5.0.

Mike Kelly
  • 69
  • 1
  • 2
4

For getting all unfiltered data from a loaded store you can try var records = store.getUnfiltered();

Note: I am using Ext Js 5.1. Not sure about earlier versions.

Sikandar
  • 160
  • 1
  • 9
2

If you want to get raw records from http response only - here is my solution:

Add the getRawRecords function to store class:

    Ext.override(Ext.data.Store, {
        getRawRecords: function(){
            return Ext.Array.map(this.getData().getRange(), function(record){
                return record.data;
            });
        }
    });

Usage:

    var rawData = store.getRawRecords();
DamianoPantani
  • 1,168
  • 2
  • 13
  • 23
1

For my case with ExtJS 4.2.1 (yeah, I know it's old) with a JSON TreeStore, I had to use: store.proxy.reader.jsonData since store.snapshot, store.data, store.query or store.queryBy did not exist.

Emmanuel
  • 13,935
  • 12
  • 50
  • 72
-1

In the Latest Extjs 6.2.0 you can use

var allRecs = Store.getData().getSource().items
AngryLeo
  • 390
  • 4
  • 23