简体   繁体   English

在sencha touch 2下过滤关联模型

[英]Filtering in a association model under sencha touch 2

I have a form with different parameters and nested json, here the code: 我有一个具有不同参数和嵌套json的表单,此处为代码:

{
"orderoptions": [
{
  "id": "10",
  "rdd": "20150108",
  "headerfieldconfigs": [
    {
      "propertyname": "name",
      "propertyvalue": "rdd",
      "required": "true",
      "hidden": "false",
      "propertyoptions": []
    },
    {
      "propertyname": "name",
      "propertyvalue": "poNr",
      "required": "false",
      "hidden": "false",
      "propertyoptions": []
    },
    {
      "propertyname": "name",
      "propertyvalue": "shipManually",
      "required": "false",
      "hidden": "false",
      "propertyoptions": []
    },
    {
      "propertyname": "name",
      "propertyvalue": "name",
      "required": "true",
      "hidden": "false",
      "propertyoptions": []
    },
    {
      "propertyname": "name",
      "propertyvalue": "region",
      "required": "true",
      "hidden": "false",
      "propertyoptions": [
        {
          "text": "Agriento",
          "value": "AG",
          "country": "IT"
        },
        {
          "text": "Alessandria",
          "value": "AL",
          "country": "IT"
        },
        {
          "text": "Ancona",
          "value": "AN",
          "country": "IT"
        },
        {
          "text": "Aosta",
          "value": "AO",
          "country": "IT"
        },
        {
          "text": "Arezzo",
          "value": "AR",
          "country": "IT"
        },
        {
          "text": "Ascoli Piceno",
          "value": "AP",
          "country": "IT"
        },
        {
          "text": "Asti",
          "value": "AT",
          "country": "IT"
        },
        {
          "text": "San Marino",
          "value": "SM",
          "country": "SM"
        },
        {
          "text": "Bari",
          "value": "BA",
          "country": "IT"
        },
        {
          "text": "BarlettaAndriaTrani",
          "value": "BT",
          "country": "IT"
        },
        {
          "text": "Belluno",
          "value": "BL",
          "country": "IT"
        }],
    {
      "propertyname": "name",
      "propertyvalue": "country",
      "required": "true",
      "hidden": "false",
      "propertyoptions": [
        {
          "text": "Italy",
          "value": "IT",
          "country": "IT"
        },
        {
          "text": "San Marino",
          "value": "SM",
          "country": "SM"
        }
      ]
    }, 
]}

Concretely I have two selectfields called region and country and depending of the country you should select determinated regions in this example we have two countries, Italy and San Marino. 具体来说,我有两个选择区域,分别称为“区域”和“国家/地区”,在此示例中,根据国家/地区,您应该选择确定的区域,我们有两个国家/地区:意大利和圣马力诺。 Here the store code with the associated model. 这里的商店代码和相关的模型。

How to filter properly and I select Italy to see the Italy regions and so on... ?? 如何正确过滤,我选择意大利以查看意大利地区等等...?

Ext.define('OrderOption', {
extend: 'Ext.data.Model',
requires: [
    'FieldConfig'
],

config: {
    useCache: false,
    idProperty: 'id',
    fields: [
        'id',
        'salesarea',
        'ordertype',
        'ordertypetext',
        'rdd'
    ],
    associations: [

        {
            type: 'hasMany',
            associatedModel: 'FieldConfig',
            ownerModel: 'OrderOption',
            name: 'headerfieldconfigs',
            associationKey: 'headerfieldconfigs'
        }

    ]
}
});

In the controller I have started the implementation but It is not working properly.. 在控制器中,我已经开始执行,但是无法正常工作。

    onShipManuallyCountryChange: function(field, newValue, oldValue, eOpts){
    var store = Ext.getStore('OrderOptions');
    switch (newValue) {
        case 'SM' :
            this.getShipManuallyRegion().setValue(newValue);
            break;
        case 'IT' :
            this.getShipManuallyRegion().setValue('Agriento');
            break;
    }
    },

Any example?? 任何例子吗? thanks!! 谢谢!!

To resolve this issue fully you would need to show the code for your stores and the form. 要完全解决此问题,您需要显示商店和表格的代码。

It's a while since I've used ST2, But I don't beleive that you can bind a components data on child data of a store. 自从我使用ST2已经有一段时间了,但是我不相信您可以将组件数据绑定到商店的子数据上。 you have to apply the logic for this yourself. 您必须自己应用逻辑。

I have created some sample code for you that should demonstrate how this can be approached, There are multiple ways of achieving this and mine may not be the best. 我为您创建了一些示例代码,这些示例代码应演示如何实现此目标。有多种方法可以实现此目标,而我的方法可能不是最好的。 But it does work. 但这确实有效。

I have simplified the data for demo purposes and used local array data. 我已出于演示目的简化了数据,并使用了本地数组数据。

Ext.application({
    name : 'Fiddle',

    launch : function() {
        // create the models
        Ext.define('Country', {
            extend: 'Ext.data.Model',
            config: {
                fields: [
                    { name: 'id', type: 'string' },
                    { name: 'name', type: 'string' }
                ],
                associations: [{
                    type: 'hasMany',
                    associatedModel: 'Region',
                    ownerModel: 'Country',
                    primaryKey: 'id',
                    foreignKey: 'countryId',
                }]
            }
        });

        Ext.define('Region', {
            extend: 'Ext.data.Model',
            config: {
                fields: [
                    { name: 'id', type: 'string' },
                    { name: 'name', type: 'string' },
                    { name: 'countryId', type: 'string' }
                ]
            }
        });


        var countries = [
            ["IT", "Italy"],
            ["SM", "San Marino"]
        ];

        var regions = [
            ["AG", "Agriento", "IT"],
            ["AL", "Alessandria", "IT"],
            ["AN", "Ancona", "IT"],
            ["AO", "Aosta", "IT"],
            ["AR", "Arezzo", "IT"],
            ["AP", "Ascoli Piceno", "IT"],
            ["AT", "Asti", "IT"],
            ["SM", "San Marino", "SM"],
            ["BA", "Bari", "IT"],
            ["BT", "BarlettaAndriaTrani", "IT"],
            ["BL", "Belluno", "IT"]    
        ];

        var countryStore = Ext.create('Ext.data.ArrayStore', {
            model: "Country",
            // store configs
            storeId: 'countryStore',
            data: countries
        });

        var regionStore = Ext.create('Ext.data.ArrayStore', {
            model: "Region",
            // store configs
            storeId: 'regionStore',
            autoLoad: false,
            data: regions
        });

        Ext.create('Ext.form.Panel', {
            fullscreen: true,
            items: [{
                xtype: 'fieldset',
                title: 'Select',
                items: [{
                    xtype: 'selectfield',
                    name: "countrySelect",
                    id: "countrySelectField",
                    displayField: "name",
                    valueField: "id",
                    label: 'Choose one',
                    store: countryStore,
                    autoSelect: false,
                    listeners: {
                        'change': function( select, newValue, oldValue, eOpts ) {
                            regionStore.filter("countryId", newValue);
                        }
                    }
                }, {

                    xtype: 'selectfield',
                    name: "regionSelect",
                    id: "regionSelectField",
                    displayField: "name",
                    valueField: "id",
                    autoSelect: false,
                    label: 'Choose one',
                    store: regionStore
                }]
            }]
        });
    }
});

I have also created a fiddle HERE using Sencha Fiddle which demonstrates this in use. 我还创建了一个小提琴这里使用煎茶小提琴这表明该使用。

Bascially you listen to the change event and then apply a filter on the child store. 通常,您会监听更改事件,然后对子存储应用过滤器。

Hope this helps 希望这可以帮助

EDIT: Here is an example of doing this with Nested Json, Although the JSON supplied is not valid, I would also recommend simplifying it: 编辑:这是使用嵌套Json进行此操作的示例,尽管提供的JSON无效,但我也建议简化它:

// app.js
Ext.application({
    name : 'Fiddle',

    launch : function () {
        // create the models
        Ext.define('Country', {
            extend: 'Ext.data.Model',
            config: {
                fields: [
                    { name: 'id', type: 'string' },
                    { name: 'name', type: 'string' }
                ],
                associations: [{
                    type: 'hasMany',
                    associatedModel: 'Region',
                    ownerModel: 'Country',
                    primaryKey: 'id',
                    foreignKey: 'countryId',
                    associationKey: "regions"
                }]
            }
        });

        Ext.define('Region', {
            extend: 'Ext.data.Model',
            config: {
                fields: [
                    { name: 'id', type: 'string' },
                    { name: 'name', type: 'string' },
                    { name: 'countryId', type: 'string' }
                ]
            }
        });

        var myStore = Ext.create("Ext.data.Store", {
            // store configs
            autoDestroy: true,
            storeId: 'myStore',
            autoLoad:true,
            model: "Country",

            proxy: {
                type: 'ajax',
                url: '/data/data1.json',
                reader: {
                    type: 'json',
                    rootProperty: 'countries',
                    idProperty: 'id'
                }
            }
        });

        Ext.create('Ext.form.Panel', {
            fullscreen: true,
            items: [{
                xtype: 'fieldset',
                title: 'Select',
                items: [{
                    xtype: 'selectfield',
                    name: "countrySelect",
                    id: "countrySelectField",
                    displayField: "name",
                    valueField: "id",
                    label: 'Choose one',
                    store: myStore,
                    autoSelect: false,
                    listeners: {
                        'change': function( select, newValue, oldValue, eOpts ) {
                            var country = select.getStore().getById(newValue);
                            console.log(country);
                            console.log(country.regions());
                        }
                    }
                }]
            }]
        });
    }
});

// data/data1.json
{
  "countries": [{
    "id": "IT",
    "name":"Italy",
    "regions": [
      {"id":"AG", "name": "Agriento", "countryId": "IT"},
      {"id":"AL", "name": "Alessandria", "countryId": "IT"},
      {"id":"AN", "name": "Ancona", "countryId": "IT"},
      {"id":"AO", "name": "Aosta", "countryId": "IT"},
      {"id":"AR", "name": "Arezzo", "countryId": "IT"},
      {"id":"AP", "name": "Ascoli Piceno", "countryId": "IT"},
      {"id":"AT", "name": "Asti", "countryId": "IT"},
      {"id":"BA", "name": "Bari", "countryId": "IT"},
      {"id":"BT", "name": "BarlettaAndriaTrani", "countryId": "IT"},
      {"id":"BL", "name": "Belluno", "countryId": "IT"}
    ]
  }, {
    "id": "SM",
    "name":"San Marino",
    "regions": [
      {"id":"SM", "name": "San Marino", "countryId": "SM"}
    ]
  }]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM