0

我的表单中有这个自定义字段。但是当我执行 form.getValues() 时,该字段的值为空。这是创建自定义表单字段的正确方法吗?

Ext.define("Tasks.view.BarcodeField", {
    extend: 'Ext.Container',
    alias:'widget.barcodeField',

    xtype: 'barcodefield',

    config: {    
        layout: 'hbox',
        id: 'barcodeField',
        itemId: 'barcodeField',
        items: [
            {
                xtype: 'textfield',
                label: 'Barcode',
                labelWidth: '37.4%',                            
                flex: 4        
            },
            {
                xtype: 'image',
                id : 'barcodeScanner',
                itemId : 'barcodeScanner',
                src: 'resources/images/barcodes.png',
                padding: '6 0 0 0',
                flex: 1,
                listeners: {
                    tap: function() {
                        console.log("Starting the barcode Scanner");
                        function success(result) {
                             alert("We got a barcode\n" +
                            "Result: " + result.text + "\n" +
                            "Format: " + result.format + "\n" +
                            "Cancelled: " + result.cancelled);
                        }

                        function fail(error) {
                              alert("Scanning failed: " + error);
                        }

                         cordova.plugins.barcodeScanner.scan(success, fail);
                    }
                }               
            }        
        ]        
    },

    getValue : function() 
    {   
        console.log(this.getItems().getAt(0).getValue());
        return this.getItems().getAt(0).getValue();
    },
    setValue : function(newvalue) {
       this.getItems().getAt(0).setValue(newvalue);
    }
});
4

2 回答 2

1

我没有从 Container 扩展,而是扩展了 Ext.field.Field 并将项目作为子组件添加到该组件。

Ext.define("Tasks.view.BarcodeField", {
extend: 'Ext.field.Field',
alias:'widget.barcodeField',

xtype: 'barcodefield',

config: {    
    layout: 'hbox',
    id: 'barcodeField',
    itemId: 'barcodeField',
    isField:false,
    component : {
        xtype:'panel',
        layout:'hbox',
        items: [
            {
                xtype: 'input',
                label: 'Barcode',
                labelWidth: '37.4%',                            
                flex: 4,
                id:'barcodeTextField',
                itemId:'barcodeTextField'
            },
            {
                xtype: 'image',
                id : 'barcodeScanner',
                itemId : 'barcodeScanner',
                src: 'resources/images/barcodes.png',
                padding: '6 0 0 0',
                flex: 1,
                listeners: {
                    tap: function() {
                        console.log("Starting the barcode Scanner");
                        var text = Ext.ComponentQuery.query("#barcodeTextField")[0];
                            text.setValue("123456");

                    }
                }               
            }        
        ]   
    }       
},

getValue : function() 
{   
    console.log(this.getComponent().getItems().getAt(0).getValue());
    return this.getComponent().getItems().getAt(0).getValue();
},
setValue : function(newvalue) {
   this.getComponent().getItems().getAt(0).setValue(newvalue);
}

});

于 2014-03-11T18:00:08.133 回答
0

您可以轻松地添加一个表单面板,因为第一个孩子将在其中包含一个文本字段。

然后执行 formpanel.getValues() 以从该表单面板内的每个字段中获取值。

另一件事是你可以做

this.getItems()[0].getValue();
this.getItems()[0].setValue('someDesiredValue');

但话虽如此,我真的更喜欢在控制器类上这样做。

于 2014-04-14T09:05:17.723 回答