how to add custom filter parameter to MvcJqGrid postData which allready has filter on beforeRequest?

StackOverflow https://stackoverflow.com/questions/15153261

سؤال

I have a simple jqgrid

@(Html.Grid("something")
  .SetCaption("")
  .AddColumn(new Column("ID").SetHidden(true))
  .AddColumn(new Column("Name").SetLabel("Name").SetSearch(true))
  .SetUrl(Url.Action(something))
)

I have var SomeOtherID = 2 in javascript. I need to add this SomeOtherID to filter parameters when grid loads (in fact there is many grids on same page and they all need this) without loosing default seatch capability. I suspect I need add this function on .OnBeforeRequest("addSomeOtherID()") but what to do inside

function addSomeOtherID(grid)
{ 
var pd = grid.getGridParams("postData");
?????? here I need to add SomeOtherID to postData filters
grid.setGridParams({postData: pd});
}
هل كانت مفيدة؟

المحلول

Solution was simple. As I suspected .OnBeforeRequest("addSomeOtherID(grid)") did the trick.

function addGridSearchOption(grid, fieldName, fieldValue) {        
    if (grid[0].p.search == false) {
        f = { groupOp: "OR", rules: [] };
        f.rules.push({ field: fieldName, op: "cn", data: fieldValue });
        grid[0].p.search = true;
        $.extend(grid[0].p.postData, { filters: JSON.stringify(f) });
    }
    else {
        tFilters = eval('(' + grid[0].p.postData.filters + ')');
        var bAddRule = true;
        var i = 0;
        for (var tRule in tFilters.rules)
        {
            if (tFilters.rules[i].field == fieldName)
            {
                tFilters.rules[i].data = fieldValue;
                bAddRule = false;
            }
            i++;
        }
        if (bAddRule) {
            tFilters.rules.push({ field: fieldName, op: "cn", data: fieldValue });
        }grid[0].p.postData.filters = JSON.stringify(tFilters);

    }
}

نصائح أخرى

With MvcJqgrid it's possible to set the default search value for a column, an example here:

http://playground.webpirates.nl/MvcJqGrid/Home/DefaultSearchValue

If you look at the source of the page, you can see how it's done.

This is great, I used it and it works. Not sure why @SpokeST says it's not usable, I don't have the search toolbar enabled and it works for me.

One fix for future readers, notice line 4 in the code assigns 'data' variable with "2" instead of fieldValue param. I guess that's leftovers from testing it..

Thanks again!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top