如何从商店获取独特/独特的数据?

时间:2013-04-03 13:06:08

标签: extjs extjs4 extjs4.1 extjs-mvc

我正在使用extjs。我需要根据要求从商店中检索不同的数据。 在我的商店中包含单个列表而非嵌套列表,其中包含AMERICAS,North Sea和SE Asia等地区。这些区域本身有subRegion,vesselName和vesselType值。我需要根据区域检索唯一值,bacasue包含许多重复记录。我尝试过如下,但它不起作用。 任何人都可以告诉我如何实现?。非常感谢。谢谢。

var vesselStore=Ext.getStore('VesselStatusReportStore');
        var arr=new Array();
        var obj;      
        vesselStore.each(function(rec,index)
        {
            obj=new Object();
            if(rec.get('region')=='AMERICAS'){
                obj.subRegionAmerica=rec.get('subRegion');
                obj.vesselNameAmerica=rec.get('vesselName');
                obj.vesselTypeAmerica=rec.get('vesselType');
            }
            if(rec.get('region')=='NorthSea'){
                obj.subRegionNorthSea=rec.get('subRegion');
                obj.vesselNameNorthSea=rec.get('vesselName');
                obj.vesselTypeNorthSea=rec.get('vesselType');
            }
            if(rec.get('region')=='SE Asia'){
                obj.subRegionSEAsia=rec.get('subRegion');
                obj.vesselNameSEAsia=rec.get('vesselName');
                obj.vesselTypeSEAsia=rec.get('vesselType');
            }
            arr.push(obj);
            console.log(obj);
        });
杰森:

[ { 
    "region" : "AMERICAS",
    "startDate" : null,
    "subRegion" : "US",
    "vesselName" : "Thoma-Sea � Hull #147",
    "vesselType" : "PSV"
  },
  { 
    "region" : "AMERICAS",
    "startDate" : null,
    "subRegion" : "US",
    "vesselName" : "Thoma-Sea � Hull #148",
    "vesselType" : "PSV"
  },
  { 
    "region" : "AMERICAS",
    "startDate" : null,
    "subRegion" : "Mexico",
    "vesselName" : "Thoma-Sea � Hull #148",
    "vesselType" : "PSV"
  }]

2 个答案:

答案 0 :(得分:4)

看起来你想要使用收藏。 http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-collect

vesselStore.collect('region')

答案 1 :(得分:1)

这部分没有意义:

            obj=new Object();
            if(rec.get('region')=='AMERICAS'){
                obj.subRegionAmerica=rec.get('subRegion');
                obj.vesselNameAmerica=rec.get('vesselName');
                obj.vesselTypeAmerica=rec.get('vesselType');
            }
            if(rec.get('region')=='NorthSea'){
                obj.subRegionNorthSea=rec.get('subRegion');
                obj.vesselNameNorthSea=rec.get('vesselName');
                obj.vesselTypeNorthSea=rec.get('vesselType');
            }
            if(rec.get('region')=='SE Asia'){
                obj.subRegionSEAsia=rec.get('subRegion');
                obj.vesselNameSEAsia=rec.get('vesselName');
                obj.vesselTypeSEAsia=rec.get('vesselType');
            }
            arr.push(obj);

您基本上是在说“无论区域是什么,请将记录复制到obj,然后将obj添加到我的阵列”。

我相信你的意思更多:

var vesselStore=Ext.getStore('VesselStatusReportStore');
var iRegions = [];

vesselStore.each(function(rec,index)
{
    var iRegionName = rec.get('region');

    // Make sure theres a array item with the region name,
    // if not create a blank array;
    iRegions[ iRegionName ] = iRegions[ iRegionName ] || [];

    // Add the record to its corresponding array item.
    // (If you don't want the full record, you can just get the
    // fields individually like you did in your code).
    iRegions[ iRegionName ] = rec;
}
console.log( iRegions );