在bootgrid中使用自定义请求处理程序后,搜索词组无效

时间:2017-07-07 15:10:23

标签: php mysql jquery-bootgrid

我已经为后续表实现了一个bootgrid,但在使用自定义请求处理程序后,搜索短语无效。这是表。

<div class="table-responsive">
    <table id="product_data" class="table table-bordered table-hover">
        <thead>
        <tr>

            <th data-column-id="sbranch" data-header-align="center" data-align="left">Branch</th>
            <th data-column-id="subject_code" data-header-align="center" data-align="center" data-css-class="hidden-xs hidden-sm" 
            data-header-css-class="hidden-xs hidden-sm">Code</th>
            <th data-column-id="sname" data-header-align="center" data-align="left">Subject Name</th>
            <th data-column-id="smode" data-header-align="center" data-align="center" data-css-class="hidden-xs hidden-sm" 
            data-header-css-class="hidden-xs hidden-sm">Mode</th>
            <th data-column-id="ssemester" data-header-align="center" data-align="center" data-order="asc" data-css-class="hidden-xs hidden-sm" 
            data-header-css-class="hidden-xs hidden-sm">Semester</th>
            <th data-column-id="stype" data-header-align="center" data-align="center" data-css-class="hidden-xs hidden-sm" 
            data-header-css-class="hidden-xs hidden-sm">Type</th>
            <th data-column-id="scredit" data-header-align="center" data-align="center" data-css-class="hidden-xs hidden-sm" 
            data-header-css-class="hidden-xs hidden-sm">Credit</th>
            <th data-column-id="sslot" data-header-align="center" data-align="center" data-css-class="hidden-xs hidden-sm" 
            data-header-css-class="hidden-xs hidden-sm">Slot</th>
            <th data-column-id="syear" data-header-align="center" data-align="center" data-css-class="hidden-xs hidden-sm" 
            data-header-css-class="hidden-xs hidden-sm">Year</th>
                        <th data-column-id="commands" data-formatter="commands" data-sortable="false" data-align="center" data-header-align="center">Download</th>
        </tr>
        </thead>
    </table>
    </div>

这是加载

的请求处理程序
        var productTable = $('#product_data').bootgrid({

        //navigation: 0,
        ajax: true,

        method:"POST",

        requestHandler: function (request) {
               //Add your id property or anything else
                request.branch = $("#branch").val();

               request.id = "b0df282a-0d67-40e5-8558-c9e93b7befed";
               return request;
        },
        url: "sylfetch2.php",

    });
$('#product_data').bootgrid('reload');

以下是将数据提取到表

的代码
<?php
//fetch.php
include("dbbranch.php");

$sbranch = mysqli_real_escape_string($bmysqli,$_POST["branch"]);

$query = '';
$data = array();
$records_per_page = 10;
$start_from = 0;
$current_page_number = 0;
if(isset($_POST["rowCount"]))
{
 $records_per_page = $_POST["rowCount"];
}
else
{
 $records_per_page = 10;
}
if(isset($_POST["current"]))
{
 $current_page_number = $_POST["current"];
}
else
{
 $current_page_number = 1;
}
$start_from = ($current_page_number - 1) * $records_per_page;
$query .= "
  SELECT 
  * FROM subject WHERE (
        sbranch = '".$sbranch."' )";


if(!empty($_POST["searchPhrase"]))
{
    $query .= 'WHERE (subject.sid LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.sbranch LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.ssemester LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.sslot LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.stype LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.scredit LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.subject_code LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.sname LIKE "%'.$_POST["searchPhrase"].'%" ';
    $query .= 'OR subject.smode LIKE "%'.$_POST["searchPhrase"].'%" ) ';
}
$order_by = '';
if(isset($_POST["sort"]) && is_array($_POST["sort"]))
{
 foreach($_POST["sort"] as $key => $value)
 {
  $order_by .= " $key $value, ";
 }
}
else
{
 $query .= 'ORDER BY subject.sid DESC ';
}
if($order_by != '')
{
 $query .= ' ORDER BY ' . substr($order_by, 0, -2);
}

if($records_per_page != -1)
{
 $query .= " LIMIT " . $start_from . ", " . $records_per_page;
}
//echo $query;
$result = mysqli_query($bmysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
 $data[] = $row;
}

$query1 = "SELECT * FROM subject WHERE (
        sbranch = '".$sbranch."' ) ";
$result1 = mysqli_query($bmysqli, $query1);
$total_records = mysqli_num_rows($result1);

$output = array(
 'current'  => intval($_POST["current"]),
 'rowCount'  => 10,
 'total'   => intval($total_records),
 'rows'   => $data
);

echo json_encode($output);

?>

使用此请求处理程序后,无论何时键入任何搜索,表都会在加载时卡住。

1 个答案:

答案 0 :(得分:0)

您应该以这种方式传递自定义请求对象中的属性:

    ... //Rest of code skipped for brevety
    requestHandler: function (request) {
        var model = {
        Current: request.current,
        RowCount: request.rowCount,
        Search: request.searchPhrase,
        Branch: $("#branch").val(),
        Id: "b0df282a-0d67-40e5-8558-c9e93b7befed"
        };

        //You could need this. Otherwise, skip...
        for (var key in request.sort) {
            model.SortBy = key;
            model.SortDirection = request.sort[key];
        };

        return JSON.stringify(model);
    },
    ....// the rest of code...