查询
有条件查询:
表格加载: 加载layui模块
layui.use(['table'], function () { layuiTable = layui.table;
//渲染表格:
tabStudent = layuiTable.render({ //tabStudent全局变量, layuiTable=layui.table;
elem: "#tabStudent",//table的ID HTML元素的ID
url: "/Main/SelectStudents",//数据接口 方法的路径
cols: [[
{ title:'序号',type:'numbers'},
//numbers 序号列,title设定标题名称,width: 160 指定宽度,align 指定对其方式
{ title: '学生编号', field: 'studentNumber', align: 'center' },
{ title: '学生姓名', field: 'studentName', align: 'center' },
{ title: '班级', field: 'calssName', align: 'center' },
{ title: '性别', field: 'studentSex', align: 'center' },
{ title: '手机号', field: 'telephone', align: 'center' },
{ title: '身份证号', field: 'studentIDCard', align: 'center' },
{ title: '图片', templet: showPicture, align: 'center' },
{ title: '操作', templet: setOperate, width: 160, align: 'center' }
]],
page: true //开启分页 });});
多条件查询table:
function tabStudentSearch(){//获取页面输入数据
var studentNumber = $("#studentNumber").val();//编号
if (studentNumber == undefined) { studentNumber = ""; }
var studentName = $("#studentName").val();//姓名
if (studentName == undefined) { studentName = ""; }
var classID = $("#classID").val();//班级ID
if (classID == "" || classID == undefined) { classID = 0; }
var studentIDCard = $("#studentIDCard").val();//身份证号
if (studentIDCard == undefined) { studentIDCard = ""; }
tabStudent.reload({//表格重载
where: {
studentNumber: studentNumber, //编号
studentName: studentName, //姓名
classID: classID, //班级ID
studentIDCard: studentIDCard, //身份证号
},page: { curr: 1//重新从第一页开始 } }); }
后台去数据库查询出需要的数据:
public ActionResult SelectStudents(LayuiTablePage layuiTablePage, string studentNumber,
string studentName, int? classID, string studentIDCard) {
var listStudent = from tbStudent in myModel.S_Student
join tbClass in myModel.S_Class on tbStudent.classID equals tbClass.classID
select new StudentVo {
studentID=tbStudent.studentID,//学生ID
classID=tbStudent.classID,//班级ID
studentNumber=tbStudent.studentNumber,//学生编号
studentName=tbStudent.studentName,//学生姓名
calssName=tbClass.calssName,//班级
studentSex=tbStudent.studentSex,//性别
telephone=tbStudent.telephone,//手机号
studentIDCard = tbStudent.studentIDCard,//身份证号
studentPicture=tbStudent.studentPicture,//图片 };
//条件筛选
if (!string.IsNullOrEmpty(studentNumber)) //编号 { Contains模糊查询
listStudent = listStudent.Where(m => m.studentNumber.Contains(studentNumber)); }
if (!string.IsNullOrEmpty(studentName)) //姓名 {
listStudent = listStudent.Where(m => m.studentName.Contains(studentName)); }
if (classID > 0) //班级ID {
listStudent = listStudent.Where(m => m.classID == classID); }
if (!string.IsNullOrEmpty(studentIDCard))//身份证号{
listStudent = listStudent.Where(m => m.studentIDCard.Contains(studentIDCard)); }
//分页查询学生数据
List<StudentVo> listStu= listStudent
.OrderByDescending(m => m.studentID)//根据学生ID倒叙排序
.Skip(layuiTablePage.GetStartIndex())//跳过前面页数的数据
.Take(layuiTablePage.limit)//查询本页数据的条数
.ToList();//返回List集合
//查询学生数据的总条数
int intTotalRow = myModel.S_Student.Count();
//准备Layui Table所需的数据格式(泛型)
LayuiTableData<StudentVo> layuiTableData = new LayuiTableData<StudentVo>() {
count = intTotalRow,//总条数
data = listStu,//本页的数据 };
//返回Json
return Json(layuiTableData, JsonRequestBehavior.AllowGet);}