jqGrid: using beforeProcessing to populate filterToolbar selection boxes

It seems to me that you use already almost correct code. The most problem is that you need refresh existing filter toolbar. You can use destroyFilterToolbar method which I suggested in the answer. I suggested it later to trirand (see here and the pull request) and it’s included in the main code of jqGrid now. Your code can be like below.

beforeProcessing: function (data) {
    var $self = $(this),
        newProductValues = data.pVals,
        newEnvironmentValues = data.eVals,
        newTypeValues = data.tVals,
        cmProduct = $self.jqGrid("getColProp, "Product"),
        cmEnvironment = $self.jqGrid("getColProp, "Environment"),
        cmType = $self.jqGrid("getColProp", "Type"),
        isChanged = false;

    if (cmProduct.editoptions.value !== newProductValues) {
        $self.jqGrid("setColProp", "Product", {
            searchoptions: { value: ":All;" + newProductValues },
            editoptions: { value: newProductValues }
        });
        isChanged = true;
    }
    if (cmEnvironment.editoptions.value !== newEnvironmentValues) {
        $self.jqGrid("setColProp", "Environment", {
            searchoptions: { value: ":All;" + newEnvironmentValues },
            editoptions: { value: newEnvironmentValues }
        });
        isChanged = true;
    }
    if (cmType.editoptions.value !== newTypeValues) {
        $self.jqGrid("setColProp", "Environment", {
            searchoptions: { value: ":All;" + newTypeValues },
            editoptions: { value: newTypeValues }
        });
        isChanged = true;
    }
    if (isChanged) {
        // recreate filter toolbar to refresh the data
        $self.jqGrid("destroyFilterToolbar");
        $self.jqGrid("filterToolbar", {
            stringResult: true,
            searchOnEnter: true,
            searchOperators: true,
            defaultSearch: "cn"
        });
    }
}

(I included new searchOperators: true option which could be intresting)

You can combine the solution with the call of the function refreshSerchingToolbar which I described in the answer to load old filter in the filter toolbar.

By the way you can consider to change format of value property which you use. Instead of usage string form "Product1:Product1;Product2:Product2;etc:etc" you can use object form {Product1: "Product1", Product2:"Product2", etc: "etc"}.

Leave a Comment