jqGrid自定义网格过滤器重新加载

时间:2013-12-10 21:17:59

标签: jquery jqgrid filter reload

我正在使用jqGrid通过JSON数据在mysql中维护数据库。我想让一个过滤器重新加载网格 - 非活动行与活动行。在我的数据url php文件中,我连接到我的数据库并提供初始查询,我只在avail_ind = 'Y'的列中拉列,因此网格中已有的所有数据都是活动记录。所以做一个搜索过滤器不会是情况的解决方案,因为找不到avail_ind = 'N'的任何记录。

我发现Oleg的demo类似于我正在寻找的东西。我不需要他演示的垂直标题,但主要是订单状态框,其中包含用于过滤网格数据的复选框。

我主要担心的是,因为我最初在网格中拉出活动记录,我如何向网格提供avail_ind ='N'或'Y'的参数,以显示网格中的所有可能记录以及某种方式链接复选框或单选按钮,用于选择用户想要查看的记录类型。

感谢您的帮助。


更新

jqGrid HTML / JavaScript代码: 为简洁起见,未包括所有代码

 jQuery(document).ready(function(){ 
      jQuery("#grid").jqGrid("initFontAwesome").jqGrid({
          pager:'#gridpager', 
          url:'getDivisions.php',
          datatype: "json",
          mtype: 'GET',
          height: 300,
          width: 1000,
          showerrors:true,
          debug:true,

          colNames:['Div', 'L1L2', 'L2', 'L1L3', 'L2L3', 'Exec Begin', 'Exec End', 'CSA', 'Area Id','Short Desc','Enabled'],
          colModel:[
                    {name:'div_id',
                        index:'div_id', 
                        width:30,
                        editable:true,
                        sortable:false, 
                        resizable:false,
                        align:"center", 
                        editoptions:{size:2,maxlength:2},
                        editrules:{required:true,number:true},
                        formoptions:{elmprefix:"(*)"}
                     },

                    {name:'l1l2_id',
                        index:'l1l2_id',
                        width:30,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        //formatter: nullFormatter,
                        editoptions:{size:4,maxlength:4},
                        editrules:{required:true,number:true},
                        formoptions:{elmprefix:"(*)"}

                    },

                    {name:'l2_id',
                        index:'l2_id', 
                        width:30,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        align:"center",
                        editoptions:{size:2,maxlength:2},
                        editrules:{required:true,number:true},
                        formoptions:{elmprefix:"(*)"}
                    },
                    {name:'l1l3_id',
                        index:'l1l3_id', 
                        width:30,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        editoptions:{size:5,maxlength:6,readonly:'readonly'},
                        editrules:{number:true}

                    },
                    {name:'l2l3_id',
                        index:'l2l3_id', 
                        width:30,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        editoptions:{size:5,maxlength:4,readonly:'readonly'},
                        editrules:{number:true}
                    },
                    {name:'exec_beg',
                        index:'exec_beg', 
                        width:60,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        editoptions:{size:10,maxlength:8,readonly:'readonly'},
                        editrules:{number:true}
                    },
                    {name:'exec_end',
                        index:'exec_end', 
                        width:50,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        editoptions:{size:10,maxlength:8,readonly:'readonly'},
                        editrules:{number:true}
                    },
                    {name:'csa_id',
                        index:'csa_id', 
                        width:35,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        editoptions:{size:5,maxlength:5},
                        editrules:{required:true},
                        formoptions:{elmprefix:"(*)"}
                    },
                    {name:'area_id',
                        index:'area_id',
                        width:40,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        align:"center",
                        editoptions:{size:2,maxlength:2},
                        editrules:{number:true},
                        formoptions:{elmprefix:"(*)"}
                    },
                    {name:'short_desc',
                        index:'short_desc', 
                        width:60,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        editoptions:{size:7,maxlength:10},
                        editrules:{required:true},
                        formoptions:{elmprefix:"(*)"}
                    },
                    {name:'avail_ind',
                        index:'avail_ind',
                        width:40,
                        editable:true,
                        resizable:false,
                        sortable:false,
                        align:"center",
                        edittype:"select",
                        editoptions:{value:"Y:Y;N:N"}}
                   ],

          viewrecords: true,
          sortorder: "asc",
          sortname: "div_id",
          caption:"Division Codes",
          editurl:'editdivisions.php',
          toppager:true, 
          recordtext:'', 
          hidegrid:false, 
          scroll:true,
          rowNum:"10000"

     }); //jQuery("#grid").jqGrid

GetDivisions.php代码:检索数据以加载网格

<?php 
    $dbhost = "localhost"; 
    $dbuser = "root"; 
    $dbpass = "*********"; 
    $dbname = "CodeTable"; 


    $page = $_GET['page']; // get the requested page
    $limit = $_GET['rows']; // get how many rows we want to have into the grid
    $sidx = $_GET['sidx']; // get index row - i.e. user click to sort
    $sord = $_GET['sord']; // get the direction

    if(!$sidx) $sidx =1;

    // connect to the database
    $db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());

    mysql_select_db($dbname) or die("Error conecting to db.");
    $result = mysql_query('SELECT div_id, l1l2_id, l2_id, l1l3_id, l2l3_id, exec_beg, exec_end, csa_id, area_id, short_desc, avail_ind FROM divcodes where div_id <> "  " and avail_ind = "Y" and active_ind="Y"');
    $row = mysql_fetch_array($result,MYSQL_ASSOC);
    $count = $row['count'];

    if( $count >0 ) 
    {
        $total_pages = cell($count/$limit);
    } 
    else 
    {
            $total_pages = 0;
    }

    if ($page > $total_pages) $page=$total_pages;
    $start = $limit*$page - $limit; // do not put $limit*($page - 1)
    $SQL = 'SELECT div_id, l1l2_id, l2_id, l1l3_id, l2l3_id, exec_beg, exec_end, csa_id, area_id, short_desc, avail_ind FROM divcodes where div_id <> "  " and avail_ind = "Y" and active_ind="Y"';
    $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());

    $response->page = $page;
    $response->total = $total_pages;
    $response->records = $count;

    $i=0;
    while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
        $response->rows[$i]['id']=$row[div_id];
        $response->rows[$i]['cell']=array($row[div_id],$row[l1l2_id],$row[l2_id],$row[l1l2_id],$row[l2l3_id],$row[exec_beg],$row[exec_end],$row[csa_id],$row[area_id],$row[short_desc],$row[avail_ind]);
        $i++;
    }        
    echo json_encode($response);

    ?>

1 个答案:

答案 0 :(得分:1)

我建议你不要对你的数据进行硬编码服务器端查询,而是让jqGrid告诉你的服务器它在寻找什么。

这可以通过设置单选按钮元素或复选框等作为网页的一部分来完成。然后在页面加载时,您可以帮助形成jqGrid发出的请求,然后在您设置的元素处于状态时重新发送。

例如:

element.ChangeEvent()( function () {
    var grid = $('#gridName');

grid.jqGrid('setGridParam', { search: false });
var postData = grid.jqGrid('getGridParam', 'postData');
    $.extend(postData, { filters: "" });

var f = { groupOp: "AND", rules: [] };

    f.rules.push({ field: "avail_ind", op: "eq", data: $(selectedElementName).val() });
    grid.jqGrid('setGridParam', { search: true });
}
JSON.stringify(f) : "")}

                                        });
    grid.jqGrid().trigger('reloadGrid', [{ page: 1}]);

});

作为此项的一部分,当您最初设置jqGrid时,您将search: true和过滤器设置为网格设置的一部分,以便显示要启动的精确数据,然后将更改的用户首选项设置为您设置更改的元素。