0

我正在使用 JQuery 网格在页面上显示一些数据。我有一个复选框列表,用于将过滤器应用于我需要传递给控制器​​操作方法的网格。我的问题是,即使在将帖子数据传递给它之前正确检索了所选复选框的值,它也没有显示在控制器中。有趣的是,如果我正在加载选中复选框的页面,在这种情况下,值就会被传递。我很困惑为什么会这样。我会非常感激任何帮助。请参阅下面我用于绑定网格的代码:

    function BindGrid() {

        var selectedPriority = [];
        $('#priority input:checked').each(function () {
            selectedPriority.push($(this).val());
        });
        var selectedState = [];
        $('#state input:checked').each(function () {
            selectedState.push($(this).val());
        });
        var selectedAckState = [];
        $('#ackState input:checked').each(function () {
            selectedAckState.push($(this).val());
        });

        var x = convertIntArrayToString(selectedPriority);
        var y = convertIntArrayToString(selectedState);
        var z = convertIntArrayToString(selectedAckState);

        jqDataUrl = "Alarm/LoadjqData";
        // Set up the jquery grid
        //$("#jqTable").jqGrid('clearGridData');
        $("#jqTable").jqGrid({
            url: jqDataUrl,
            datatype: "json",
            mtype: "POST",
            postData: {
                sp: x,
                ss: y,
                sak: z
            },
            cache: false,
            // Specify the column names
            colNames: ["Id", "PointRef", "Value", "AlarmStateName", "AckStateName", "Priority", "AlarmDate"],

            // Configure the columns
            colModel: [
                { name: "AlarmId", index: "AlarmId", width: 70, align: "left" },
                { name: "PointRef", index: "PointRef", width: 200, align: "left" },
                { name: "Value", index: "Value", width: 200, align: "left" },
                { name: "AlarmStateName", index: "AlarmStateName", width: 150, align: "left" },
                { name: "AcknowledgementStateName", index: "AcknowledgementStateName", width: 170, align: "left" },
                { name: "Priority", index: "Priority", width: 100, align: "left" },
                { name: "AlarmDate", index: "AlarmDate", width: 250, align: "left" }
                ],

            // Grid total width and height
            width: 700,
            height: 250,

            // Paging
            toppager: true,
            pager: $("#jqTablePager"),
            rowNum: 10,
            rowList: [5, 10, 20],
            viewrecords: true, // Specify if "total number of records" is displayed

            // Default sorting
            sortname: "AlarmId",
            sortorder: "desc",

            // Grid caption
            caption: "Telemetry Alarms"
        }).navGrid("#jqTablePager",
                { refresh: true, add: false, edit: false, del: false },
                    {}, // settings for edit
                    {}, // settings for add
                    {}, // settings for delete
                    {sopt: ["cn"]} // Search options. Some options can be set on column level
             )
        //.trigger('reloadGrid', [{ page: 1}]); 
             .trigger('reloadGrid');

    }

    function convertIntArrayToString(data) {
        var x='';
        //alert(data.length);
         for(var i=0;i<data.length;i++) {
          x = x + data[i] + ",";
      }
      if (x != '') {
          x = x.substr(0, x.length - 1);
          //alert(x);
      }
      return x.toString();
    }

//Controller action method

 [HttpPost]
        public ActionResult LoadjqData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper,
            string searchString, string sp, string ss, string sak)
        {

            //code ommited.
            return Json(jsonData);
        }

sp、ss 和 sak 参数的值未出现在控制器中。

4

1 回答 1

0

这篇文章解决了我的问题:

jqgrid在重新加载时不更新数据

我不得不将用于读取复选框值的代码移动到一个单独的函数中,并在 PostData 中访问该函数,它就像一个魅力。没有这个,它每次都发送相同的数据。

更新的代码如下:

function BindGrid() {
    var jqDataUrl = null;
    jqDataUrl = "Alarm/LoadjqData" //+ x;
    $("#jqTable").jqGrid({
        url: jqDataUrl,
        cache: false,
        datatype: "json",
        mtype: "POST",
        postData: {
            sp: function () { return getPriority(); }
        },
        // Specify the column names
        colNames: ["Id", "PointRef", "Value", "AlarmStateName", "AckStateName", "Priority", "AlarmDate"],

        // Configure the columns
        colModel: [
            { name: "AlarmId", index: "AlarmId", width: 70, align: "left" },
            { name: "PointRef", index: "PointRef", width: 200, align: "left" },
            { name: "Value", index: "Value", width: 200, align: "left" },
            { name: "AlarmStateName", index: "AlarmStateName", width: 150, align: "left" },
            { name: "AcknowledgementStateName", index: "AcknowledgementStateName", width: 170, align: "left" },
            { name: "Priority", index: "Priority", width: 100, align: "left" },
            { name: "AlarmDate", index: "AlarmDate", width: 250, align: "left" }
            ],

        // Grid total width and height
        width: 700,
        height: 250,

        // Paging
        toppager: true,
        pager: $("#jqTablePager"),
        rowNum: 10,
        rowList: [5, 10, 20],
        viewrecords: true, // Specify if "total number of records" is displayed

        // Default sorting
        sortname: "AlarmId",
        sortorder: "desc",

        // Grid caption
        caption: "Telemetry Alarms"
    }).navGrid("#jqTablePager",
            { refresh: true, add: false, edit: false, del: false },
                {}, // settings for edit
                {}, // settings for add
                {}, // settings for delete
                {sopt: ["cn"]} // Search options. Some options can be set on column level
         )
         .trigger('reloadGrid');

}

function convertIntArrayToString(data) {
    var x = '';
    //alert(data.length);
    for (var i = 0; i < data.length; i++) {
        x = x + data[i] + ",";
    }
    if (x != '') {
        x = x.substr(0, x.length - 1);
        //alert(x);
    }
    return x.toString();
}

function getPriority()
{
 var selectedPriority = [];
    $('#priority input:checked').each(function () {
        selectedPriority.push($(this).val());
    });
    return convertIntArrayToString(selectedPriority);

}
于 2012-04-12T20:58:30.203 回答