在学习了表格的分页后,本文尝试在表格顶部加入搜索栏,用来筛选表格的数据,先看一下效果:

未进行搜索时,有394条记录:

在输入乘车码“1”和订单号“20150120”后,只有28条记录:

此处使用了两个筛选条件:乘车码和订单号进行了模糊查询,从而大大减少了表格的记录数,以达到搜索的目的。

HTML页面代码如下:

<div id="toolbar">
<div class="my-container">
<label class="myLabel-content">乘车码:</label>
<div class="myText-content">
<input id="busNo" type="text" class="form-control" placeholder="输入乘车码">
</div>
</div>
<div class="my-container">
<label class="myLabel-content">订单号:</label>
<div class="myText-content">
<input id="orderId" type="text" class="form-control" placeholder="输入订单号">
</div>
</div>
<div class="myBtn-content">
<button id="search" type="button" class="btn btn-primary">搜索</button>
<button id="reset" type="button" class="btn btn-default">重置</button>
</div>
</div>
<table id="testTable" data-toggle="table" data-height="530"
data-striped="true" data-classes="table table-hover" data-toolbar="#toolbar"
data-side-pagination="server" data-pagination="true" data-page-list="[10,20,50,100]">
<thead>
<tr>
<th class="col-md-3" data-field="BusNo" data-align="center">乘车码</th>
<th class="col-md-5" data-field="OrderId" data-align="center">订单号</th>
<th class="col-md-4" data-field="OrderDate" data-align="center" data-formatter="FormatDateTime">订单日期</th>
</tr>
</thead>
</table>

可以注意到:在table中并没有URL,因为在查询的过程中参数可以是变化的,会在JavaScript中对URL做动态的绑定。JavaScript代码如下:

$(function () {
$('#testTable').bootstrapTable('refresh', { url: '/Home/GetPaginationData2' });
$('#search').click(function () {
var busNo = $('#busNo').val();
var orderId = $('#orderId').val();
$('#testTable').bootstrapTable('refresh', { url: '/Home/GetPaginationData2?orderId=' + orderId + '&busNo=' +busNo });
});
$('#reset').click(function() {
$('#busNo').val('');
$('#orderId').val('');
$('#testTable').bootstrapTable('refresh', { url: '/Home/GetPaginationData2' });
});
});

其中三个函数对表格的URL进行了设定:

  1. 初始化页面时;

  2. 点击“搜索”按钮,对URL添加了两个参数:“orderId”和“busNo”,稍后在后台代码中会对这两个参数进行处理;

  3. 点击“重置”按钮,将清空查询条件,重新查询表格数据。

后台代码如下:

public JsonResult GetPaginationData2(string orderId, string busNo)
{
var offset = Request.Params["offset"] == null ? : int.Parse(Request.Params["offset"]);
var limit = Request.Params["limit"] == null ? : int.Parse(Request.Params["limit"]); using (var context = new TestEntities())
{
int count;
var q = (from a in context.Tickets
join b in context.Order on a.OrderId equals b.Id
select new
{
BusNo = a.BusNumber,
OrderId = b.Id,
OrderDate = b.OrderDateTime,
}).Where(
t => (string.IsNullOrEmpty(orderId) || t.OrderId.Contains(orderId)) &&
(string.IsNullOrEmpty(busNo) || t.BusNo.Contains(busNo)))
.Distinct()
.Pagination(offset, limit, out count);
var data = q.ToList();
return Json(new {rows = data, total = count}, JsonRequestBehavior.AllowGet);
}
}

下面是程序中会引用的CSS样式,供参考:

.my-container {
float: left;
display: inline-block;
margin-right:30px;
} .myLabel-content ,.myText-content,.myBtn-content{
float: left;
display: inline-block;
margin-left: 10px;
}
.myLabel-content,.myText-content input[type='text'],.myBtn-content .btn {
height: 30px;
font-size: 12px;
}
.myBtn-content .btn {
margin-bottom: 10px;
}

Bootstrap表格添加搜索栏的更多相关文章

  1. bootstrap表格添加按钮、模态框实现

    bootstrap表格添加按钮.模态框实现 原创 2017年07月20日 17:35:48 标签: bootstrap 1723 bootstrap表格添加按钮.模态框实现 - 需求: 需要表格后面每 ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:为任意 <table> 添加基本样式 (只有横向分隔线)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Bootstrap 表格 笔记

    Bootstrap 表格 Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. < ...

  4. Bootstrap 表格

    Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. <thead> 表格 ...

  5. 【原创】bootstrap框架的学习 第七课 -[bootstrap表格]

    Bootstrap 表格 标签 描述 <table> 为表格添加基础样式. <thead> 表格标题行的容器元素(<tr>),用来标识表格列. <tbody& ...

  6. Bootstrap Bootstrap表格插件bootstrap-table配置与应用小结

    Bootstrap表格插件bootstrap-table配置与应用小结   by:授客 QQ:1033553122 1.   测试环境 win7 JQuery-3.2.1.min.js 下载地址: h ...

  7. 在<s:iterator>标签里给动态表格添加序号

    在<s:iterator>标签里给动态表格添加序号,需要用到<s:iterator>标签里的Status属性里的count eg:<s:iterator value=&q ...

  8. 用JSON数据向已定义列的表格添加数据行

    其实添加方式和在MVC中动态读取JSON数据创建表格一样,只不过一个是完整表格添加,一个是从表格中间添加.不详细说明了. <div> <table class="table ...

  9. 利用jquery表格添加一行并在每行第一列大写字母显示实现方法

    表格添加一行并在每行第一列大写字母显示jquery实现方法 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN& ...

随机推荐

  1. JS 模板引擎 Handlebars.js 中文说明

    Handlebars 为你提供了一个可以毫无挫折感的高效率书写 语义化的模板 所必需的一切. Mustache 模板和 Handlebars 是兼容的,所以你可以把Mustache模板拿来导入到Han ...

  2. speechSynthesis,TTS语音合成。

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. vue-router 介绍

    vue-router是官方的路由管理工具,用于组建单页面应用很方便快捷.还记得我们在上一篇文章中,使用vue-cli创建的hello-vue的那个项目吗?打开项目,我们先看下目录结构: 看了上面的目录 ...

  4. 获取iOS系统版本号,慎重使用[[[UIDevice currentDevice] systemVersion] floatValue]——【sdk缺陷】

    iOS 最常见的获取系统版本的方法是: [[[UIDevice currentDevice] systemVersion] floatValue] 可是.这个floatValue是不靠谱的,这也算是i ...

  5. multiTarget within one project pods manage

    step1:file->new->target create 1 targetstep2:change Podfile and update podstep3:check new targ ...

  6. spring源码解析——2容器的基本实现(第2版笔记)

    感觉第二版写的略潦草,就是在第一版的基础上加上了新的流行特性,比如idea,springboot,但是,潦草痕迹遍布字里行间. 虽然换成了idea,但是很多截图还是eclipse的,如果不是看了第一版 ...

  7. Performance Tuning Using Linux Process Management Commands

    [root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...

  8. 点击文本选中checkbox

    <checbox文本编辑/>      : 只点击checkbox时,才可以选中,点击文本时无法选中 <label><checbox文本编辑/></label ...

  9. 软件版本GA,RC,alpha,beta含义

    软件版本GA,RC,alpha,beta含义 (1)RC:(Release Candidate) Candidate是候选人的意思,用在软件上就是候选版本.Release.Candidate.就是发行 ...

  10. self = [super init]的解释

    在Object-C中我们很多时候都会重写init方法.一般情况下我们都会这样写: -(instancetype)initWithDic:(NSDictionary *)dic{ if(self=[su ...