为什么网格中没有添加记录?

时间:2011-12-21 09:13:42

标签: extjs grid extjs4 store

我为多个文件上传创建了一个自定义字段,问题是在第一步我甚至无法将所选文件添加到网格中,你能告诉我我的代码有什么问题吗?我查看了firebug,没有java脚本错误。

Ext.define('VDOA.form.fields.Attachment', {
extend: 'Ext.form.FieldContainer',
mixins: {field: 'Ext.form.field.Field'},
requires: ['Ext.form.field.Base'],
alias: 'widget.attachment',
layout: 'fit',
constructor: function()
{
    var me = this;

        me.items = [
                {
                    itemId: 'grid',
                    anchor: '100%',
                    width: 300,
                    height: 100,
                    xtype: 'gridpanel',
                    layout: 'fit',
                    autoRender: true,
                    autoShow: true,
                    tbar: [
                        {
                            itemId: 'add',
                            hideLabel: true,
                            buttonOnly: true,
                            buttonText: 'Browse a file',
                            xtype: 'fileuploadfield'
                        }
                    ],
                    columns: [
                        {
                            dataIndex: 'Id',
                            xtype: 'gridcolumn',
                            text: 'File Id'
                        },
                        {
                            dataIndex: 'Title',
                            xtype: 'gridcolumn',
                            text: 'File Name'
                        }
                    ]
                }
        ];

        me.callParent(arguments);

    var store = Ext.create('Ext.data.ArrayStore', {
            fields: [
                {name: 'Id', type: 'int'},
                {name: 'Title', type: 'string'},
                {name: 'IsUploading', type: 'bool'}
            ],
            data: []
        });

        me.down('#grid').store = store;

        me.down('#add').on('change', function(o, e){
            store.add({Id: Ext.id(), Title: o.value, IsUploading: true});
            store.load();
        });
},

getErrors: function() {
    return [];
},

validate: function() {
    return true;
}});   Ext.onReady(function() {
Ext.QuickTips.init();

var win = new Ext.Window({
     width:500
    ,id:'winid'
    ,height:300
    ,layout:'fit'
    ,border:false
    ,closable:false
    ,title:'File Upload'
    ,items:[{
         xtype:'form'
        ,frame:true
        ,labelWidth:100
        ,items:[{
            name: 'Title',
            xtype: 'textfield',
            fieldLabel: 'Title',
            allowBlank: false,
            anchor: '100%'
        },
        {
            name: 'Attachment',
            xtype: 'attachment',
            fieldLabel: 'Attached Files'
        }]
    }]
    ,buttons:[{
         text:'Submit'
        ,handler:function() {
            Ext.getCmp('form').getForm().submit();
        }
    }]
});
win.show();});

3 个答案:

答案 0 :(得分:1)

 Ext.define('VDOA.form.fields.Attachment', {
        extend:'Ext.form.FieldContainer',
        mixins:{field:'Ext.form.field.Field'},
        requires:['Ext.form.field.Base'],
        alias:'widget.attachment',
        layout:'fit',
        constructor:function () {
            var me = this,
                store = Ext.create('Ext.data.ArrayStore', {
                    fields:[
                        {name:'Id', type:'int'},
                        {name:'Title', type:'string'},
                        {name:'IsUploading', type:'bool'}
                    ],
                    data:[]
                });
            me.items = [
                {
                    itemId:'grid',
                    anchor:'100%',
                    width:300,
                    height:100,
                    store: store, // link store there... 
                    xtype:'gridpanel',
                    layout:'fit',
                    height:400,
                    autoRender:true,
                    autoShow:true,
                    tbar:[
                        {
                            itemId:'add',
                            hideLabel:true,
                            buttonOnly:true,
                            buttonText:'Browse a file',
                            xtype:'filefield'
                        }
                    ],
                    columns:[
                        {
                            dataIndex:'Id',
                            xtype:'gridcolumn',
                            text:'File Id'
                        },
                        {
                            dataIndex:'Title',
                            xtype:'gridcolumn',
                            text:'File Name'
                        }
                    ]
                }
            ];

            me.callParent(arguments);

            //me.down('#grid').store = store;

            me.down('#add').on('change', function (o, e) {
                me.down('#grid').store.add({Id:Ext.id(), Title:o.value, IsUploading:true});
                // store.load(); // remove it - it set data = [] as it was initialized before
            });
        },

        getErrors:function () {
            return [];
        },

        validate:function () {
            return true;
        }});
    Ext.onReady(function () {
        Ext.QuickTips.init();

        var win = new Ext.Window({
            width:500, id:'winid', height:300, layout:'fit', border:false, closable:false, title:'File Upload', items:[
                {
                    xtype:'form', frame:true, labelWidth:100, items:[
                    {
                        name:'Title',
                        xtype:'textfield',
                        fieldLabel:'Title',
                        allowBlank:false,
                        anchor:'100%'
                    },
                    {
                        name:'Attachment',
                        xtype:'attachment',
                        fieldLabel:'Attached Files'
                    }
                ]
                }
            ], buttons:[
                {
                    text:'Submit', handler:function () {
                    Ext.getCmp('form').getForm().submit();
                }
                }
            ]
        });
        win.show();
    });

在这个片段中拍摄。

正如我之前所说,商店未成功与其网格链接。当onchange事件出现时,存储重新加载默认数据= []。 请享用! :)

答案 1 :(得分:0)

不用

尝试
store.load();
on onchange handler。

另外,检查商店。它是否成功链接到商店?

答案 2 :(得分:0)

另外..良好做法是在

上实现嵌套组件和小部件
  

initComponent

方法

这样的东西
  

initComponent:function(){           var me = this;           / * ------ * / me.callParent(arguments); }

并使用

  

Ext.apply

y或

  

Ext.applyIf

用于组件初始化

相关问题