`
kidiaoer
  • 浏览: 822013 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Ext.ux.grid.RowEditor的使用

阅读更多
Ext.ux.grid.RowEditor的使用

RowEditor is a normal grid plugin, so you’ll need to instantiate it and add to your grid’s ‘plugins’ property. You also need to define what type of Editor is available (if any) on each column:

var editor = new Ext.ux.grid.RowEditor();
 
var grid = new Ext.grid.GridPanel({
  plugins: [editor],
  columns: [
    {
      header   : 'User Name',
      dataIndex: 'name',
      editor   : new Ext.form.TextField()
    },
    {
      header   : 'Email',
      dataIndex: 'email',
      editor   : new Ext.form.TextField()
    }
  ]
  ... the rest of your grid config here
});
RowEditor defines a few events, the most useful one being ‘afteredit’. Its signature looks like this:

/**
* @event afteredit
* Fired after a row is edited and passes validation.  This event is fired
* after the store's update event is fired with this edit.
* @param {Ext.ux.grid.RowEditor} roweditor This object
* @param {Object} changes Object with changes made to the record.
* @param {Ext.data.Record} r The Record that was edited.
* @param {Number} rowIndex The rowIndex of the row just edited
*/
'afteredit'
All you need to do is listen to that event on your RowEditor and save your model object appropriately. First though, we’ll define the Ext.data.Record that we’re using in this grid’s store:

var User = Ext.data.Record.create([
  {name: 'user_id', type: 'int'},
  {name: 'name',    type: 'string'},
  {name: 'email',   type: 'string'}
]);
And now the afteredit listener itself

editor.on({
  scope: this,
  afteredit: function(roweditor, changes, record, rowIndex) {
    //your save logic here - might look something like this:
    Ext.Ajax.request({
      url   : record.phantom ? '/users' : '/users/' + record.get('user_id'),
      method: record.phantom ? 'POST'   : 'PUT',
      params: changes,
      success: function() {
        //post-processing here - this might include reloading the grid if there are calculated fields
      }
    });
  }
});
The code above simply takes the changes object (which is just key: value object with all the changed fields) and issues a request to your server backend. ‘record.phantom’ returns true if this record does not yet exist on the server – we use this information above to specify whether we’re POSTing to /users or PUTing to /users/123, in line with normal RESTful practices.

Adding a new record
The example above allows for editing an existing record, but how do we add a new one? Like this:

var grid = new Ext.grid.GridPanel({
  //... the same config from above goes here,
  tbar: [
    {
      text   : "Add User",
      handler: function() {
        //make a new empty User and stop any current editing
        var newUser = new User({});
        rowEditor.stopEditing();
 
        //add our new record as the first row, select it
        grid.store.insert(0, newUser);
        grid.getView().refresh();
        grid.getSelectionModel().selectRow(0);
 
        //start editing our new User
        rowEditor.startEditing(0);
      }
    }
  ]
});
Pretty simple stuff – we’ve just added a toolbar with a button which, when clicked, creates a new User record, inserts it at the top of the grid and focusses the RowEditor on it.

Configuration Options
Although not documented, the plugin has a few configuration options:

var editor = new Ext.ux.grid.RowEditor({
  saveText  : "My Save Button Text",
  cancelText: "My Cancel Button Text",
  clicksToEdit: 1, //this changes from the default double-click activation to single click activation
  errorSummary: false //disables display of validation messages if the row is invalid
});
If you want to customise other elements of the RowEditor you probably can, but you’ll need to take a look at the source (it’s not scary).

Final Thought
RowEditor is a really nice component which can provide an intuitive interface and save you writing a lot of CRUD code. It is best employed on grids with only a few columns – for models with lots of data fields you’re better off with a full FormPanel.

I’d be pretty happy to see this included in the default ExtJS distribution, as I find myself returning to it frequently.
分享到:
评论

相关推荐

    ExtJs选中var editor = new Ext.ux.grid.RowEditor详解

    ### ExtJs选中 `var ...以上就是关于 `var editor = new Ext.ux.grid.RowEditor` 的详细解析,希望对使用ExtJs框架的开发者有所帮助。通过合理地配置和使用 `RowEditor`,可以极大地提高Web应用的交互性和用户体验。

    ext的表格行编辑(roweditor)实现(c#)

    - `plugins`属性:添加RowEditor插件,代码示例:`plugins: [ Ext.grid.plugin.RowEditing ]`。 - `clicksToEdit`属性:定义触发编辑所需的点击次数,一般设为1表示单击即编辑。 3. **绑定编辑器** - 对每个列...

    Ext Js权威指南(.zip.001

    Ex4.0共2个压缩包特性,《ext js权威指南》 ...10.3.4 数据汇总功能:ext.grid.featrue.abstractsummary与ext.grid.featrue. summary / 539 10.3.5 分组功能:ext.grid.featrue.grouping / 543 10.3.6 分组汇总...

    Ext+JS高级程序设计.rar

    8.3.2 在CRUD操作中restful的设置以及使用Ext.Direct的问题 247 8.4 ListView控件 248 8.5 本章小结 251 第四部分 Ext 扩展和Ext插件 第9章 Ext 扩展 254 9.1 利用Ext.extend实现继承 254 9.2 与Ext扩展相关的预备...

    Ext 连接数据库的相关操作

    plugins: new Ext.grid.RowEditor() }); ``` 3. **分页**:EXT支持分页,通过配置Store的paging属性为true,并添加PagingToolbar。 ```javascript store.pageSize = 25; // 每页显示25条记录 var pagingToolbar =...

    ext3 gridRowEditor本地数据简单demo,有注释

    开发者可能会使用`Ext.grid.EditorGridPanel`,并为每个列定义一个`editor`属性来启用单元格编辑。同时,Store的配置可能包括数据源(即"data"),以及定义字段的`fields`属性。 在深入理解这个demo的过程中,你...

    EXTJSEXT实例GridPanel.

    var grid = new Ext.grid.GridPanel({ store: store, cm: cm, renderTo: Ext.getBody() // 渲染到页面的body元素 }); ``` 此外,EXTJS的GridPanel还支持许多高级特性,例如: - **行编辑**: 可以通过添加...

    ExtJS 表格面板GridPanel完整例子

    - **行编辑**: 使用`RowEditor`插件实现行内编辑。 - **行选择**: 可以配置`selModel`来实现单选或多选行。 - **扩展功能**: 如`ColumnResizing`(列宽调整)、`ColumnMoving`(列移动)等。 在实际项目中,你可能...

    EXTJS记事本 当CompositeField遇上RowEditor

    在这个场景中,开发者面临的问题是如何在EXTJS的RowEditor中处理由多个Combobox(下拉框)组成的CompositeField,以便实现级联筛选功能。RowEditor是EXTJS中的一个组件,它允许用户直接在表格行上编辑数据,提供了...

    extjs demo程序

    1. **RowEditor.js**: 这个文件是ExtJS中的Row Editor组件,它允许你在数据网格的每一行上进行编辑。RowEditor提供了一种方便的方式来编辑表格中的记录,通常适用于需要快速更新大量数据的情况。它将单元格转换为...

    基于primefaces对table的增删改查的源码

    3. **修改(Edit)**: PrimeFaces的`<p:cellEditor>`或`<p:rowEditor>`组件允许在表格行内直接编辑数据。当用户激活编辑模式时,单元格变为可编辑,用户可以更新字段值。编辑完成后,提交更改至后台进行处理。 4. *...

    jsf primefaces datatable

    - **行编辑**: 通过`p:rowEditor`可以启用行编辑,使用户能直接在表格中修改数据。 - **扩展功能**: PrimeFaces还提供了诸如导出、拖放、扩展列等功能,极大地丰富了`DataTable`的使用场景。 6. **最佳实践** - ...

Global site tag (gtag.js) - Google Analytics