MVCJSONJQuery分页实现
思路:
1、用Ado.NET获取数据
2、控制器中创建一个方法参数为搜索条件
3、返回前台一个Json对象,把对象用一个类封装
4、用JQuery接收数据
public ActionResult IntroduceCustomerList(string CustomerName, string CustomerMobile, string CustomerNo, string ElectronMemberIDCard,int? idx) { //搜素条件 StringBuilder strwhere = new StringBuilder(); if (CustomerName!=null &&CustomerName!="") { strwhere.Append( " ct.RealName like'" +'%' + CustomerName +'%' + "' "); strwhere.Append(" and "); } if (CustomerMobile != null && CustomerMobile != "") { strwhere.Append(" ct.Mobile like'" +'%' + CustomerMobile + '%' + "' "); strwhere.Append(" and "); } if (CustomerNo != null && CustomerNo != "") { strwhere.Append(" MemberIDCard like'" + '%' + CustomerNo + '%' +"' "); strwhere.Append(" and "); } if (ElectronMemberIDCard != null && ElectronMemberIDCard != "") { strwhere.Append(" ElectronMemberIDCard like'" + '%' + ElectronMemberIDCard + '%' + "' "); strwhere.Append(" and "); } return Json(cySearch, JsonRequestBehavior.AllowGet); }
Ado取数据:
public List<Customer> ChunYuIntroduceCustomerList( int pageIndex,int pageSize,out int pageCount, string strWhere) { SELECT DISTINCT customeruserid FROM ( SELECT ROW_NUMBER() OVER(Order by customeruserid DESC ) AS RowId,* FROM( SELECT DISTINCT o.customeruserid FROM orders o WITH(NOLOCK) }
JQuery时间戳的处理:
function getLocalTime(nS) { //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' '); var ydate = new Date(nS * 1000); var year = ydate.getFullYear(); var mouth = ydate.getMonth(); var day = ydate.getDate(); return year + "-" + (mouth+1) + "-" + day; } function getLocalTimeHou(nS) { //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' '); var ydate = new Date(nS * 1000); var year = ydate.getFullYear(); var mouth = ydate.getMonth(); var day = ydate.getDate(); var hous = ydate.getHours(); var mini = ydate.getMinutes(); return year + "-" + (mouth + 1) + "-" + day + " " + hous + ":" + mini; }
分页和接收数据拼接URL地址:
$("#layerBtnConfirmIntroduceCustomerList1").click(function () { if(idx-1<=0) { return false; } idx=idx-1; LodinigSearch() }) //下一页 $("#layerBtnConfirmIntroduceCustomerList2").click(function () { if (idx + 1 > rowCount) { return false; } idx=idx+1; LodinigSearch() }) //尾页 $("#layerBtnConfirmIntroduceCustomerList3").click(function () { idx = rowCount; LodinigSearch() }) //首页 $("#layerBtnConfirmIntroduceCustomerList4").click(function () { idx = 1; LodinigSearch() }) function LodinigSearch() { //顾客姓名 var CustomerName = $("input[name='CustomerName']").val(); //电话 var CustomerMobile = $("input[name='CustomerMobile']").val(); //卡号 var CustomerNo = $("input[name='CustomerNo']").val(); //电子会员卡号 var ElectronMemberIDCard = $("input[name='ElectronMemberIDCard']").val(); var ChunYuSearch = { "CustomerName": CustomerName, "CustomerMobile": CustomerMobile, "CustomerNo": CustomerNo, "ElectronMemberIDCard": ElectronMemberIDCard, "idx": idx }; var url = "/Customer/IntroduceCustomerList?CustomerName=" + CustomerName + "&CustomerMobile="
+ CustomerMobile + "&CustomerNo=" + CustomerNo + "&ElectronMemberIDCard=" + ElectronMemberIDCard + "&idx=" + idx; //var url = "/Customer/IntroduceCustomerList"; _ajaxSubmitForm(url, { model: ChunYuSearch }, //_ajaxSubmitForm(url, function (data) { var sex = ''; var date = ''; var dateLast = ''; var dateJoinTime = ''; var tb = ""; rowCount = data.pageTotal; var pageindex = ""; pageindex = data.pageindex; for (var i = 0; i < data.list.length; i++) { if (data.list[i].GenderCode == 1) { sex = "男"; } else { sex = "女" } if (data.list[i].Birthday != null && data.list[i].Birthday != '') { date = data.list[i].Birthday; date = getLocalTime(date.substr(6, 9)); } else { date == date; } if (data.list[i].LastTakeTime != null && data.list[i].LastTakeTime != '') { dateLast = data.list[i].LastTakeTime; dateLast = getLocalTimeHou(dateLast.substr(6, 10)); } else { dateLast == dateLast; } if (data.list[i].JoinTime != null && data.list[i].JoinTime != '') { dateJoinTime = data.list[i].JoinTime; dateJoinTime = getLocalTimeHou(dateJoinTime.substr(6, 10)); } else { dateJoinTime = dateJoinTime; } tb += "<tr><td>"; tb += "<input type='checkbox' id='oneCheck' name='oneBox' onclick=\"checkOne(this,'" +
data.list[i].RealName + "'+'" + '(' + data.list[i].MemberIDCard + ')' + "','" + data.list[i].CustomerUserId + "')\" />"; tb += "<td id='rel'>" + data.list[i].RealName + '(' + data.list[i].MemberIDCard + ')';//顾客姓名(卡号) tb += "<td>" + data.list[i].CorporationName + "</td>";//所属分公司 tb += "<td>" + data.list[i].OrgFullName + "</td>";//所属门店 tb += "<td>" + sex + "</td>";//性别 tb += "<td>" + date + "</td>";//出生日期 tb += "<td>" + dateLast + "</td>";//最近一次提取时间呢 tb += "<td>" + dateJoinTime + "</td>";//建档时间 tb += "<td>" + data.list[i].CreatedOrgName + "</td>";//建档部门 tb += "<td>" + data.list[i].CreatedStaffName + '(' + data.list[i].CreatedStaffNo + ')' + "</td></tr>"; } $("#chunyu").html(tb); $("#rowCount").val(rowCount); $("#txrowCount").text(rowCount); $("#txrowIndex").text(idx); }, function (data) { //common.errResultMessage("出错了", "", "") } , true); } function checkOne(oneBox, val, id) { $("#txtcyIntroducer").val(''); $("#cyIntroducer").val(''); var obj = document.getElementsByName("oneBox"); for (i = 0; i < obj.length; i++) { //判斷obj集合中的i元素是否為cb,若否則表示未被點選 if (obj[i] != oneBox) obj[i].checked = false; } $("#txtcyIntroducer").val(val); $("#cyIntroducer").val(id); }