javascriptjqgridmvcgridmvcjqgrid

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


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

  • 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);
    
        }
    }