开发工具与关键技术:Visual Studio、MVC
作者:幻奏
撰写时间:2019.04.13
我之前已经写过新增了,那么现在我来写一下查询。查询很多地方都会用到吧,比如我们要让数据在表格显示出来就要用查询来让表格数据初始化,还有绑定下拉框查询什么的。。。好了,话不多说了就进入今天的讲解吧。
讲查询之前有一个东西要讲一下的,那就是如何进行表格初始化的。在这过程中我们要用到一个《layui》,这个jQuery插件很好用。我们先把它引到视图里,再声明两个变量。
var tabTitles;
var layer, layuiTable;
最后看下它的数据表格的使用方法,复制过来然后初始化layui。如下:
//加载&初始化layui模块layui.use(['layer', 'table'], function () {layer = layui.layer;layuiTable = layui.table;tabTitles = layuiTable.render({elem: '#tabTitles',//url: 'selectStaffInfor',cols: [[{ type: 'checkbox' },{ type: 'numbers', title: '序号' },{ field: 'EmployeeID', title: 'EmployeeID', hide: true },{ field: 'EmployeeNumber', title: '编号', align: 'center' },{ field: 'EmployeeName', title: '姓名', align: 'center' },{ field: 'Phone', title: '电话', align: 'center' },{ field: 'Cellphone', title: '手机', align: 'center' },{ field: 'DeoartmentName', title: '部门', align: 'center' },{ field: 'PositionName', title: '职位', align: 'center' },{ field: 'Address', title: '地址', align: 'center' },{ field: 'Remark', title: '备注', align: 'center' },]],page: {limit: 10,//每页显示的条数limits: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],//每页条数的选择项},data: []});//监听表格行单击事件layuiTable.on('row(tabTitles)', function (obj) {//console.log(obj);//标注选中行样式obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");//选中行,勾选复选框obj.tr.find("div.layui-unselect.layui-form-checkbox")[0].click();});searchYuago();//多条件查询的方法});
初始化layui模块,大概就是复制过来用就是了,不过我们还要把查询按钮的方法放里面,要不然表格出不来。我们还要一个监听表格行的单击事件,也是layui里的方法。如上
我们如果用了模态框,那么在查询之前是不是要清空一下输入框里面的数据,要不然可能有残留的数据,给人不好的体验。我们要获取输入框的ID然后赋值空就OK了。我们还要绑定一个方法到下拉框,我会在最后给你们看下。
//弹出组合的查询模态框function opSearchModal() {//清空姓名信息$("#nameNumberType").val("");//绑定部门下拉框createSelect("Department", "/Page/selectDepartment");//绑定职位下拉框createSelect("Position", "/Page/selectPosition");//弹出模态框$("#modalCombox").modal("show");}
到查询的按钮了,当我们点击时要获取所有的查询条件,然后判断数据是否为空或者undefined,因为下拉框的ID如果为空的话,浏览器会报错。所以我们要判断并让它等于0,但输入框的字符串可以为空,只要判断它不为undefined就行了。数据不为空就进行数据表格的重载,这个重载也是那个插件的方法。
//组合查询function searchYuago() {//获取所有的查询条件//部门var departmentId = $("#Department").val();//职位var positionId = $("#Position").val();//姓名var employeeNumber = $("#nameNumberType").val();if (departmentId == "" || departmentId == undefined) {departmentId = 0;}if (positionId == "" || positionId == undefined) {positionId = 0;}if (employeeNumber == undefined) {employeeNumber = "";}//表格数据重载tabTitles.reload({url: '/Page/selectStaffInfor',where: {DepartmentID: departmentId,PositionID: positionId,EmployeeNumber: employeeNumber},page: {curr: 1}});//关闭组合查询的模态框$("#modalCombox").modal("hide");}
注意:视图写的ID要和传到控制器的ID一样。在写之前我们要创建两个实体类,一个是LayuiTableData类的封装,用于返回layuiTable数据。
public class LayuiTableData<T>{/// <summary>/// 数据状态码 -- 可以不设置/// </summary>public int code { get; set; }/// <summary>/// 状态信息 -- 可以不设置/// </summary>public string msg { get; set; }/// <summary>/// 总行数--必须/// </summary>public int count { get; set; }/// <summary>/// 数据--必须/// </summary>public List<T> data { get; set; }}
还有一个是LayuiTablePage类的封装,这样就不用每次都请求分页的数据了。
/// <summary>/// layui table组件分页请求数据封装/// </summary>public class LayuiTablePage{/// <summary>/// page 代表当前页码/// </summary>public int page { get; set; }/// <summary>/// limit 代表每页数据量/// </summary>public int limit { get; set; }//方法/// <summary>/// 分页开始序号/// </summary>/// <returns></returns>public int GetStartIndex(){return (page - 1) * limit;}/// <summary>/// 分页结束序号/// </summary>/// <returns></returns>public int GetEndIndex(){return page * limit - 1;}}
实体类的命名第一个字母最好都大写。
在控制器那里我们要先接收页面传过来的参数。
然后用linq查询出你要的所有数据,注意:这里一定要用orderby排序,要不然后面分页查询时会不行。
List<EmployeeVo> listEmployee = (from tbEmployee in myModels.PW_Employeejoin tbDeoartmentName in myModels.SYS_Department on tbEmployee.DepartmentID equals tbDeoartmentName.DepartmentIDjoin tbPositionName in myModels.SYS_Position on tbEmployee.PositionID equals tbPositionName.PositionIDorderby tbEmployee.EmployeeIDselect new EmployeeVo{EmployeeID = tbEmployee.EmployeeID,//员工IDEmployeeNumber = tbEmployee.EmployeeNumber,//员工编号EmployeeName = tbEmployee.EmployeeName,//员工姓名Cellphone = tbEmployee.Cellphone,//手机号码Phone = tbEmployee.Phone,//电话Remark = tbEmployee.Remark,//备注Address = tbEmployee.Address,//地址DeoartmentName = tbDeoartmentName.DeoartmentName,//部门PositionName = tbPositionName.PositionName,//职位DepartmentID = tbEmployee.DepartmentID,PositionID = tbEmployee.PositionID}).ToList();
查询出数据后我们就要对视图传过来的参数进行判断,不能为空或null,然后再对数据进行条件筛选,把我们要查询的数据用lamb表达式查出。如下:
//条件筛选if (DepartmentID > 0){listEmployee = listEmployee.Where(m => m.DepartmentID == DepartmentID).ToList();}if (PositionID > 0){listEmployee = listEmployee.Where(m => m.PositionID == PositionID).ToList();}if (!string.IsNullOrEmpty(EmployeeNumber)){listEmployee = listEmployee.Where(m => m.EmployeeName.Contains(EmployeeNumber) || m.EmployeeNumber.Contains(EmployeeNumber)).ToList();}
然后,我们要计算数据的总条数。
//计算数据总条数int totalRow = listEmployee.Count();
再进行分页查询
//分页查询List<EmployeeVo> dbEmployee = listEmployee.Skip(layuiTablePage.GetStartIndex())//分页开始的序号.Take(layuiTablePage.limit)//limit每页显示的条数.ToList();
最后实例化一下数据,并return 返回数据到页面。
//实例化LayuiTableData<EmployeeVo> layuiTableData = new LayuiTableData<EmployeeVo>{ //赋值count = totalRow,//总行数data = dbEmployee//数据};return Json(layuiTableData, JsonRequestBehavior.AllowGet);
下面是绑定下拉框的方法,我就不说了,你们自己看一下吧。
/// <summary>/// 查询部门/// </summary>/// <returns></returns>public ActionResult selectDepartment(){List<SelectVo> listDepartment = (from tbDepartment in myModels.SYS_Departmentselect new SelectVo{id = tbDepartment.DepartmentID,text = tbDepartment.DeoartmentName}).ToList();//拼接选择项listDepartment = Common.Tools.SetSelectJson(listDepartment);return Json(listDepartment, JsonRequestBehavior.AllowGet);}
查询就到这里结束了,你们学会了没(o゜▽゜)o☆