AJAX分頁帶頁碼
///視圖
GoToPage(1, "pageHtml")
function GoToPage(pageindex, pageHtml) {
$.ajax({
url: "/fenye/index",
type: "post",
data: { pageindex: pageindex },
success: function (data) {
var str = "";
for (var i = 0; i < data.Titemlist.length; i++) {
str += " <tr>";
str += " <td>" + data.Titemlist[i].Id + "</td>";
str += " <td>" + data.Titemlist[i].Name + "</td>";
str += " <td>" + data.Titemlist[i].Leixing + "</td>";
str += " <td>" + data.Titemlist[i].Feiyong + "</td>";
str += " <td>" + data.Titemlist[i].Shoufeiren + "</td>";
str += " <td>" + new Date(parseInt(data.Titemlist[i].shijian.replace(/\D/img, ""))).toLocaleDateString() + "</td>";
str += " </tr>";
}
$("#tfoot1").html(str);
GetpageHtml(data.totapage, pageindex, pageHtml)
}
})
}
///控制器
public ActionResult Index(int pageindex)
{
var result = new BLL.wuyebll().show(pageindex, 6);
return Json(result);
}
///wcf
/// <summary>
/// 萬能分頁
/// </summary>
/// <param name="pageindex"></param>
/// <param name="pagesize"></param>
/// <returns></returns>
public wuyeinfo<wuyemodel> show(int pageindex,int pagesize)
{
SqlCommand cmd = new SqlCommand("fenye",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pagesize",pagesize);
cmd.Parameters.AddWithValue("@pageindex", pageindex);
cmd.Parameters.AddWithValue("@pagecount",SqlDbType.Int);
cmd.Parameters["@pagecount"].Direction = ParameterDirection.Output;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable de = new DataTable();
da.Fill(de);
var totalcount = Convert.ToInt32(cmd.Parameters["@pagecount"].Value);//把存儲過程裏的count總條數強行轉化為int類型
var ds = JsonConvert.SerializeObject(de);
var result = JsonConvert.DeserializeObject<List<wuyemodel>>(ds);
wuyeinfo<wuyemodel> list = new wuyeinfo<wuyemodel>();
list.Titemlist = result;
list.pagesize = pagesize;
list.pageindex = pageindex;
if(totalcount%pagesize==0)
{
list.totapage = totalcount / pagesize;
}
else
{
list.totapage = totalcount / pagesize + 1;
}
return list;
}
///model類
public class wuyeinfo<T>
{
public int pageindex { get; set; }
public int pagesize { get; set; }
public int totapage { get; set; }
public List<T> Titemlist { get; set; }
}
public class wuyemodel
{
public int Id{get;set;}
public string Name{get;set;}
public string Leixing{get;set;}
public int Feiyong{get;set;}
public string Shoufeiren{get;set;}
public DateTime shijian{get;set;}
}
AJAX分頁帶頁碼