2

I have a grid where one column has

... formatter: 'checkbox', edittype: 'checkbox', formatoptions: { disabled: false }

I would like to toggle the disabled value based on other columns for the same row. The result would be a grid with some enabled and some disabled checkboxes.

Is this possible?

I tried putting a function in the formatoptions value - formatoptions: { disabled: somefunction()} but it only got called once at table load and didn't seem to accept any parameters.

ethrbunny
  • 10,379
  • 9
  • 69
  • 131

2 Answers2

3

I recommend you to use to use custom formatter or register your custom formatter like it's described in the answer or in this one and call the original custom formatter inside of it:

(function ($) {
    "use strict";
    $.extend($.fn.fmatter, {
        yourFormatterName: function (cellValue, options,
                rowObject, action) {

            // call formatter: "checkbox"
            return $.fn.fmatter.call(this, "checkbox",
                cellValue, options, rowObject, action);
        }
    });
    $.extend($.fn.fmatter.yourFormatterName, {
        unformat: function (cellValue, options, elem) {
            var cbv = (options.colModel.editoptions != null &&
                        typeof options.colModel.editoptions.value === "string") ?
                            options.colModel.editoptions.value.split(":") :
                            ["Yes","No"];
            ret = $("input", elem).is(":checked") ? cbv[0] : cbv[1];
        }
    });
}(jQuery));

In the way you can change any property of options.colModel before calling the original formatter:

yourFormatterName: function (cellValue, options, rowObject, action) {
    var myValue = true, // or false value DYNAMICALLY
        newOptions = $.extend(true, {
                colModel: { formatoptions: { disabled: myValue } }
            },
            options);
    return $.fn.fmatter.call(this, "checkbox", cellValue, newOptions, rowObject,
        action);
}

where

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hello @Oleg I just tried doing this making minor changes. This doesn't seem to work anymore. Maybe something changed in versions of free-jqGrid. Can you please have a look when you have a second? Thanks. – Dipen Shah Nov 16 '16 at 16:54
  • 1
    @DipenShah: It's better that you share the code which you tried... In any way I created the demo https://jsfiddle.net/OlegKi/wugfty1o/ for you where I uses exact code which I included in the answer and the formatter `formatter: "yourFormatterName"` works. – Oleg Nov 16 '16 at 18:10
  • Many thanks @Oleg. I had a simple misunderstanding and made a simple typo in my code which I fixed by looking at your demo. You are great help. Thanks once again. – Dipen Shah Nov 16 '16 at 20:20
1

I had encountered this issue before. Instead of implementing a custom function in formatoptions, I loop each row of the grid in the loadComplete event and enable/disable the checkbox based on the value of another column. Check this out.

The all checkboxes in third column are based on the value of second Name column.

Wilts C
  • 1,720
  • 1
  • 21
  • 28