Jqgrid动态地将搜索运算符发送到服务器

时间:2017-04-16 17:12:13

标签: php jquery jqgrid

我需要你的帮助, 我有一个工具栏搜索表。 我使用jqgrid 5.2,jquery 3.0和php 7。 我的专栏之一有搜索运算符,如'eq','ne','le','lt','gt','ge'。当我更改搜索运算符时,发送到服务器的过滤器不能更改。它的价值总是一样的

{"groupOp":"AND","rules":[{"field":"item",**"op":"eq"**,"data":"12"}]}

我的问题是如果我更改搜索运算符,如何将“op”:“eq”更改为其他值?

这是我的jqgrid代码

function fixSearchOperators() {
    var $grid = $("#list"),
        columns = $grid.jqGrid ('getGridParam', 'colModel'),
        filterToolbar = $($grid[0].grid.hDiv).find("tr.ui-search-toolbar");

    filterToolbar.find("th").each(function(index) {
        var $searchOper = $(this).find(".ui-search-oper");
        if (!(columns[index].searchoptions &&
                columns[index].searchoptions.searchOperators)) {
            $searchOper.hide();
        }
    });
}

var mygrid =
$("#list").jqGrid({
    url:'tabel/tablejson.php?q=1', 
    datatype: 'json', 
    mtype: "POST",
    colNames:['Item','Qty'],
    colModel :[  
        {name:'item', index:'item', width:240,
            editable:true, editoptions:{size:20, maxlength:'20'},
            editrules:{required:true}, sorttype:'string',
            searchoptions:{clearSearch: false},
             formoptions:{elmprefix:"(*)"}},
        {name:'qty', index:'qty', width:50, align:'right', editable:true,
            editoptions:{size: 5}, editrules:{required:true, number:true},
            sorttype:'integer',searchoptions:{sopt:['eq', 'ne', 'le', 'lt', 'gt', 'ge'],
            searchOperators: true, clearSearch: false},
            formoptions:{elmprefix:"(*)",elmsuffix:""}}
    ],
    multiselect: true, 
    altRows: true, 
    altclass: 'myAltRowClass',
    pager: '#pager', 
    rowNum:500, 
    rownumbers: true, 
    rownumWidth: 20,
    gridview: true,
    pginput: false,
    height: 380, 
    shrinkToFit: false,
    width: 390, 
    sortname: 'Qty', 
    sortorder: 'asc', 
    scrollOffset:0,
    viewrecords: true, 
    caption: 'TEST', 
    editurl:"edit/editjson.php", 
    loadComplete: function() {
        //reset search operator
        $("a.soptclass[colname='FieldName']").attr("soper","eq");
        $("a.soptclass[colname='FieldName']").text("==");
    }
});

$("#list").jqGrid('navButtonAdd',"#pager", {
    caption:"Reset",
    title:"Reset Filter",
    buttonicon:'ui-icon-refresh', 
    onClickButton:function(){
        mygrid[0].clearToolbar() 
    } 
});

$("#list").jqGrid('filterToolbar', {
    multipleSearch: true,
    searchOnEnter: false,
    searchOperators: true,
    stringResult: true,
    defaultSearch: "cn"
});
fixSearchOperators();

这是我的服务器代码

<?php
include '../config/connections.php';
$examp = $_REQUEST["q"]; //query number
$page  = $_REQUEST['page']; // get the requested page
$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid – rowNum parameter in the grid 
$sidx  = $_REQUEST['sidx']; // get index row - i.e. user click to sort. At first time sortname parameter - after that the index from colModel
$sord  = $_REQUEST['sord']; // get the direction
if(!$sidx) $sidx =1;// if we not pass at first time index use the first column for the index or what you want
$filters = str_replace('\"','"' ,$_POST['filters']);
$search = $_POST['_search'];
$where = "";

if(($search==true) &&($filters != "")) {
    $filters = json_decode($filters);
    $where = " WHERE ";
    $whereArray = array();
    $rules = $filters->rules;
    $groupOperation = $filters->groupOp;
    foreach($rules as $rule) {
        $fieldName = $rule->field;
        $fieldData = mysqli_real_escape_string($db,$rule->data);
        switch ($rule->op) {
       case "eq":
            $fieldOperation = " = '".$fieldData."'";
            break;
       case "ne":
            $fieldOperation = " != '".$fieldData."'";
            break;
       case "lt":
            $fieldOperation = " < '".$fieldData."'";
            break;
       case "gt":
            $fieldOperation = " > '".$fieldData."'";
            break;
       case "le":
            $fieldOperation = " <= '".$fieldData."'";
            break;
       case "ge":
            $fieldOperation = " >= '".$fieldData."'";
            break;
       case "nu":
            $fieldOperation = " = ''";
            break;
       case "nn":
            $fieldOperation = " != ''";
            break;
       case "in":
            $fieldOperation = " IN (".$fieldData.")";
            break;
       case "ni":
            $fieldOperation = " NOT IN '".$fieldData."'";
            break;
       case "bw":
            $fieldOperation = " LIKE '".$fieldData."%'";
            break;
       case "bn":
            $fieldOperation = " NOT LIKE '".$fieldData."%'";
            break;
       case "ew":
            $fieldOperation = " LIKE '%".$fieldData."'";
            break;
       case "en":
            $fieldOperation = " NOT LIKE '%".$fieldData."'";
            break;
       case "cn":
            $fieldOperation = " LIKE '%".$fieldData."%'";
            break;
       case "nc":
            $fieldOperation = " NOT LIKE '%".$fieldData."%'";
            break;
        default:
            $fieldOperation = "";
            break;
            }
        if($fieldOperation != "") $whereArray[] =  $fieldName.$fieldOperation;
    }
    if (count($whereArray)>0) {
        $where .= join(" ".$groupOperation." ", $whereArray);
    } else {
        $where = "";
    }
}

switch ($examp) {
case 1:
// calculate the number of rows for the query. We need this for paging the result 
$query = "SELECT COUNT(*) AS count FROM test".$where;
$result = $db->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC);/* associative array */
$count = $row['count'];
// calculate the total pages for the query 
if( $count >0 ) {
    $total_pages = ceil($count/$limit);
} else {
    $total_pages = 0;
}
// if for some reasons the requested page is greater than the total 
// set the requested page to total page 
    if ($page > $total_pages) $page=$total_pages;
// calculate the starting position of the rows
    $start = $limit*$page - $limit; // do not put $limit*($page - 1)
// if for some reasons start position is negative set it to 0 
// typical case is that the user type 0 for the requested page 
    if ($start<0) $start = 0;
// the actual query for the grid data 
    $SQL = "SELECT * FROM test".$where." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit;
    $result = $db->query($SQL) or die("Couldn't execute query.".mysqli_error($db)); 
    // Construct the json data
    $responce->page = $page;// current page
    $responce->total = $total_pages;// total pages
    $responce->records = $count;// total records
    $i=0;
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $responce->rows[$i]['id']=$row['item'];
        $responce->rows[$i]['cell']=array($row['item'],$row['qty']);
        $i++;
    } 
//echo $json->encode($responce); // coment if php 5
    echo json_encode($responce);

    break;
case 3:
}
mysqli_close($db);

function Strip($value)//a function called Strip that will remove slashes from the user typed text
{
if(get_magic_quotes_gpc() != 0)
{
    if(is_array($value))  
        if ( array_is_associative($value) )
        {
            foreach( $value as $k=>$v)//more or less this is pretty much saying for every value that the user entered in *note that the $v is data that comes out of your Strip function which as I mentioned it removes slashes from the user typed fields.
                $tmp_val[$k] = stripslashes($v);
            $value = $tmp_val; 
        }               
        else  
            for($j = 0; $j < sizeof($value); $j++)
                $value[$j] = stripslashes($value[$j]);
    else
        $value = stripslashes($value);
}
return $value;
}
function array_is_associative ($array)
{
if ( is_array($array) && ! empty($array) )
{
    for ( $iterator = count($array) - 1; $iterator; $iterator-- )
    {
        if ( ! array_key_exists($iterator, $array) ) { return true; }
    }
    return ! array_key_exists(0, $array);
}
return false;
}
?>

1 个答案:

答案 0 :(得分:0)

loadComplete用于在工具栏上重置过滤器时自动重置搜索运算符。但它似乎不起作用。

fixSearchOperator函数用于项目列上的隐藏搜索运算符。我只想在qty列上显示搜索运算符。

是的,我试图在qty列中更改sopt。无论我改变了sopt的内容,它只发送第一个搜索运算符选项。

我已尝试过jqgrid 4.4.3但它还没有搜索运算符功能