~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:VS MVC.NET
作者:周乐献
撰写时间:2020年8月18日
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
当DOM载入就绪可以查询及操纵时查询出全部的学生数据数据
$(document).ready(function () {
layui.use(['layer', 'table'], function () {
layer = layui.layer;
layTable = layui.table;
TabStudent = layTable.render({
elem: '#tabStudent',
data: [],
cols: [[
{ type: 'numbers', title: '序号', width: 100 },
{ field: 'studentNumber', title: '学生编号', align: 'center' },
{ field: 'studentName', title: '学生姓名', align: 'center' },
{ field: 'calssName', title: '班级', align: 'center' },
{ field: 'studentSex', title: '性别', align: 'center' },
{ field: 'telephone', title: '手机号', align: 'center', width: 180, },
{ field: 'studentIDCard', title: '身份证号', align: 'center', width: 220, },
{ templet: InquirePicture, title: '图片', align: 'center' },
{ templet:operation, title: '操作', align: 'center' },
]],
page: {
limit: 10,
limits:[5,10,15,20,25,30,50],
}
})
InquireStudent();
})
})
然后根据条件筛选学生数据
function InquireStudent() {
var studentNumber = $("#studentNumber").val();
var studentName = $("#studentName").val();
var classID = $("#classID").val();
var studentIDCard = $("#studentIDCard").val();
TabStudent = layTable.reload("tabStudent",{
url:"@Url.Content("~/Main/InquireStudentInfo")",
where: {
studentNumber: studentNumber,
studentName: studentName,
classID: classID,
studentIDCard: studentIDCard
},
page: {
curr:1
},
})
}
在控制器根据页面传递过来的筛选条件对所以的学生数据进行筛选,然后再把符合筛选条件的数据返回到页面
public ActionResult InquireStudentInfo(LayuiTablePage layuiTablePage, string studentNumber, string studentName, int? classID, string studentIDCard)
{
try
{
var dbStudent = (from tbStudent in myModel.S_Student
join tbClass in myModel.S_Class on tbStudent.classID equals tbClass.classID
orderby tbStudent.studentID descending
select new StudentVo()
{
studentID = tbStudent.studentID,
classID = tbClass.classID,
calssName = tbClass.calssName,
studentNumber = tbStudent.studentNumber,
studentName = tbStudent.studentName,
studentSex = tbStudent.studentSex,
telephone = tbStudent.telephone,
studentIDCard = tbStudent.studentIDCard,
studentPicture = tbStudent.studentPicture,
});
//筛选条件----学生编号
if (!string.IsNullOrEmpty(studentNumber))
{
dbStudent = dbStudent.Where(m => m.studentNumber.Contains(studentNumber));
}
//筛选条件----学生姓名
if (!string.IsNullOrEmpty(studentName))
{
dbStudent = dbStudent.Where(m => m.studentName.Contains(studentName));
}
//筛选条件----班级
if (classID > 0)
{
dbStudent = dbStudent.Where(m => m.classID == classID);
}
//筛选条件----学生身份证号
if (!string.IsNullOrEmpty(studentIDCard))
{
dbStudent = dbStudent.Where(m => m.studentIDCard.Contains(studentIDCard));
}
int StudentCount = dbStudent.Count();
List<StudentVo> listStudent = dbStudent
.Skip(layuiTablePage.GetStartIndex())
.Take(layuiTablePage.limit)
.ToList();
LayuiTableData<StudentVo> layuiTableData = new LayuiTableData<StudentVo>()
{
count = StudentCount,
data = listStudent,
};
return Json(layuiTableData, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
Console.WriteLine(e);
return Content("数据异常");
}
}
这里的筛选条件用的是模糊查询,只要学生数据中对应的信息里包含有筛选的条件就能把含有筛选条件的学生数据查询出来;
1.不进行筛选时所有的学生数据
2.把学生编号包含10的学生数据查询出来, 不符合的则不显示
3.根据学生姓名进行筛选
4.根据班级进行筛选
5.根据身份证进行筛选