Sencha Touch Пользовательское поле

У меня есть это настраиваемое поле в моей форме. Но когда я делаю 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);
    }
});

person Kathir    schedule 11.03.2014    source источник


Ответы (2)


Вместо расширения из контейнера я расширил 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);
}

});

person Kathir    schedule 11.03.2014

Вы можете легко добавить formpanel в качестве первого дочернего элемента, который будет содержать текстовое поле внутри.

А затем выполните formpanel.getValues(), чтобы получить значения из каждого поля внутри этой панели форм.

и другое дело, что вы можете сделать

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

но при этом я действительно предпочитаю делать это в классе контроллера.

person Rajan    schedule 14.04.2014