3

我正在使用 extjs。我需要根据要求从存储中检索不同的数据。在我的商店中包含单个列表而不是嵌套列表,其中包含美洲、北海和东南亚等地区。这些区域本身具有 subRegion、vesselName 和vesselType 值。我需要根据区域检索唯一值,因为它包含许多重复记录。我已经尝试如下,但它对我不起作用。谁能告诉我如何实现?. 非常感谢。谢谢你。

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"
  }]
4

2 回答 2

5

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

vesselStore.collect('region')

于 2013-04-03T13:47:32.070 回答
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 );
于 2013-04-04T12:15:41.917 回答