Bootstrap Table使用整理(五)-分页组合查询

来源:互联网 发布:电脑网络开关设置 编辑:程序博客网 时间:2024/05/16 12:17

一、分页组合查询

[javascript] view plain copy
 print?
  1. /* 
  2. * data-pagination 指定是否启用分页 
  3. * data-page-list 指定分页的页数据量数组 '[5,10]' 
  4. * data-side-pagination 指定分页是否是服务端(server)/客户端(client) 
  5. * 特别说明: 
  6. * 客户端,请求参数: 
  7. * search:文本框内容,在文本框内容改变是自动提交请求 
  8. * order: 排序方式 
  9. * sort:排序列名 
  10. * offset:划过条数 
  11. * limit:要获取的数据的条数 
  12. * 
  13. */  
  14. var $table1= $('#table1').bootstrapTable({  
  15.     columns: [  
  16.         { field: 'sno', title: '学生编号',sortable:true },  
  17.         { field: 'sname', title: '学生姓名' },  
  18.         { field: 'ssex', title: '性别' },  
  19.         { field: 'sbirthday', title: '生日' },  
  20.         { field: 'class', title: '课程编号' },  
  21.     ],  
  22.     url: '@Url.Action("GetStuList", "DataOne")',  
  23.     pagination: true,  
  24.     sidePagination: 'server',  
  25.     pageList:[5,10,20,50],  
  26.     queryParams: function (params) {  
  27.         params.name = '张三丰';  
  28.         //特别说明,返回的参数的值为空,则当前参数不会发送到服务器端  
  29.         //这种指定请求参数的方式和datatables控价类似  
  30.         params.sex = $('input[name="sex"]:checked').val();  
  31.         return params;  
  32.     }  
  33. });  
  34. //刷新方法  
  35. $('#heartBtn').click(function () {  
  36.     $table1.bootstrapTable('refresh');  
  37. });  
[html] view plain copy
 print?
  1. <table id="table1"  
  2.        data-classes="table table-hover "  
  3.        data-search="true"  
  4.        data-show-refresh="true"  
  5.        data-show-toggle="true"  
  6.        data-show-columns="true"  
  7.        data-toolbar="#toolbar"></table>  
  8. <div id="toolbar">  
  9.     <div class="btn-group">  
  10.         <button class="btn btn-default">  
  11.             <i class="glyphicon glyphicon-plus"></i>  
  12.         </button>  
  13.         <button class="btn btn-default">  
  14.             <i class="glyphicon glyphicon-heart" id="heartBtn"></i>  
  15.         </button>  
  16.         <button class="btn btn-default">  
  17.             <i class="glyphicon glyphicon-trash"></i>  
  18.         </button>  
  19.     </div>  
  20.     <div class="form-group">  
  21.         <label class="control-label">性别:</label>  
  22.         <label class="radio-inline">  
  23.             <input type="radio" name="sex" value="男" /> 男  
  24.         </label>  
  25.         <label class="radio-inline">  
  26.             <input type="radio" name="sex" value="女" /> 女  
  27.         </label>  
  28.     </div>  
  29. </div>  

2.服务端代码处理

[csharp] view plain copy
 print?
  1. public JsonResult GetStuList(string sex, string search, string sort, string order, int offset, int limit)  
  2. {  
  3.     var query = _Context.Student.AsQueryable();  
  4.     if (string.IsNullOrEmpty(sex) == false)  
  5.         query = query.Where(q => q.Ssex == sex);  
  6.     if (string.IsNullOrEmpty(search) == false)  
  7.         query = query.Where(q => q.Sno.Contains(search) || q.Sname.Contains(search));  
  8.     //排序  
  9.     if (sort == "sno")  
  10.     {  
  11.         if (order == "asc")  
  12.             query = query.OrderBy(q => q.Sno);  
  13.         else  
  14.             query = query.OrderByDescending(q => q.Sno);  
  15.     }  
  16.     else  
  17.         query = query.OrderBy(q => q.Sbirthday);  
  18.   
  19.     int total = query.Count();  
  20.     var list = query.Skip(offset).Take(limit).ToList();  
  21.     return Json(new  
  22.     {  
  23.         rows = list,  
  24.         total = total  
  25.     });  
  26. }