ExtJS 列表資料編輯

來源:互聯網
上載者:User

標籤:des   datagridview   style   blog   http   io   ar   color   使用   

         在ExtJs中,GridPanel一般用於展示列表資料。同時利用一些附加的外掛程式也能編輯資料。類似於asp.net中的DataGridView控制項.

展示資料比較簡單,利用Store則可以自動展示,只要在伺服器端將資料Json化即可;

下面在Extjs中編輯列表資料

一、單條編輯

        單條資料的編輯利用了Ext.ux.grid.RowEditor外掛程式達到目的

var gridEditor = new Ext.ux.grid.RowEditor({ listeners: {        ‘canceledit‘: function () {            var re = FieldGrid.getSelectionModel().getSelected();            var id = re.get("Id");            if (id == null) {                var r = FieldStore.getAt(0);                if (Ext.isDefined(r)) {                    FieldStore.remove(r);                }            }        },        ‘beforeedit‘: function () {        },          //賦值
‘beforerecordsubmit‘: function (r, cm, ri) {            r.set(‘Content‘, cm.getCellEditor(1, ri).getValue());            r.set(‘Description‘, cm.getCellEditor(2, ri).getValue());        }    }    });

新增時:

gridEditor.stopEditing();FieldStore.insert(0, new FieldStore.recordType);FieldGrid.getView().refresh();FieldGrid.getSelectionModel().selectRow(0);gridEditor.startEditing(0);

在store中。定義如下:

var FieldStore = new Ext.data.Store({            url: ‘url’,            remoteSort: true,            reader: new Ext.data.JsonReader({                root: ‘data‘,                totalProperty: ‘total‘,                id: ‘Id‘,                fields: [                    {                        name: ‘Id‘,                        type: ‘string‘// ID                    },                    {                        name: ‘Name‘,                        type: ‘string‘                    } ,                    {                        name: ‘Description‘,                        type: ‘string‘                    } ,                    {                        name: ‘CreateTime‘,                        type: ‘date‘                    }                ]            }),            listeners: {                ‘update‘: function (thiz, record, operation) {                    var model = record.data;                    if (operation == Ext.data.Record.EDIT                        && !record.getChanges().Id) {                        Ext.Ajax.request({                            url: ‘url’,                            params: model,                            success: function (response, opts) {                                var result = Ext.util.JSON.decode(response.responseText);                                if (result.success) {                                    if (!record.get(‘Id‘))                                        record.set(‘Id‘, result.id);                                    thiz.commitChanges();                                } else {                                    alert(result.msg);                                    thiz.rejectChanges();                                    if (!record.get(‘Id‘)) //新增                                        thiz.remove(record);                                }                            }                        });                    }                }            }        }    );

在更新時,利用了store組件的update事件進行更新。

 

:

 

二、大量編輯資料

      在開發過程中,一些業務往往需要一次性儲存,如憑證,單據資料,如;批量儲存資料,這時需要使用EditorGridPanel組件。來定義可編輯的地方。

 

 

  

1、grid以利用Cm來定義列及列的相關屬性.定義如下:

var Cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), Sm,    {        header: "ID",        dataIndex: ‘Id‘,        name: ‘Id‘,        sortable: true,        hidden: true    },    {        header: "數量",        dataIndex: ‘Quantity‘,        sortable: true,        width: 80,        editor: {            xtype: ‘numberfield‘,            allowBlank: false,            name: ‘Quantity‘        }    },    {        header: "時間(分鐘)",        dataIndex: ‘WorkingHour‘,        sortable: true,        width: 80,        editor: {            xtype: ‘numberfield‘,            allowBlank: false,            name: ‘Workinghour‘        }    }]);

 

在提交時,

var submit = new Ext.ux.SubmitBtn({    handler: function () {        //grid失去焦點,會將編輯的資料儲存到store,如果不採用些,則最後的資料如果直接提交時,則會丟失        if (RouteSheetFormGrid.activeEditor != null) {            RouteSheetFormGrid.activeEditor.completeEdit();        }                //將所有修改後的資料儲存到數組中。        var arrItems = new Array();        Store.each(            function (r) {                arrItems.push(r.data);            }        );        if (form.getForm().isValid()) {            form.getForm().submit({                url: ‘url‘,                params: {                    //以json方式提交到後台                    ‘record‘: Ext.encode(arrItems)                },                success: function (form, action) {                    win.destroy(); // 關閉視窗                },                failure: function (form, action) {                    alert(action.result.msg);                }            }); // formSubmitArgs            // 引用之前定義的請求參數變數        }    }});

 

 

由於提交時,對象已json化。在後台,需要將json對象化。

ExtJS 列表資料編輯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.