【问题标题】:Combo box values in ext jsext js中的组合框值
【发布时间】:2018-03-20 14:09:03
【问题描述】:

我是 ext js 的新手(使用 ext js 6.2)并试图从组合框中获取值以从选定值获取 3d 条形图,但我既没有得到选定值,也没有得到选定值的图表。请帮我解决这个问题。

我的代码如下:

2FAData.js:

Ext.define('LICApp.store.2FAData', {
    extend: 'Ext.data.Store',
    alias: 'store.2fa-data',

    requires: [
        'Ext.data.reader.Xml'
    ],
    autoLoad: true,

    fields: ['name', 'cnt', 'zone'],
    groupField: 'zone',


    proxy: {
        type: 'ajax',
        cors: 'true',
        url: 'http://localhost:8080/UserManagement/rest/BiodataController/bio',
        method: 'POST',
        reader: {
            type: 'xml',
            record: 'biodata',
            rootProperty: 'biodatas'
        }
    },
});

基本.js

Ext.define('LICApp.view.charts.bar3d.Basic', {
    extend: 'Ext.Panel',
    xtype: 'bar-basic-3d',
    controller: 'bar-basic-3d',

    requires: [
        'Ext.chart.theme.Muted',
        'LICApp.store.2FAData',
        'Ext.grid.feature.Grouping'

    ],

    width: 1300,

    items: [{
        xtype: 'combobox',
        hideLabel: true,
        store: {
            type: '2fa-data'

        },
        valueField: 'zone',
        displayField: 'zone',
        typeAhead: true,
        queryMode: 'local',
        triggerAction: 'query',
        emptyText: 'Select a Zone...',
        selectOnFocus: true,
        width: 200,
        indent: true
    },
    {
        xtype: 'cartesian',
        flipXY: true,
        reference: 'chart',
        width: '100%',
        height: 500,
        insetPadding: '40 40 30 40',
        innerPadding: '3 0 0 0',
        theme: {
            type: 'muted'
        },
        store: {
            type: '2fa-data',


        },
        animation: {
            easing: 'easeOut',
            duration: 500
        },
        interactions: ['itemhighlight'],
        axes: [{
            type: 'numeric3d',
            //position: 'bottom',
            //fields: 'name',
            //maximum: 150000,
            //majorTickSteps: 10,
            renderer: 'onAxisLabelRender',
            //title: 'Number of Employees',
            grid: {
                odd: {
                    fillStyle: 'rgba(245, 245, 245, 1.0)'

                },
                even: {
                    fillStyle: 'rgba(255, 255, 255, 1.0)'

                }
            }
        }, {
            type: 'category3d',
            position: 'left',
            fields: 'name',
            label: {
                textAlign: 'right'
            },
            grid: true
        }],
        series: [{
            type: 'bar3d',
            xField: 'name',
            yField: 'cnt',
            style: {
                minGapWidth: 10
            },
            highlight: true,
            label: {
                field: 'cnt',
                display: 'insideEnd',
                renderer: 'onSeriesLabelRender'
            },
            tooltip: {
                trackMouse: true,
                renderer: 'onSeriesTooltipRender'
            }
        }],
        sprites: [{
            type: 'text',
            text: 'Implementation of 2FA Biometric - Progress Chart',
            fontSize: 22,
            width: 100,
            height: 30,
            x: 40, // the sprite x position
            y: 20  // the sprite y position
        }, {
            type: 'text',
            text: 'Source: 2FA Data Server',
            fontSize: 10,
            x: 12,
            y: 490
        }]
    }],
    tbar: [
        '->',
        {
            text: 'Preview',
            handler: 'onPreview'
        }
    ],
    listeners: {
        select: 'onItemSelected'
    }

});

基本控制器.js

Ext.define('LICApp.view.charts.bar3d.BasicController', {
extend: 'Ext.app.ViewController',
alias: 'controller.bar-basic-3d',

onAxisLabelRender: function (axis, label, layoutContext) {
    return Ext.util.Format.number(layoutContext.renderer(label) );
},

onSeriesLabelRender: function (v) {
    return Ext.util.Format.number(v);
},

onSeriesTooltipRender: function (tooltip, record, item) {
    var formatString = '0,000 ';

    tooltip.setHtml(record.get('zone') + ': ' +
        Ext.util.Format.number(record.get('cnt'), formatString));

},

onPreview: function () {
    if (Ext.isIE8) {
        Ext.Msg.alert('Unsupported Operation', 'This operation requires a newer version of Internet Explorer.');
        return;
    }
    var chart = this.lookupReference('chart');
    chart.preview();

        },
 onItemSelected: function (sender, record) {
    var zone = combo.getValue();
 },       

});

【问题讨论】:

标签: javascript extjs


【解决方案1】:

您应该将select 侦听器附加到您的组合框:

{
  xtype: 'combobox',
  hideLabel: true,
  store: {
    type: '2fa-data'
  },
  valueField: 'zone',
  displayField: 'zone',
  typeAhead: true,
  queryMode: 'local',
  triggerAction: 'query',
  emptyText: 'Select a Zone...',
  selectOnFocus: true,
  width: 200,
  indent: true,
  listeners: {
     select: 'onItemSelected' //this one
  }
}

在 ViewController 中的 onItemSelected 方法中注意:

onItemSelected: function (sender, record) { //<- param is sender
   //var zone = combo.getValue();  //you are using combo, this is undefined
   var zone = sender.getValue();
   // or better yet, why not use the `record` parameter?
   var theValue = record[0].data.zone;
}, 

【讨论】:

    【解决方案2】:

    Sencha Ext JS MVVM 架构的binding 特性有助于编写声明性代码并避免在控制器中编写处理程序。由于它是向 6.2 的迁移,因此可以采用这种方法来删除控制器中不必要的处理程序。 Here's 一个小例子。

    更新: 参考提供的示例,请阅读以下几点:

    1. 父类 Panel 有一个 View Model,它在层次结构中可用于 combo boxchart 组件。

    2. View Model 有一个data 属性rec,它保存对组合框中所选记录的引用。组合框的selection 属性绑定到此rec 属性。这意味着从组合框中选择新记录的那一刻,View Model 的rec 属性就会更新。

    3. View Model 还有两家商店 - comboStorechartStore

      一个。 comboStore: - 包含完整的数据集。它绑定到组合框。

      b. chartStore:- 是comboStore 的子存储(子存储从父存储获取其数据,但具有额外的过滤和排序能力,独立于父存储。)。它绑定到图表组件并在rec.name 属性上进行过滤(即,在更新rec 的那一刻,触发过滤。)因此,从技术上讲,此存储始终仅包含组合框中的选定记录。并且由于图表绑定到这个商店,它也会更新并显示所选记录的 3dbar 图表。

    这是内联代码。有关更多信息,请参阅 cmets。

    Ext.define('MyPanel', {
            extend: 'Ext.panel.Panel',
            layout: 'vbox',
    
            //Declare parent class view model
            //This view model will be available down the hierarchy
            viewModel: {
                //Declare viewmodel's static data properties
                data: {
                    //This references the selected record from combo box
                    rec: null,
                },
                //Declare stores for this viewmodel
                stores: {
                    //Declare store for combo box containing complete dataset
                    comboStore: {
                        fields: ['name', 'apples', 'oranges'],
                        data: [{
                            name: 'Eric',
                            apples: 10,
                            oranges: 13
                        }, {
                            name: 'Mary',
                            apples: 7,
                            oranges: 20
                        }, {
                            name: 'John',
                            apples: 5,
                            oranges: 12
                        }, {
                            name: 'Bob',
                            apples: 2,
                            oranges: 30
                        }, {
                            name: 'Joe',
                            apples: 19,
                            oranges: 17
                        }, {
                            name: 'Macy',
                            apples: 13,
                            oranges: 4
                        }]
                    },
                    //Declare store for chart component
                    chartStore: {
                        //This is child store of 'ComboStore'. Its data source is 'comboStore'
                        source: '{comboStore}',
                        //This filters this child store to contain only the selected record from combo box
                        filters: [{
                            //This filters the store by 'name' property, which is the 'displayField' of combo box
                            property: 'name',
                            //This binding helps to filter by the selected record's 'name' property
                            //'rec' is the selecte record which is available in the view model
                            value: '{rec.name}'
                        }],
                    }
                }
            },
            items: [{
                xtype: 'mycombo',
                bind: {
                    //This binding provides 'comboStore' data to the combobox
                    store: '{comboStore}',
                    //The selection property of combo box is published to the viewmodel
                    //and its reference is stored in the viewmodel data property 'rec'
                    selection: '{rec}'
                }
            }, {
                xtype: 'mychart',
                bind: {
                    //This binding provides 'chartStore' data to the chart component
                    //Since it is bound, the moment this store is updated/filtered, the chart view gets updated too
                    store: '{chartStore}'
                }
            }]
        });
    

    【讨论】:

    • 我无法获得示例。
    【解决方案3】:

    请将 select 事件的侦听器(而不是 panel)更改为 combobox。面板没有选择事件。

    我为组合框选择/更改这两个事件创建了一个Sencha Fiddle 演示。它将显示它是如何工作的。希望这将帮助您解决问题或实现您的功能。

    // The data store containing the list of states
    var states = Ext.create('Ext.data.Store', {
        fields: ['abbr', 'name'],
        data: [{
            "abbr": "AL",
            "name": "Alabama"
        }, {
            "abbr": "AK",
            "name": "Alaska"
        }, {
            "abbr": "AZ",
            "name": "Arizona"
        }]
    });
    
    // Create the combo box, attached to the states data store
    Ext.create('Ext.form.ComboBox', {
        fieldLabel: 'Choose State',
        store: states,
        queryMode: 'local',
        margin: 10,
        displayField: 'name',
        valueField: 'abbr',
        renderTo: Ext.getBody(),
        listeners: {
            select: function (combo, record) {
                    Ext.Msg.alert('Success', 'Congrtas you have selected <b>' + record.get("name") + '</b>');
                }
                /*change: function (combo, newValue,oldValue) {
                    Ext.Msg.alert('Success', 'Congrtas you have selected <b>' + newValue + '</b>');
                }*/
        }
    });
    

    【讨论】:

    • 非常欢迎 :) 请接受答案,以便对其他人也有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多