extjsextjs4.2

Hide action column items in ext js


I want to hide action column items on condition, Please look below code.

{
 xtype: 'actioncolumn',
 flex: 1,
 sortable: false,
 menuDisabled: true,
 align: 'center',
 items: [{

     icon: (fleet.rolerightscreate.modulerights('Editmanagevehicle', 'Manage Vehicle', 'E') != null) ? FLEET_SERVER_URL + 'images/edit2.png' : '',
     tooltip: fleet.Language.get('_FLEET_VEHICLE_VEHICLELIST_EDIT_'),
     scope: this,

     handler: function(grid, rowIndex, colIndex) {

         //var data = Vehiclestore.data.items[rowIndex].data;
         var rec = grid.getStore().getAt(rowIndex);
         console.log(rec);
         if (fleet.rolerightscreate.modulerights('Editmanagevehicle', 'Manage Vehicle', 'E') != null) {
             this.fireEvent("ShowVehicleDetails", rec);
         }
     }
 }, {
     xtype: 'spacer'
 }, {
     icon: (Ext.getStore('userStore').first().get('issuperadmin') == 1 ? FLEET_SERVER_URL + 'images/del.png' : ''),
     //(fleet.rolerightscreate.modulerights('Deletemanagevehicle', 'Manage Vehicle', 'D') != null) ? FLEET_SERVER_URL + 'images/del.png' : '',  // Use a URL in the icon config  

     tooltip: fleet.Language.get('_FLEET_VEHICLE_VEHICLELIST_DELETE_'),
     handler: function(grid, rowIndex, colIndex) {
         var rec = grid.getStore().getAt(rowIndex);
         if (fleet.rolerightscreate.modulerights('Deletemanagevehicle', 'Manage Vehicle', 'D') != null) {
             Ext.Msg.show({
                 title: fleet.Language.get('_FLEET_VEHICLE_VEHICLELIST_REMOVETITLE_'),
                 msg: fleet.Language.get('_FLEET_VEHICLE_VEHICLELIST_REMOVEMSG_'),
                 buttons: Ext.Msg.YESNO,
                 icon: Ext.Msg.QUESTION,
                 callback: function(button) {
                     if (button == fleet.Language.get('_FLEET_VEHICLE_VEHICLELIST_YESBTN_')) {
                         me.removeVehicle(rec);
                     }
                 }
             });
         }
     }
 }]
}

In above code, Action column contain 3 Items, Edit, space (i.e. { xtype: 'spacer' }), and Delete Icons. I want to hide Delete Icon on condition. Means delete option shown only when admin user login. please look design for this, you can get better idea about problem

enter image description here

Using this code bellow I am able to hide Delete icon but tooltip and click event still fire on click on hidden icon location.

icon: (Ext.getStore('userStore').first().get('issuperadmin') == 1 ? FLEET_SERVER_URL + 'images/del.png' : ''),

enter image description here


Solution

  • I don't think hiding or disabling the icon is the solution here. If the user is not admin why bother with adding the icon at all. I would buildActionColumnItems method that will return the items for the action column based on current user's role.

    The action column config:

    {
        xtype: 'actioncolumn',
        flex: 1,
        sortable: false,
        menuDisabled: true,
        align: 'center',
        items: this.buildActionColumnItems()
    }
    

    The buildActionColumnItems method implementation:

    buildActionColumnItems: function () {
        //set the implicit items
        var columnItems = [{
            icon: (fleet.rolerightscreate.modulerights('Editmanagevehicle', 'Manage Vehicle', 'E') != null) ? FLEET_SERVER_URL + 'images/edit2.png' : '',
            tooltip: fleet.Language.get('_FLEET_VEHICLE_VEHICLELIST_EDIT_'),
            ...
        }];
    
        //add aditional items if the user is super admin
        if (Ext.getStore('userStore').first().get('issuperadmin') == 1) {
            columnItems.push({
                xtype: 'spacer'
            });
            columnItems.push({
                icon: FLEET_SERVER_URL + 'images/del.png',
                tooltip: fleet.Language.get('_FLEET_VEHICLE_VEHICLELIST_DELETE_'),
                ...
            });
        }
    
        return columnItems;
    }