Extjs模型数组的字符串映射 [英] Extjs model array of string mapping

查看:239
本文介绍了Extjs模型数组的字符串映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个非常基本的问题,但是恰好浪费了我很多时间.

It seems to be very basic question, but happens to kill lot of my time.

如何将以下内容映射到Ext.data.Model?

How can I map following in to Ext.data.Model?

<Principal>STEMMED CORP.</Principal>
<BusinessDefinitions>
    <value>NY CLIENTS CORE</value>
    <value>US LISTED DERIVS- STOCK,ADR,ETF</value>
    <value>US CLIENT SITE TRADING</value>
    <value>SYNDICATES - ADRS AND CBS</value>
    <value>GWM CAPITAL MARKETS</value>
</BusinessDefinitions>
<countryOfResidence>USA</countryOfResidence>

问题是,我无法弄清楚如何针对每个值获取BusinessDefinitions的字符串数组.

Problem is, I unable to figure out how to get array of string for BusinessDefinitions against each value.

以下字段是我添加到Model.fields的内容:

The following field is what I have added to Model.fields:

// Business Definition(s)
{
    name: 'BusinessDefinitions',
    type: 'auto',
    mapping: 'value',
    convert: function(n, record) {
        console.log('BusinessDefinition: ' + n);
        return n;
    }                       
} 

我也尝试了其他组合,但似乎无济于事.

I have tried other combinations as well, but nothing seem to work.

推荐答案

下面的示例适合于您的数据.

The following was fabricated to fit your data from the example below.

以下是我提供的答案的 Sencha Fiddle .符合 4.2.1.883 .我还没有尝试使用版本 5.1.0 .

Here is a Sencha Fiddle of the answer I have provided. It is 4.2.1.883 compliant. I have yet to try this with version 5.1.0.

<BusinessArray>
    <BusinessItem>
        <Principal>STEMMED CORP.</Principal>
        <BusinessDefinitions>
            <value>NY CLIENTS CORE</value>
            <value>US LISTED DERIVS- STOCK,ADR,ETF</value>
            <value>US CLIENT SITE TRADING</value>
            <value>SYNDICATES - ADRS AND CBS</value>
            <value>GWM CAPITAL MARKETS</value>
        </BusinessDefinitions>
        <countryOfResidence>USA</countryOfResidence>
    </BusinessItem>
</BusinessArray>

应用程序

Ext.define('App.model.Business', {
    requires: [ 'Ext.data.reader.Xml' ],
    extend: 'Ext.data.Model',
    fields: [{
        name: 'principal',
        mapping: 'Principal',
        type: 'string'
    }, {
        name: 'country',
        mapping: 'countryOfResidence',
        type: 'string'
    }, {
        name: 'businessDefs',
        type : 'auto',
        convert: function(value, record) {
            var nodes = record.raw.querySelectorAll('BusinessDefinitions value');
            var items = [];
            for (var i = 0; i < nodes.length; i++) {
                items.push(nodes[i].textContent);
            }
            return items;
        }
    }] 
});

Ext.application({
    name : 'Fiddle',
    launch : function() {
        var store = Ext.create('Ext.data.Store', {
            model: 'App.model.Business',
            proxy: {
                type: 'ajax',
                url: 'business.xml',
                reader: {
                    type: 'xml',
                    record: 'BusinessItem'
                }
            },
            autoLoad : true
        });
        Ext.create('Ext.panel.Panel', {
            title : 'XML Model Example',
            layout : 'hbox',
            items : [{
                xtype: 'combo',
                fieldLabel: 'Business',
                emptyText: 'select',
                editable: false,
                queryMode: 'local',
                store: store,
                displayField: 'principal',
                valueField: 'businessDefs',
                listeners : {
                    select: function (combo, record, index) {
                        Ext.Msg.alert('Business Definitions', combo.getValue().join('<br />'));
                    }
                }
            }],
            renderTo: Ext.getBody()
        });
    }
});


示例

以下示例来自


Example

The example below is from the accepted solution from Sencha Forums: How do I parse a XML node to an array of strings? Also handing XML attributes?.

<jobs>
    <job>
        <id>1</id>
        <name audioSrc="audio/jobs/names/electrician.mp3">Electrician</name>
        <attributes>
            <attributeID>sitting</attributeID>
            <attributeID>individual</attributeID>
            <attributeID>lightEquip</attributeID>
            <attributeID>doer</attributeID>
            <attributeID>physical</attributeID>
            <attributeID>repair</attributeID>
        </attributes>
    </job>
</jobs>

商店

Ext.create('Ext.data.Store', {
    model: 'App.model.JobData',
    proxy: {
        type: 'ajax',
        url: dataURL,
        reader: {
            type: 'xml',
            record: 'job'
        }
    }        
});

型号

Ext.define('App.model.JobData', {
    requires: [ 'Ext.data.reader.Xml' ],
    extend: 'Ext.data.Model',
    config: {
        fields: [{
            name: 'id',
            mapping: 'id',
            type: 'int'
        }, {
            name: 'title',
            mapping: 'name',
            type: 'string'
        }, {
            name: 'attributeList',
            mapping : 'attributes',
            convert: function(value, record) {
                var nodes = record.raw.querySelectorAll('attributes attributeID');
                var arrayItem = [];
                var l = nodes.length;
                for (var i = 0; i < l; i++) {
                    var node = nodes[i];
                    arrayItem.push(nodes[i].textContent);
                    console.log(nodes[i].textContent);
                }
                return arrayItem;
            }
        }] 
    }
});

这篇关于Extjs模型数组的字符串映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆