Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making certain cells of an ExtJS GridPanel un-editable

I currently have a GridPanel with the Ext.ux.RowEditor plugin. Four fields exist in the row editor: port, ip address, subnet and DHCP. If the DHCP field (checkbox) of the selected row is checked, I need to make the other three fields un-editable.

I've been trying to perform this code when the beforeedit event is triggered, but to no avail... I've only found ways to make the entire column un-editable. My code so far:

this.rowEditor.on({
    scope: this,
    beforeedit: this.checkIfEditable
});

checkIfEditable:function(rowEditor, rowIndex) {
    if(this.getStore().getAt(rowIndex).get('dhcp')) {
        // this function makes the entire column un-editable:
        this.getColumnModel().setEditable(2, false); 

        // I want to make only the other three fields of the current row 
        // uneditable.
    }
 }

Please let me know if any clarification is needed.

Any help potentially extending RowEditor to accomplish the target functionality would be greatly appreciated as well!

like image 336
Alex Wood Avatar asked Feb 16 '10 00:02

Alex Wood


3 Answers

You can add into RowEditor.js within the function startEditing the call to the function isCellEditable

//.... RowEditor.js
startEditing: function(rowIndex, doFocus){
//.... search for the starting of the below loop into the source code
            var cm = g.getColumnModel(), fields = this.items.items, f, val;
            for(var i = 0, len = cm.getColumnCount(); i < len; i++){
                val = this.preEditValue(record, cm.getDataIndex(i));
                f = fields[i];
                f.setValue(val);

                // *** here add a call to isCellEditable *** //
                f.setDisabled(!cm.isCellEditable(i, rowIndex));
                // ***************************************** //

                this.values[f.id] = Ext.isEmpty(val) ? '' : val;
            }
//....

Then override the isCellEditable of your column model and disabled the field based on you condition:

YYY.getColumnModel().isCellEditable = function(colIndex, rowIndex){
 if (grid.getStore().getAt(rowIndex).get('dhcp')) {
    // you can do more check base on colIndex
    // only to disabled the one you want
    return false; 
  }
  return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, colIndex, rowIndex);
}
like image 86
Patrick Avatar answered Nov 07 '22 17:11

Patrick


As the docs state:

If the listener returns false the editor will not be activated.

So...

this.rowEditor.on({
      scope: this,
     beforeedit: this.checkIfEditable
});

checkIfEditable:function(rowEditor, rowIndex) {
         if(this.getStore().getAt(rowIndex).get('dhcp')) {

             return false; 

         }
 }

Simply returning false will be enough to cancel the editing ability.


Gotcha.

Interesting idea - a bit of a hassle to implement, but possible.

You need to approach this from two directions:

1 ) edit starts

2 ) checkbox is checked/unchecked

For the first part, I think you could use almost the same code I have above, remove the 'return false' and use the reference to the rowEditor to loop through the items collection, disabling (call the disable method on them) the fields that are not your checkbox field.

The second part of this is to add a handler to the checkbox which would do the same thing.

like image 23
Shea Frederick Avatar answered Nov 07 '22 17:11

Shea Frederick


The following works to make a specific cell uneditable (add the event to the roweditor):

    beforeedit: function (roweditor, rowIndex) {
        var data = roweditor.grid.store.getAt(rowIndex).data;
        var cm = roweditor.grid.getColumnModel();

        // disable the first column (can not be edited)
        if (** make your check here ***) {
            var c = cm.getColumnAt(0);
            c.editor.disable();
        }
        roweditor.initFields();
    }
like image 2
Allie Avatar answered Nov 07 '22 17:11

Allie