使用纯HTML页与js、ajax、Linq实现分页与组合查询的配合使用
<body>
<div id="top"><input type="button" id="btn1" class="btn" value="刷新" /></div>
<div style="position:relative;text-align:center;height:50px">
商品名:<input type="text" id="tx1" />
价格<input type="text" id="tx2" />~<input type="text" id="tx3" />
<input type="button" id="btt" value="查询" style="cursor:pointer"/>
</div>
<div style="position:relative;">
<table id="tb1" style="background-color: #00ffff ; text-align: center; width: 100%;">
<thead>
<tr style="color: #ff6a00;">
<td>商品名</td>
<td>图片</td>
<td>数量</td>
<td>尺寸</td>
<td>CPU</td>
<td>价格</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<!--<tr style="background-color: white;">
<td></td>
</tr>-->
</tbody>
</table>
</div>
<div id="ye">
当前第 <span id="nowye"></span> 页  共 <span id="allye"></span> 页
<input id="btn_first" type="button" value="首页" /> <input id="btn_prev" type="button" value="上一页" /> <input type="button" id="btn_next" value="下一页" /> <input type="button" id="btn_end" value="末页" />
<select id="dr1"></select>
</div>
</body>
HTML代码
js与ajax代码:
<script>
var pcount = ;
var Mcount = ;
var aname = "";
var aprice = "";
var bprice ="";
var NowNumber = parseInt($("#nowye").text());
data();
max();
//点击刷新按钮
$("#btn1").click(function () {
data();
})
//上一页
$("#btn_prev").click(function () { NowNumber = parseInt($("#nowye").text()) - ;
if (NowNumber<)
{
return;
}
data();
}
);
//首页
$("#btn_first").click(function () { NowNumber = ;
data();
} );
//末页
$("#btn_end").click(function () { NowNumber = parseInt($("#allye").text());
data();
} );
//快捷跳转
$("#dr1").change(function () {
NowNumber = parseInt($("#dr1").val());
data();
} );
//下一页
$("#btn_next").click(function () { NowNumber = parseInt($("#nowye").text()) + ;
if (NowNumber > parseInt($("#allye").text())) {
return;
}
data();
}
);
//点击查询按钮
$("#btt").click(function () {
aname = $("#tx1").val();
aprice = $("#tx2").val();
bprice = $("#tx3").val();
NowNumber = ;
max();
data();
})
//获取总页数与绑定快捷页数
function max() {
$.ajax({
url: "ajax/allcom.ashx",
data: { "ak": aname, "bk": aprice, "ck": bprice },
type: "post",
dataType: "json",
success: function (data) {
var maxcount = data.cn;
var te = maxcount / (pcount * 1.0);
Mcount = Math.ceil(te);
$("#allye").html(Mcount);
$("#dr1").empty();
for(var i=;i<=Mcount;i++)
{
var str = "<option value=\"" + i + "\">" + i + "</option>";
$("#dr1").append(str);
} },
error: function () {
alert('服务器连接失败!!!');
},
beforeSend: function () { } });
}
//数据展现
function data() {
$.ajax({
url: "ajax/com.ashx",
data: {"un":NowNumber,"yn":pcount,"ak":aname,"bk":aprice,"ck":bprice},
type: "post",
dataType: "json",
success: function (data) {
$("#tb1 tbody").empty();//清空tbody
for (i in data) {
var str = "<tr style=\"background-color: #a9ff98; \" class=\"trs\">";
str += "<td>" + data[i].pname + "</td>";
str += "<td> <img src=\""+ data[i].pic +"\" style=\"width:60px;height:60px\"/> </td>";
str += "<td>" + data[i].pcode + "</td>";
str += "<td>" + data[i].size + "</td>";
str += "<td>" + data[i].cpu + "</td>";
str += "<td>" + data[i].price + "</td>";
str += "<td> <input type=\"button\" class=\"btn2\" value=\"删除\" /> </td>";
str += "</tr>";
$("#tb1 tbody").append(str);
$("#nowye").html(NowNumber);
$("#dr1").val(NowNumber);
}
},
error: function () {
alert('服务器连接失败!!!');
},
beforeSend: function () { } }); };
</script>
public void ProcessRequest(HttpContext context)
{
int pcount = Convert.ToInt32(context.Request["yn"]);
int pagenumber = Convert.ToInt32(context.Request["un"]);
string sname = context.Request["ak"];
int sprice1;
int sprice2;
try
{
sprice1 = Convert.ToInt32(context.Request["bk"]);
}
catch
{
sprice1 = ;
}
try
{
sprice2 = Convert.ToInt32(context.Request["ck"]);
}
catch
{
sprice2 = ;
}
int count = ;
string end = "[";
using (WebDataContext con = new WebDataContext())
{ var clist = con.Computer.AsEnumerable();
if (sname!="")
{
var namelist = con.Computer.Where(r => r.Name.Contains(sname)); clist = clist.Intersect(namelist);
}
if (sprice1 != )
{
var plist = con.Computer.Where(r => Convert.ToInt32(r.price) >= sprice1);
clist = clist.Intersect(plist);
}
if (sprice2 != )
{
var plist2 = con.Computer.Where(r => Convert.ToInt32(r.price) <= sprice2);
clist = clist.Intersect(plist2);
} clist = clist.Skip(pcount * (pagenumber - )).Take(pcount);
foreach (Computer c in clist)
{
//前面有数据
if (count > )
{
end += ",";
}
end += "{\"pname\":\"" + c.Name + "\",\"size\": \"" + c.size + "\",\"cpu\": \"" + c.Cpu + "\",\"price\": \"" + c.price + "\",\"pic\": \"" + c.pic + "\",\"pcode\":\"" + c.pcode + "\",\"pid\":\"" + c.ids + "\" }";
count++;
}
}
end += "]";
context.Response.Write(end);
context.Response.End(); }
com.ashx
public void ProcessRequest (HttpContext context) {
string sname = context.Request["ak"];
int sprice1;
int sprice2;
try
{
sprice1 = Convert.ToInt32(context.Request["bk"]);
}
catch
{
sprice1 = ;
}
try
{
sprice2 = Convert.ToInt32(context.Request["ck"]);
}
catch
{
sprice2 = ;
}
string end="";
using (WebDataContext con = new WebDataContext())
{ var clist = con.Computer.AsEnumerable();
if (sname!="")
{
var namelist = con.Computer.Where(r => r.Name.Contains(sname)); clist = clist.Intersect(namelist);
}
if (sprice1 != )
{
var plist = con.Computer.Where(r => Convert.ToInt32(r.price) >= sprice1);
clist = clist.Intersect(plist);
}
if (sprice2 != )
{
var plist2 = con.Computer.Where(r => Convert.ToInt32(r.price) <= sprice2);
clist = clist.Intersect(plist2);
}
end = "{\"cn\":\"" + clist.Count() + "\"}";
}
context.Response.Write(end);
context.Response.End();
}
allcom.ashx