我正在为我的网站使用“bootstrap-table”(https://github.com/wenzhixin/bootstrap-table)插件,目前正在使用它的服务器端分页属性。
该表已正确填充,并且搜索功能也有效。但是当搜索不存在的东西时,它会显示所有记录而不是显示“未找到匹配记录”。
这是我正在使用的html代码...
<table data-toggle="table"
data-url="1.php"
data-pagination="true"
data-side-pagination="server"
data-page-list="[5, 10, 20, 50, 100, 200]"
data-search="true"
data-height="300">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th data-field="memberID" data-align="right" data-sortable="true">Member ID</th>
<th data-field="name" data-align="center" data-sortable="true"> Name</th>
<th data-field="dob" data-sortable="true">Date of Birth</th>
</tr>
</thead>
</table>
这是我用来创建 JSON 响应的 php 脚本...
<?php
require_once('db-connect.php');
if(isset($_GET["limit"])) {
$limit = $_GET["limit"];
} else {
$limit = 10;
}
if(isset($_GET["offset"])) {
$offset = $_GET["offset"];
} else {
$offset = 0;
}
if(isset($_GET["sort"])) {
$sort = $_GET["sort"];
} else {
$sort = "";
}
if(isset($_GET["order"])) {
$order = $_GET["order"];
} else {
$order = "asc";
}
if(isset($_GET["search"])) {
$search = $_GET["search"];
} else {
$search = "";
}
if($search == "") {
$result = mysqli_query($connection,"select memberID, name, dob from memberdetails" );
} else {
$result = mysqli_query($connection,"select memberID, name, dob from memberdetails WHERE memberID LIKE '%$search%'" );
}
$row = array();
if ( mysqli_num_rows($result) > 0 ) {
while($row = mysqli_fetch_assoc($result)) {
$result_2d_arr[] = array ( 'memberID' => $row['memberID'],
'name' => $row['name'],
'dob' => $row['dob']);
}
//get the result size
$count = sizeof($result_2d_arr);
//order the array
if($order != "asc") {
$result_2d_arr = array_reverse($result_2d_arr);
}
//get the subview of the array
$result_2d_arr = array_slice($result_2d_arr, $offset, $limit);
echo "{";
echo '"total": ' . $count . ',';
echo '"rows": ';
echo json_encode($result_2d_arr);
echo "}";
}
?>
JSON响应如下...
{"total": 23,"rows": [{"memberID":"1","name":"asd","dob":"2015-06-03"},{"memberID":"2","name":"asd","dob":"2015-06-03"},{"memberID":"3","name":"asd","dob":"2015-06-03"},{"memberID":"4","name":"asd","dob":"2015-06-03"},{"memberID":"5","name":"asd","dob":"2015-06-03"},{"memberID":"6","name":"asd","dob":"2015-06-03"},{"memberID":"7","name":"asd","dob":"2015-06-03"},{"memberID":"8","name":"asd","dob":"2015-06-03"},{"memberID":"9","name":"asd","dob":"2015-06-03"},{"memberID":"10","name":"asd","dob":"2015-06-03"}]}