如何使用jqGrid自定义搜索

时间:2016-03-18 09:51:55

标签: jquery asp.net-mvc jqgrid

全部。 我在ASP.NET MVC中使用jqGrid。我为jqGrid表创建了一个js文件,这个文件有getdata(getlist)和CRUD。这对我来说不是问题。 我的问题不是带按钮的控制器中的调用操作。这不适合我。 我的例子是:

$(function () {
    $("#grid").jqGrid({
        url: "/JqGrid/GetList",
        datatype: 'json',
        mtype: 'Get',
        colNames: ['ProductID', 'ProductName', 'UnitPrice'],
        colModel: [
            { key: true, hidden: true, name: 'ProductID', index: 'ProductID', editable: true },
            { key: false, name: 'ProductName', index: 'ProductName', editable: true },
            { key: false, name: 'UnitPrice', index: 'UnitPrice', editable: true }
        ],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [10, 20, 30, 40],
        height: '100%',
        viewrecords: true,
        loadonce: true,
        caption: 'Jqgrid List A.saadati',
        emptyrecords: 'No records to display',
        jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            ProductID: "0"
        },
        autowidth: true,
        multiselect: false
    }).navGrid('#pager', { edit: true, add: true, del: true, search: false, refresh: true },
        {
            // edit options
            zIndex: 100,
            url: '/JqGrid/Edit',
            closeOnEscape: true,
            closeAfterEdit: true,
            recreateForm: true,
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        },
        {
            // add options
            zIndex: 100,
            url: "/JqGrid/Create",
            closeOnEscape: true,
            closeAfterAdd: true,
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        },
        {
            // delete options
            zIndex: 100,
            url: "/JqGrid/Delete",
            closeOnEscape: true,
            closeAfterDelete: true,
            recreateForm: true,
            msg: "Are you sure you want to delete this task?",
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        });


    $('#filterButton').click(function (event) {
        event.preventDefault();
        filterGrid();
    });
});

function filterGrid() {
    var postDataValues = $("#grid").jqGrid('getGridParam', 'postData');

    // grab all the filter ids and values and add to the post object
    $('.filterItem').each(function (index, item) {
        postDataValues[$(item).attr('id')] = $(item).val();
    });

    $('#grid').jqGrid().setGridParam({ postData: postDataValues, page: 1 }).trigger('reloadGrid');
    alert("Fucntion is call with button,it is ok");
}

注意:$('#filterButton').click( ....filterGrid,但filterGrid无效。我有一个文本框和搜索按钮。

我的行动是

public JsonResult GetList(string sidx, string sord, int page, int rows, string ProductName)
{
    int PageIndex = Convert.ToInt32(page) - 1;
    int PageSize = rows;
    var ListResult = db.Products.ToList();
    int TotalRecords = ListResult.Count;
    var TotalPages = (int)Math.Ceiling((float)TotalRecords / (float)rows);
    if (!string.IsNullOrEmpty(ProductName))
    {
        ListResult = ListResult.Where(p => p.ProductName.Contains(ProductName)).ToList();
    }
    if (sord.ToUpper() == "DESC")
    {
        ListResult = ListResult.OrderByDescending(s => s.ProductName).ToList();
        ListResult = ListResult.Skip(PageIndex * PageIndex).Take(PageSize).ToList();
    }
    else
    {
        ListResult = ListResult.OrderBy(s => s.ProductName).ToList();
        ListResult = ListResult.Skip(PageIndex * PageIndex).Take(PageSize).ToList();
    }
    var jsonData = new { total = TotalPages, page, records = TotalRecords, rows = ListResult };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

当然我知道filterGrid函数适用于jqGrid而不是调用top操作。但是这段代码对我不起作用,我尝试用另一种方式用URL调用动作,但它也不起作用。

请帮助我一个方法。谢谢。

0 个答案:

没有答案