Custom Filter records in Free-jqgrid 4.15.4

2019-08-10 03:17发布

问题:

I have one question regarding custom filters in free jqGrid 4.15.4. I want to implement a search functionality where if I select 'less than but not null or empty' filter then it should show only rows of records which column isn't null or empty. I am able to create custom filter which gives 'is null' or 'isn't null' from this thread. But when I tried to create for mine requirement, I wasn't able to figure out what operator I have to use for 'less than but not null or empty'.

for example, I have used this code sample to create custom filter:

 customUnaryOperations: ["lne"],
 customSortOperations: {
           lne: {
                operand: "<!=''",
                text: "less but not empty",
                filter: function (options) {
                    var v = options.item[options.cmName];
                    if (v !== undefined && v !== "") {
                        return true;
                    }
                }
            }

The above operator I've used in search option toolbar.

searchoptions: {
                     searchOperators: true,
                     sopt: ['eq', 'lt', 'gt','lne'],
                 }, search: true, 

Meanwhile, I don't want to use formatter: "integer" (suggested here) as this only assigns 0 to all null record column cells, and still visible in records whenever one selects 'less than' filter .

For reference, I've created one fiddle which consists of the requirement and two images for more clarity. So, Can anyone help me out on this? I hope I'vent re-asked this again.

Thank you in advance.

回答1:

The code of the filter could be like the following

customSortOperations: {
    lne: {
        operand: "<!=''",
        text: "less but not empty",
        filter: function (options) {
            var v = options.item[options.cmName];

            if (v !== undefined && v !== "" &&
                    parseFloat(v) < parseFloat(options.searchValue)) {
                return true;
            }
        }
    }
}

See modified demo https://jsfiddle.net/OlegKi/x3fger4z/17/