简体   繁体   中英

How to populate jqGrid filter toolbar and search when the page loads (ASP.net webforms)

Currently, I'm trying to populate the filterToolbar with values taken in from a cookie. If there is cookie data for the filters, I want it to fill the respective textboxes and filter the jqGrid for that data.

I'm using ASP.net webforms, so most of my data is initialized already. How/where could I add javascript in order to get this going?

I actually figured out what I was doing.

So what I ended up doing as a solution was adding a timeout function in the document.ready function

$(document).ready(function () {

    // some code

    setTimeout(function () {
        $('#Jqgrid1')[0].triggerToolbar();
    }, 500)

    //some code
}

My guess is that I couldn't use the $('#grid')[0].toggleToolbar() to force it because whenever I tried to use it, it was before the whole grid was finish setting up.

In the ASP webform, I had several functions registered.

<cc1:JQGrid ID="Jqgrid1" runat="server"
        Height="630"
        SearchDialogSettings-Draggable="true"
        EnableViewState="false"
        AutoWidth="True" >

        <ClientSideEvents 
            LoadComplete="Jqgrid1_LoadComplete"
            GridInitialized="initGrid"
            />
 <%-- grid code --%>
 </cc1:JQGrid>

The LoadComplete is executed after the grid is loaded. I tried doing triggering my toolbar there, but didn't work. My guess is, again, it was too early in the grid execution to use the triggerToolbar() function.

The same went for the GridInitialized events (even though both events would seem to imply to me that the grid is done doing its thing... but whatever...)

The way that I read my cookies in was actually in the GridInitialized event handler.

function initGrid() {
var myJqGrid = $(this);
var valueName = 'GridFilters';
var myCookie = document.cookie;
var gridFilterString;
var gridFilterArray;
var currentFilter;
var myCookie_arr;
var myDic = {};
if (myCookie.indexOf(valueName) > -1) { // don't even bother if the cookie isn't there...
    myCookie_arr = myCookie.split("; ");     // looking for the cookie I need
    // read cookies into an array
    for (var i = 0; i < myCookie_arr.length; i++)   
    {
        parts = myCookie_arr[i].split("=");
        first = parts.shift();  // remove cookie name
        myDic[first.trim()] = parts.join("=").trim();   // handles multiple equality expressions in one cookie
    }

    if (myDic.hasOwnProperty("GridFilters"))


        gridFilterString = myDic["GridFilters"];

    if (gridFilterString != "NONE") {
        myFiltersDic = {}
        myFiltersArr = gridFilterString.split("&")
        for (var i = 0; i < myFiltersArr.length; i++) {
            parts = myFiltersArr[i].split("=");
            myFiltersDic[parts[0].trim()] = parts[1].trim();
        }
        myParams = $(this).jqGrid("getGridParam", "postData");

        var filters = []

        for (keys in myFiltersDic) {

            $('#gs_' + keys.trim()).val(myFiltersDic[keys].trim());
        }
        $.cookie('m_blnSearchIsHidden', "0", "/");
        if (!isLoaded)
        {
            $(this)[0].toggleToolbar();
        }
        isLoaded = true;
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM