jqGrid Filter Toolbar initial default value

前端 未结 5 1985
天涯浪人
天涯浪人 2020-12-14 22:02

I\'m using jqGrid with the filter toolbar, i need to set an initial default filter value to one of the fields so that only rows with status \'Open\' are displayed by default

5条回答
  •  一向
    一向 (楼主)
    2020-12-14 22:09

    After reading through Ziege's answer, I thought about what was happening there. I came up with a much simpler way to get default values initialized before the first request goes to the server.

    Here's a full working example. The idea is that there is a column filter with a drop-down of statuses, "Active" and "Closed", where I want the default to be "Active". The code has comments to explain what's happening:

    $('#deals').jqGrid({
        colNames: ['...','Status','...'],
        colModel: [
            { ... },
            // Use the defaultValue attribute to set your defaults in the searchOptions object
            { name: 'Status', stype: 'select', searchoptions: { defaultValue: 'Active', value: {"":"All","Active":"Active","Closed":"Closed",}, sopt: [ 'eq'] }, width: 60 },
            { ... }
        ],
        // Here's where we intercept each server request to cancel it if it's the first one. 
        // Returning false from this method causes the request to the server to be aborted.
        beforeRequest: function () {
            // Define a local reference to the grid
            var $requestGrid = $(this);
            // Check a data value for whether we've completed the setup. 
            // This should only resolve to true once, on the first run.
            if ($requestGrid.data('areFiltersDefaulted') !== true) {
                // Flip the status so this part never runs again
                $requestGrid.data('areFiltersDefaulted', true);
                // After a short timeout (after this function returns false!), now
                // you can trigger the search
                setTimeout(function () { $requestGrid[0].triggerToolbar(); }, 50);
                // Abort the first request
                return false;
            }
            // Subsequent runs are always allowed
            return true;
        },
        url: 'Url/to/your/action',
        datatype: 'json',
        mtype: 'POST',
        pager: '#deals-pager',
        rowNum: 15,
        sortname: 'Status',
        sortorder: 'desc',
        viewrecords: true,
        height: '100%'
    }).jqGrid('filterToolbar', { stringResult: true });
    

    This also works with Lib.Web.Mvc library (.NET) which doesn't support the local datatype.

    If you have multiple grids, or want to move the beforeRequest logic to a library for sharing, simply define it as a standalone function and reference it by name in your grid setup:

    function jqGridFilterSetupRequestHandler = function () {
        var $requestGrid = $(this);
        if ($requestGrid.data('areFiltersDefaulted') !== true) {
            $requestGrid.data('areFiltersDefaulted', true);
            setTimeout(function () { $requestGrid[0].triggerToolbar(); }, 50);
            return false;
        }
        return true;
    }
    
    $('#deals').jqGrid({
        colNames: ['...','Status','...'],
        colModel: [
            { ... },
            // Use the defaultValue attribute to set your defaults in the searchOptions object
            { name: 'Status', stype: 'select', searchoptions: { defaultValue: 'Active', value: {"":"All","Active":"Active","Closed":"Closed",}, sopt: [ 'eq'] }, width: 60 },
            { ... }
        ],
        beforeRequest: jqGridFilterSetupRequestHandler,
        url: 'Url/to/your/action',
        datatype: 'json',
        mtype: 'POST',
        pager: '#deals-pager',
        rowNum: 15,
        sortname: 'Status',
        sortorder: 'desc',
        viewrecords: true,
        height: '100%'
    }).jqGrid('filterToolbar', { stringResult: true });
    

提交回复
热议问题