首页 新闻 会员 周边

EXTJS 4.2 对表格进行增加操作

0
悬赏园豆:20 [已解决问题] 解决于 2013-07-25 15:44
Ext.define('MyApp.view.homepage.ConsoleFirstLevelAdvert' ,{
    extend: 'Ext.grid.Panel',
    alias: 'widget.consolefirstleveladvert',
    requires:['Ext.toolbar.Paging','Ext.data.*','Ext.util.*','Ext.grid.*','Ext.state.*'],
    id:'homepageconsolefirstleveladvert',
    closable:true,
    initComponent: function() {
        var rowEditing = Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToMoveEditor: 1,
            autoCancel: false
        });
        var store = new Ext.data.JsonStore({
            storeId: 'listconsolefirstleveladvert',
            pageSize:2,
            proxy: {
                type: 'ajax',
                api: {
                     read: 'listconsolefirstleveladvert.do',
                     update: 'update_consolefirstleveladvert.do',
                 },
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty :'totalProperty'
                },
                writer: {
                    type: 'json',  
                    allowSingle: false,
                    writeAllFields: true,    
                    root: 'data'
                }
            },
            autoSync: true,
            autoLoad: {start: 0, limit: 2},
            fields: ['productId', 'productTitle', 'productName','productPrice','pictureUrl', 'productDeposit', 'categoryId','url']
        });
        this.plugins=[rowEditing];
        this.store =store; 
        this.columns = [
                        {header: '编号',  dataIndex: 'productId', hidden:true, flex: 1},
                        {header: '产品标题', dataIndex: 'productTitle',editor:'textfield', flex: 1},
                        {header: '产品名称', dataIndex: 'productName', editor: 'textfield',flex: 1},
                        {header: '产品价格',  dataIndex: 'productPrice', editor: 'numberfield',flex: 1},
                        {header: '图片地址', dataIndex: 'pictureUrl',editor: 'textfield', flex: 1},
                        {header: '商品库存', dataIndex: 'productDeposit', flex: 1},
                        {header: '类目ID',  dataIndex: 'categoryId', flex: 1},
                        {header: '详细页面', dataIndex: 'url', editor: 'textfield',flex: 1}
                    ];
        this.dockedItems = [{
              xtype: 'pagingtoolbar',
              store: store,   // same store GridPanel is using
              dock: 'bottom',
              displayInfo: true,
              displayMsg: '显示 {0} - {1} 条,共计 {2} 条',
              emptyMsg: "没有数据"
        },{
            xtype: 'toolbar',
                dock: 'top',
                items: [
                { 
                   xtype: 'button', 
                   text: '增加' ,
                   handler:function(){
                       rowEditing.cancelEdit();
//                     var panelModel = Ext.getCmp('homepageconsolefirstleveladvert').getSelectionModel();
//                     var panelModel = this.up("panel").getSelectionModel();
                     var panelModel = this.up("panel").getSelectionModel();
                     store.insert(0,panelModel);
                     rowEditing.startEdit(0, 0); 
                } 
                },
                { xtype: 'button', text: '删除' },
                { xtype: 'button', text: '修改' ,handler:function(){
                    store.sync();
                    store.reload();
                }
                }]
        }];
       
        this.callParent();
    },
    
    listeners : {
        itemdblclick: function(dv, record, item, index, e) {
        }
    },
});

代码如上所示,异常情况如下:

点击添加按钮时运行到这行代码: store.insert(0,panelModel);时报错:TypeError: q is undefined ext-all.js(第21行);

谢安1的主页 谢安1 | 初学一级 | 园豆:186
提问于:2013-07-25 09:35
< >
分享
最佳答案
0

var _rs = new Ext.data.Record({productId:'',productTitle:'',productName:'',productPrice:'',pictureUrl:'',productDeposit:'',categoryId:'',url:''});

this.getStore().add(_rs) ;

 
收获园豆:7
dd_冬 | 初学一级 |园豆:138 | 2013-07-25 10:05

大哥,你那个定义变量的代码我允许就报错说:TypeError: n.fields is undefined 

谢安1 | 园豆:186 (初学一级) | 2013-07-25 10:35

@xiezhong: 我不是男的。你可以叫姐!我刚才没看到你的这个是4.2的版本,看了楼上的评论才知道的,不好意思哈!这个是在3.0里面是这样写的!

dd_冬 | 园豆:138 (初学一级) | 2013-07-25 10:52

@xiezhong: 我没有玩儿过4.2版本,我在网上搜了一下,看到这个例子是4.0的。里面有两行新增的代码,你试一下看可以吗

 

http://hi.baidu.com/peter_zk/item/ba2ff0538d4aa1aeacc85781

dd_冬 | 园豆:138 (初学一级) | 2013-07-25 11:41
其他回答(1)
0

var newRecord = new this.Model(rowData);//rowData就是初始化的数据
this.Store.add(newRecord);

收获园豆:7
菜鸟的故事 | 园豆:26 (初学一级) | 2013-07-25 09:53

this 是你的grid

支持(0) 反对(0) 菜鸟的故事 | 园豆:26 (初学一级) | 2013-07-25 09:54

rowData该如何初始化呢?我是新手,第一次接触EXT

支持(0) 反对(0) 谢安1 | 园豆:186 (初学一级) | 2013-07-25 10:28

@xiezhong: 类似于楼下的:{productId:'',productTitle:'',productName:'',productPrice:'',pictureUrl:'',productDeposit:'',categoryId:'',url:''}

支持(0) 反对(0) 菜鸟的故事 | 园豆:26 (初学一级) | 2013-07-25 10:32

@菜鸟的故事: 楼下的那个定义变量的代码我运行的时候也报错:TypeError: n.fields is undefined

 

还有我查了EXT4.2的API里面好像没有看见Ext.data.Record这个类!这是怎么回事呀?

支持(0) 反对(0) 谢安1 | 园豆:186 (初学一级) | 2013-07-25 10:36

@xiezhong: 好像4.0以后就把这个类取消了,改成Model了,相当于表结构

支持(0) 反对(0) 菜鸟的故事 | 园豆:26 (初学一级) | 2013-07-25 10:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册