javascript Extjs 获取所有商店记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18658105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:47:24  来源:igfitidea点击:

Extjs get all store records

javascriptextjsfilterstore

提问by kuldarim

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?

我有一个问题。是否可以在将过滤器添加到存储时获取加载到存储中的所有记录?例如,如果我将 34 条记录加载到商店中,然后应用过滤器,并且只剩下 15 条记录,那么我是否可以在不清除过滤器的情况下获得这 34 条记录?

回答by rixo

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

编辑:这最初是为 Ext 4.2 回答的,其中snapshot公开并记录在案。现在已经没有了。所以这里是 Ext 5 和 6 的更新。

Ext 5 / 6

分机 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#getDatagives you the store's collection.

Store#getData为您提供商店的收藏。

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

Collection#getSource为您提供商店的“来源”,即未过滤的集合 - 但前提是该集合已被过滤,否则返回null.

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

在这两种情况下,您都会得到一个Ext.util.Collection. 使用getRange来获得项目的实际阵列。

Ext 5 getUnfilteredmethod

扩展 5getUnfiltered方法

A getUnfilteredmethod 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.

getUnfiltered在 Ext 5 中的某个时候引入了一种方法(据我所知是 5.0.1,但 Ext 5 的文档目前处于离线状态......)。它没有出现在 Ext 5 的第一个版本中,它在 Ext 6 中消失了。所以,好吧...不要使用它!除非您想无缘无故地将代码绑定到 Ext 5,否则请使用上述方法。

Ext 4

分机 4

(original answer)

(原答案)

The whole loaded dataset is stored in the snapshotproperty of the store.

整个加载的数据集存储在snapshot商店的属性中。

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

分机 4 / 5 / 6

(and probably future versions)

(可能还有未来的版本)

You can use queryor queryBy.

您可以使用queryqueryBy

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

这似乎是更向前兼容的方法,因为与以前的方法相反,此 API 没有跨版本更改。

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

回答by Mike Kelly

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

也许更向前兼容的方法(即 ExtJS 版本 >= 5)如下:

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

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

根据文档,这应该适用于 >= 5.0 的版本。

回答by Sikandar

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

要从加载的存储中获取所有未过滤的数据,您可以尝试 var records = store.getUnfiltered();

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

注意:我使用的是 Ext Js 5.1。不确定早期版本。

回答by Damiano

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

如果您只想从 http 响应中获取原始记录 - 这是我的解决方案:

Add the getRawRecordsfunction to storeclass:

getRawRecords函数添加到store类:

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

Usage:

用法:

    var rawData = store.getRawRecords();

回答by Emmanuel

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.jsonDatasince store.snapshot, store.data, store.queryor store.queryBydid not exist.

对于我来说,用的ExtJS 4.2.1(是的,我知道这是旧的),用JSON TreeStore,我不得不使用:store.proxy.reader.jsonData因为store.snapshotstore.datastore.querystore.queryBy根本不存在。

回答by AngryLeo

In the Latest Extjs 6.2.0 you can use

在最新的 Extjs 6.2.0 中你可以使用

var allRecs = Store.getData().getSource().items