使不可编辑的列可编辑为新添加的行

时间:2015-05-12 21:00:50

标签: javascript jsp dojo

我创建了增强型网格,其中Column1不可编辑,但用户可以编辑其他列并可以更新。 但是当我添加新行时,新行是空白的,我应该允许编辑并在所有列中键入数据。但由于第一列的可编辑性不正确,我无法编辑新添加的行的column1来提供信息。 请建议我如何为新添加的空白行编辑column1但不能为现有行编辑。请找到小提琴:http://jsfiddle.net/Q9GYv/77/

以下是代码:

require(['dojo/_base/lang', 'dojox/grid/EnhancedGrid', 'dojo/data/ItemFileWriteStore', 'dijit/form/Button', 'dojo/dom', 'dojo/domReady!'],

function (lang, EnhancedGrid, ItemFileWriteStore, Button, dom) {
    /*set up data store*/
    var data = {
          items: [{
            col1 : "John",
            col2 : "aa",
            col3 : "bb",
            col4 : "cC"
         }]
    };

    var store = new ItemFileWriteStore({
        data: data
    });

    /*set up layout*/
    var layout = [
        [{
            'name': 'FirstName',
                'field': 'col1',
                'width': '100px'
        }, {
            'name': 'LastName', editable: 'true',
                'field': 'col2',
                'width': '100px'
        }, {
            'name': 'Designation',editable:'true',
                'field': 'col3',
                'width': '200px'
        }, {
            'name': 'Address',editable:'true',
                'field': 'col4',
                'width': '150px'
        }]
    ];

    /*create a new grid*/
    var grid = new EnhancedGrid({
        id: 'grid',
        store: store,
        structure: layout,
        rowSelector: '20px',
        canEdit: function(inCell, inRowIndex) {
            if(inRowIndex === 0) {
                return true;
            }
            return this._canEdit;
        }
    });

    /*append the new grid to the div*/
    grid.placeAt("gridDiv");

    /*Call startup() to render the grid*/
    grid.startup();


    var button = new Button({
        onClick: function () {
            console.log(arguments);
            store.newItem({
                col1: "",editable:true,
                col2: "",
                col3: "",
                col4: "New Row"
            });

        }
    }, "addRow");
});

- 编辑 -

请建议如何在添加新行时将Column1设为可编辑 ,并在编辑网格中的现有行时只读 。请找到小提琴http://jsfiddle.net/Q9GYv/77/

1 个答案:

答案 0 :(得分:1)

When creating your grid, you can override the canEdit function so that you can specify which rows cannot be modified. See below:

$('div').click(function(){ 
    this.style.backgroundColor = this.style.backgroundColor === 'blue' ? 'red' : 'blue' 
})

You would also need to make your rows editable by default and I would suggest not allowing the 'id' row to be mutable as you're running a ItemFileWriteStore and that doesn't allow for changing an item's ID after it's been inserted.

I've updated your JSFiddle with these updates: http://jsfiddle.net/Q9GYv/75/

New fiddle with column 1 being uneditable for existing rows: http://jsfiddle.net/Q9GYv/78/