tabalinas / jsgrid

Lightweight Grid jQuery Plugin
http://js-grid.com
MIT License
1.53k stars 351 forks source link

How to disable specific row when grid have edit mode on? #1152

Open ChetanChaudhari03 opened 6 years ago

ChetanChaudhari03 commented 6 years ago

Hi,

I have jsGrid. In this gird edit mode is on. But on some of row i don't want to give for edit. So How can i do this in JSGird?

Please help me for that.

Thanks, Chetan Chaudhari

brichardson2013 commented 6 years ago

Use the editTemplate function when defining the fields. In that function, determine whether the row should be editable or not, and set readonly to true or false accordingly:

    fields: [
        {name: "serviceid", title: "ServiceId", type: "text", visible: false },
        {name: "service", width: 200, title: "Service", type: "text", editing: false },
        {name: "additional_info", width: 300, title: "Additional Info", type: "text", 
        editTemplate: function(value, item) {
                var $result = jsGrid.fields.text.prototype.editTemplate.apply(this, arguments);
                if(item.additionalfield == true)
                {
                    $result.prop("readOnly", false );
                }
                else
                {
                    $result.prop("readonly", true);
                }

                return $result;                    
            }
        },
        {name: "additionalfield", width: 10, title: "Info Required", type: "text", visible: false },
        {type: "control" }
    ]

Another way to do it is to use the onItemEditing function:

    onItemEditing: function(args) {
        if(args.item.additionalfield == false) {
            args.cancel = true;
        }
    },

This will prevent the field from editing at all, whereas the previous answer will still allow the cell to APPEAR to be editable, but the field will actually be read only.

mofrafrank commented 1 year ago

It works for me, you can try this.

{ type: "control", width: 2, deleteButton: true, editButton: true,
itemTemplate: function(value, item) {
            var $result = $([]);

            if(this.editButton) {
                 //add your condition
                if(item.disableedit != true)
                     $result = $result.add(this._createEditButton(item));
            }

            if(this.deleteButton) {
                $result = $result.add(this._createDeleteButton(item));
            }

            return $result;
        }
}