Ext4.2学习之GridPanel行编辑功能

首先定义一个Ext.grid.plugin.RowEditing类型的编辑器
在这里插入图片描述
然后在表格的plugins属性当中添加即可。
在这里插入图片描述
结果如下所示
在这里插入图片描述
对应源代码

<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link type="text/css" rel="stylesheet" href="ext-4/resources/css/ext-all.css"/>
    <script type="text/javascript" src="ext-4/ext-all.js"></script>
    <script type="text/javascript" src="ext-4/locale/ext-lang-zh_CN.js"></script>
    <script>
        Ext.onReady(function () {
            // Define our data model
            Ext.define('Employee', {
                extend: 'Ext.data.Model',
                fields: [
                    'name',
                    'email',
                    {name: 'start', type: 'date', dateFormat: 'n/j/Y'},
                    {name: 'salary', type: 'float'},
                    {name: 'active', type: 'bool'}
                ]
            });

            // Generate mock employee data
            var data = (function () {
                var lasts = ['Jones', 'Smith', 'Lee', 'Wilson', 'Black', 'Williams', 'Lewis', 'Johnson', 'Foot', 'Little', 'Vee', 'Train', 'Hot', 'Mutt'],
                    firsts = ['Fred', 'Julie', 'Bill', 'Ted', 'Jack', 'John', 'Mark', 'Mike', 'Chris', 'Bob', 'Travis', 'Kelly', 'Sara'],
                    lastLen = lasts.length,
                    firstLen = firsts.length,
                    usedNames = {},
                    data = [],
                    // s = new Date(2020, 1, 3),
                    now = new Date(),
                    s = Ext.Date.add(now, Ext.Date.MONTH, -20);
                    getRandomInt = Ext.Number.randomInt,

                    generateName = function () {
                        var name = firsts[getRandomInt(0, firstLen - 1)] + ' ' + lasts[getRandomInt(0, lastLen - 1)];
                        if (usedNames[name]) {
                            return generateName();
                        }
                        usedNames[name] = true;
                        return name;
                    };

                while (s.getTime() < now.getTime()) {
                    var ecount = getRandomInt(0, 3);
                    for (var i = 0; i < ecount; i++) {
                        var name = generateName();
                        data.push({
                            start: Ext.Date.add(Ext.Date.clearTime(s, true), Ext.Date.DAY, getRandomInt(0, 27)),
                            name: name,
                            email: name.toLowerCase().replace(' ', '.') + '@sencha-test.com',
                            active: getRandomInt(0, 1),
                            salary: Math.floor(getRandomInt(35000, 85000) / 1000) * 1000
                        });
                    }
                    s = Ext.Date.add(s, Ext.Date.MONTH, 1);
                }

                return data;
            })();

            // create the Data Store
            var store = Ext.create('Ext.data.Store', {
                // destroy the store if the grid is destroyed
                autoDestroy: true,
                model: 'Employee',
                proxy: {
                    type: 'memory'
                },
                data: data,
                sorters: [{
                    property: 'start',
                    direction: 'ASC'
                }]
            });

            var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
                clicksToMoveEditor: 1,
                autoCancel: false
            });

            // create the grid and specify what field you want
            // to use for the editor at each column.
            var grid = Ext.create('Ext.grid.Panel', {
                store: store,
                columns: [{
                    header: 'Name',
                    dataIndex: 'name',
                    flex: 1,
                    editor: {
                        // defaults to textfield if no xtype is supplied
                        allowBlank: false
                    }
                }, {
                    header: 'Email',
                    dataIndex: 'email',
                    width: 160,
                    editor: {
                        allowBlank: false,
                        vtype: 'email'
                    }
                }, {
                    xtype: 'datecolumn',
                    header: 'Start Date',
                    dataIndex: 'start',
                    width: 90,
                    editor: {
                        xtype: 'datefield',
                        allowBlank: false,
                        format: 'm/d/Y',
                        minValue: '01/01/2006',
                        minText: 'Cannot have a start date before the company existed!',
                        maxValue: Ext.Date.format(new Date(), 'm/d/Y')
                    }
                }, {
                    xtype: 'numbercolumn',
                    header: 'Salary',
                    dataIndex: 'salary',
                    format: '$0,0',
                    width: 90,
                    editor: {
                        xtype: 'numberfield',
                        allowBlank: false,
                        minValue: 1,
                        maxValue: 150000
                    }
                }, {
                    xtype: 'checkcolumn',
                    header: 'Active?',
                    dataIndex: 'active',
                    width: 60,
                    editor: {
                        xtype: 'checkbox',
                        cls: 'x-grid-checkheader-editor'
                    }
                }],
                renderTo: 'grid',
                width: 600,
                height: 400,
                title: 'Employee Salaries',
                frame: true,
                tbar: [{
                    text: 'Add Employee',
                    iconCls: 'employee-add',
                    handler: function () {
                        rowEditing.cancelEdit();

                        // Create a model instance
                        var r = Ext.create('Employee', {
                            name: 'New Guy',
                            email: 'new@sencha-test.com',
                            start: new Date(),
                            salary: 50000,
                            active: true
                        });

                        store.insert(0, r);
                        rowEditing.startEdit(0, 0);
                    }
                }, {
                    itemId: 'removeEmployee',
                    text: 'Remove Employee',
                    iconCls: 'employee-remove',
                    handler: function () {
                        var sm = grid.getSelectionModel();
                        rowEditing.cancelEdit();
                        store.remove(sm.getSelection());
                        if (store.getCount() > 0) {
                            sm.select(0);
                        }
                    },
                    disabled: true
                }],
                plugins: [rowEditing],
                listeners: {
                    'selectionchange': function (view, records) {
                        grid.down('#removeEmployee').setDisabled(!records.length);
                    }
                }
            });
        });
    </script>
</head>
<body>
<div id="grid"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lang20150928

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值