Jquery+Ajax+Json+存儲進程完成高效分頁。本站提示廣大學習愛好者:(Jquery+Ajax+Json+存儲進程完成高效分頁)文章只能為提供參考,不一定能成為您想要的結果。以下是Jquery+Ajax+Json+存儲進程完成高效分頁正文
之前在做分頁時,許多同伙都是用Jquery分頁插件,之前我就用的jquery.paper,有須要的同伙可以接洽我,接上去小編給年夜家分享用Jquery+Ajax+Json+存儲進程完成高效分頁。
完成此功效用分頁存儲進程,pagination,js款式,空話不多了,詳細請看上面代碼
分頁存儲進程:PAGINATION
CREATE PROCEDURE [dbo].[PAGINATION] @FEILDS VARCHAR(),--要顯示的字段 @PAGE_INDEX INT,--以後頁碼 @PAGE_SIZE INT,--頁面年夜小 @ORDERTYPE BIT,--當為時 則為 desc 當為 時 asc @ANDWHERE VARCHAR()='',--where語句 不消加where @ORDERFEILD VARCHAR(), --排序的字段 @TABLENAME VARCHAR() --查詢的注解 as DECLARE @EXECSQL VARCHAR() DECLARE @ORDERSTR VARCHAR() DECLARE @ORDERBY VARCHAR() BEGIN set NOCOUNT on IF @ORDERTYPE = BEGIN SET @ORDERSTR = ' > ( SELECT MAX(['+@ORDERFEILD+'])' SET @ORDERBY = 'ORDER BY '+@ORDERFEILD+' ASC' END ELSE BEGIN SET @ORDERSTR = ' < ( SELECT MIN(['+@ORDERFEILD+'])' SET @ORDERBY = 'ORDER BY '+@ORDERFEILD+' DESC' END IF @PAGE_INDEX = --當頁碼是第一頁時直接運轉,進步速度 BEGIN IF @ANDWHERE='' SET @EXECSQL = 'SELECT TOP '+STR(@PAGE_SIZE)+' '+@FEILDS+' FROM '+@TABLENAME+' '+@ORDERBY ELSE SET @EXECSQL = 'SELECT TOP '+STR(@PAGE_SIZE)+' '+@FEILDS+' FROM '+@TABLENAME+' WHERE '+@ANDWHERE+' '+ @ORDERBY END ELSE BEGIN IF @ANDWHERE='' BEGIN --以子查詢成果當作新表時 要給表名別號能力用 SET @EXECSQL = 'SELECT TOP'+STR(@PAGE_SIZE)+' '+@FEILDS+' FROM '+@TABLENAME+' WHERE '+@ORDERFEILD+ @ORDERSTR+' FROM (SELECT TOP '+STR(@PAGE_SIZE*(@PAGE_INDEX-))+' '+@ORDERFEILD+ ' FROM '+@TABLENAME+' '+@ORDERBY+') AS TEMP) '+ @ORDERBY END ELSE BEGIN SET @EXECSQL = 'SELECT TOP'+STR(@PAGE_SIZE)+' '+@FEILDS+' FROM '+@TABLENAME+' WHERE '+@ORDERFEILD+ @ORDERSTR+' FROM (SELECT TOP '+ STR(@PAGE_SIZE*(@PAGE_INDEX-))+' '+@ORDERFEILD+ ' FROM '+@TABLENAME+' WHERE '+@ANDWHERE+' '+@ORDERBY+') AS TEMP) AND '+@ANDWHERE+' '+ @ORDERBY END END EXEC (@EXECSQL)--這裡要加括號 END
分頁款式:
<style type="text/css"> a,area { -moz-outline-style: none; blr:expression(this.onFocus=this.blur()); text-decoration:none} div.badoo { padding:px; text-align:center; } div.badoo a { border:px solid #ededed; padding:px px; color:#; border-radius:px; margin-right:px;} div.badoo a:hover {border:px solid #ffa; color: #ffa; } div.badoo a:active {border:px solid #ffa; margin-right:px;} div.badoo span { border:px solid #EDEDED; padding:px px; color:#f;font-weight:bold; background:#FAFAFA; border-radius:px; margin-right:px;} div.badoo span.disabled { border:px solid #EDEDED; padding:px px; color:#; margin-right:px; font-weight:;} </style>
起首創立普通處置法式,讀取數據庫中內容,獲得前往值.
創立文件,GetData.ashx.
我這裡是用的存儲進程,存儲進程會再上面粘出來,至於數據只是實例,你們可依據需求自行讀取數據
using System; using System.Web; using System.Data.SqlClient; using System.Data; using System.Collections.Generic; using System.Web.Script.Serialization; using Model; context.Response.ContentType = "text/plain"; var pageIndex = context.Request["PageIndex"]; //斷定以後索引存不存在,假如不存在則獲得記載的總數。 if (string.IsNullOrEmpty(pageIndex)) { //獲得查詢記載總數的sql語句 int count = ; int.TryParse(new BLL.t_profit().SelectAllNum(), out count); context.Response.Write(count); context.Response.End(); } //當依據索引獲得數據 else { int currentPageIndex = ; int.TryParse(pageIndex, out currentPageIndex); SqlParameter[] parms = new SqlParameter[] { new SqlParameter("@FEILDS",SqlDbType.NVarChar,), new SqlParameter("@PAGE_INDEX",SqlDbType.Int,), new SqlParameter("@PAGE_SIZE",SqlDbType.Int,), new SqlParameter("@ORDERTYPE",SqlDbType.Int,), new SqlParameter("@ANDWHERE",SqlDbType.VarChar,), new SqlParameter("@ORDERFEILD",SqlDbType.VarChar,) }; parms[].Value = "id,name,sex,tel";//獲得一切的字段 parms[].Value = pageIndex;//以後頁面索引 parms[].Value = ;//頁面年夜小 parms[].Value = ;//升序分列 parms[].Value = "";//前提語句 parms[].Value = "id";//排序字段 List<Book> list = new List<Book>(); using (SqlDataReader sdr = Yoodor.DAL.SqlHelper.ExecuteReader(CommandType.StoredProcedure, "PAGINATION", parms)) { while (sdr.Read()) { list.Add(new Book { id = sdr[].ToString(), name = sdr[].ToString(), sex = sdr[].ToString(), tel = sdr[].ToString() }); } } context.Response.Write(new JavaScriptSerializer().Serialize(list).ToString());//轉為Json格局 } public string id { get; set; } public string name { get; set; } public string sex { get; set; } public string tel { get; set; }
款式代碼:
<style type="text/css"> a,area { -moz-outline-style: none; blr:expression(this.onFocus=this.blur()); text-decoration:none} div.badoo { padding:px; text-align:center; } div.badoo a { border:px solid #ededed; padding:px px; color:#; border-radius:px; margin-right:px;} div.badoo a:hover {border:px solid #ffa; color: #ffa; } div.badoo a:active {border:px solid #ffa; margin-right:px;} div.badoo span { border:px solid #EDEDED; padding:px px; color:#f;font-weight:bold; background:#FAFAFA; border-radius:px; margin-right:px;} div.badoo span.disabled { border:px solid #EDEDED; padding:px px; color:#; margin-right:px; font-weight:;} </style>
js代碼
<script type="text/javascript"> $(function () { $.post("GetData.ashx", null, function (data) { var total = data; PageClick(, total, ); }); PageClick = function (pageIndex, total, spanInterval) { $.ajax({ url: "GetData.ashx", data: { "PageIndex": pageIndex }, type: "post", dataType: "json", success: function (data) { //索引從開端 //將以後頁索引轉為int類型 var intPageIndex = parseInt(pageIndex); //獲得顯示數據的表格 var table = $("#content"); //清晰表格中內容 $("#content tr").remove(); //向表格中添加內容 for (var i = ; i < data.length; i++) { table.append( $("<tr><td>" + data[i].id + "</td><td>" + data[i].name + "</td><td>" + data[i].sex + "</td><td>" + data[i].tel + "</td></tr>") ); } //for //創立分頁 //將總記載數成果 獲得 總頁碼數 var pageS = total if (pageS % == ) pageS = pageS / ; else pageS = parseInt(total / ) + ; var $pager = $("#pager"); //清晰分頁div中的內容 $("#pager span").remove(); $("#pager a").remove(); //添加第一頁 if (intPageIndex == ) { // $pager.append("<span class='disabled'>第一頁</span>"); } else { // var first = $("<a href='javascript:void()' first='" + + "'>第一頁</a>").click(function () { // PageClick($(this).attr('first'), total, spanInterval); // return false; // }); // $pager.append(first); } //添加上一頁 if (intPageIndex == ) $pager.append("<span class='disabled'>上一頁</span>"); else { var pre = $("<a href='javascript:void()' pre='" + (intPageIndex - ) + "'>上一頁</a>").click(function () { PageClick($(this).attr('pre'), total, spanInterval); return false; }); $pager.append(pre); } //設置分頁的格局 這裡可以依據需求完成本身想要的成果 var interval = parseInt(spanInterval); //設置距離 var start = Math.max(, intPageIndex - interval); //設置肇端頁 var end = Math.min(intPageIndex + interval, pageS)//設置末頁 if (intPageIndex < interval + ) { end = ( * interval + ) > pageS ? pageS : ( * interval + ); } if ((intPageIndex + interval) > pageS) { start = (pageS - * interval) < ? : (pageS - * interval); } //生成頁碼 for (var j = start; j < end + ; j++) { if (j == intPageIndex) { var spanSelectd = $("<span class='current'>" + j + "</span>"); $pager.append(spanSelectd); } //if else { var a = $("<a href='javascript:void()'>" + j + "</a>").click(function () { PageClick($(this).text(), total, spanInterval); return false; }); $pager.append(a); } //else } //for //上一頁 if (intPageIndex == Math.ceil(total / )) { $pager.append("<span class='disabled'>下一頁</span>"); } else { var next = $("<a href='javascript:void()' next='" + (intPageIndex + ) + "'>下一頁</a>").click(function () { PageClick($(this).attr("next"), total, spanInterval); return false; }); $pager.append(next); } //最初一頁 if (intPageIndex == pageS) { // $pager.append("<span class='disabled'>最初一頁</span>"); } else { // var last = $("<a href='javascript:void()' last='" + pageS + "'>最初一頁</a>").click(function () { // PageClick($(this).attr("last"), total, spanInterval); // return false; // }); // $pager.append(last); } } //sucess }); //ajax }; //function }); //ready </script> <table id="content"> </table> <div id="pager" class="badoo"></div> /// <summary> /// Execute a SqlCommand that returns a resultset against the database specified in the connection string /// using the provided parameters. /// </summary> /// <param name="connectionString">一個有用的數據庫銜接字符串</param> /// <param name="cmdType">SqlCommand敕令類型 (存儲進程, T-SQL語句, 等等。)</param> /// <param name="cmdText">存儲進程的名字或許 T-SQL 語句</param> /// <param name="commandParameters">以數組情勢供給SqlCommand敕令頂用到的參數列表</param> /// <returns>A SqlDataReader containing the results</returns> public static SqlDataReader ExecuteReader( CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connectionString); // we use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandBehaviour.CloseConnection will not work try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch (Exception ex) { conn.Close(); throw new Exception(ex.Message); } }
以上就是本文應用Jquery+Ajax+Json+存儲進程完成高效分頁的全體內容,願望年夜家愛好。