问题描述
一个关于获取record的id 的问题,请看下面删除的函数deleteUserUserList = Ext.extend(Ext.Panel, { layout: 'fit', autoWidth: true, store: 'UserStore', initComponent: function() { this.items = [ { xtype: 'editorgrid', id:'grid_id', store: 'UserStore', border: false, loadMask: true, view: new Ext.grid.GridView({forceFit:true}), selModel: new Ext.grid.CheckboxSelectionModel({width:20}), columns: [new Ext.grid.RowNumberer({width: 30}), { xtype: 'gridcolumn', header: '姓名', sortable: true, dataIndex: 'name', width: 100, editor: {xtype: 'textfield'} }, { xtype: 'gridcolumn', header: '电子邮箱', sortable: true, dataIndex: 'email', width: 150, editor: {xtype: 'textfield'} }, { xtype: 'gridcolumn', header: '用户名', sortable: true, dataIndex: 'username', editable:false, width: 100, editor: {xtype: 'textfield'} }, { xtype: 'booleancolumn', dataIndex: 'enable', header: '可用', sortable: true, width: 100, editor: { xtype: 'checkbox', boxLabel: '是否可用' } } ] } ]; this.bbar = { xtype: 'paging', pageSize: 10, store: 'UserStore', displayInfo: true, displayMsg: '显示第{0}条到{1}条记录,一共{2}条', emptyMsg:'没有记录', items: [ { xtype: 'button', text: '提交更新', handler:this.saveUser } ] }; this.tbar = { xtype: 'toolbar', items: [ { xtype: 'tbfill' }, { xtype: 'tbseparator' }, { xtype: 'button', id: 'deleteUser', text: '删除', handler:this.deleteUser }, { xtype: 'tbseparator' } ] }; UserList.superclass.initComponent.call(this); }, deleteUser :function() {var userIds = Ext.getCmp("grid_id").selModel.selections.keys; //问题出在这里:我想获取被选取的record的id,但获得的Id值如ext-record-21,ext-record-22.... // 看网上好多人用var userIds = this.selModel.selections.keys,在这里我不行,所以用getCmp来获取表格对象,可能是这个的问题,但不确定。请高手可以帮忙看一下,谢谢, if (userIds.length <= 0) { Ext.MessageBox.alert('提示', '请选择要删除的数据'); return; } var submitFun = function(buttonId, text, opt) { if (buttonId == 'yes') { micrite.util.ajaxRequest({ url: 'DeleteUser.action', params:{'userIds':userIds}, success:function(r,o){ //var res = Ext.decode(r.responseText); //if (res && res.success){ this.store.reload(); //} }, failure:Ext.emptyFn },this); } }; Ext.Msg.show({ title:'删除操作', msg: '是否删除', buttons: Ext.Msg.YESNO, scope: this, fn: submitFun //icon: Ext.MessageBox.QUESTION });}, 问题补充:感谢各位的回答,我也做过了测试,各位的都是可以采纳的,而且可行的。我上面的代码大部分是用extjs designer生成的,我自己做了一点修改。问题就想下面的大哥说的我没有在ds中定义id,我加多了一个id的属性,就可以获取了,我是用this.selModel.selections.keys; 其实就是可以得到一个以id组成的数组,中间也是用逗号隔开的,只要把它作为参数,传到后台,后台用以下代码就可以实现多行删除了public void delete(int[] userIds) { for (int i = 0; i < userIds.length; i++) { User user = userDAO.get(userIds[i]); userDAO.delete(user); } }不是我写的,是我看别的项目里是这样用的,觉得不错就借用了。当然this.selModel.selections.keys;的用法,我也不是很了解,看对大伙有没有用,如果有哪位对它比较了解的,可以分享一下
解决方案
看你的代码应该是一次删除多行:首先应取到所选中的行:var m = this.grid.getSelections();var store = this.ds;其次再在ajax中,对所选的行在页面删除,并在后台进行删除操作:......var userIds = "";for (var i = 0, len = m.length;i < len; i++){ var ss = m[i].get("grid_id"); userIds = jsonData + ss + ","; store.remove(m[i]); }........url: 'DeleteUser.action', params:{'userIds':userIds},...........如果是删除一行,那就直接取即可:var userIds = this.grid.getSelections().get("grid_id");
解决方案二:
mark一下,楼上的回答的都不错。。我想知道的,多选情况下,我如何从record获取选择的记录总数,我通过var record = this.grid.getSelectionModel().getSelected();record.length()获取不到记录总数..
解决方案三:
跟Ext.getCmp()没关系,主要是看你Record的id配置
解决方案四:
之前说的小例子 <script type="text/javascript">Ext.onReady(function(){var sm =new Ext.grid.CheckboxSelectionModel(); var cm = new Ext.grid.ColumnModel([sm, {header:'编号',dataIndex:'id',sortable:true}, {header:'名称',dataIndex:'name',sortable:true}, {header:'操作',dataIndex:'descn',sortable:true} ]); var data = { count:5, "rows":[{id:'1',name:'name1d',descn:'descn1'},{id:'2',name:'name1d',descn:'descn1'},{id:'4',name:'name1d',descn:'descn1'},{id:'5',name:'name1d',descn:'descn1'},{id:'3',name:'name2',descn:'descn2'} ] }; var reader = new Ext.data.JsonReader({ totalProperty: 'count', root: 'rows', id:"id", fields:[ {name: 'id',mapping:"id"}, {name: 'name',mapping:"name"}, {name: 'descn',mapping:"descn"} ]}) var store = new Ext.data.Store({ proxy: new Ext.data.MemoryProxy(data), reader: reader }); var grid = new Ext.grid.GridPanel({ height: 200, applyTo: 'grid', width :310, store: store, cm: cm,sm:sm, frame:"true" }); store.load(); Ext.get("get").on("click",function(){ var s = grid.getSelectionModel().getSelected().id; alert(s); })}); </script> </head> <body> <div id="grid"></div> <input type="button" value="选择一行后 点我" id="get"> </body></html>
解决方案五:
我觉得如果你的store定义了id属性的话(表明哪个字段是 唯一标识的那个)可以这样 Ext.getCmp("grid_id").getSelectionModel().getSelected().id即可获得