MVC+EasyUI+三層新聞網(wǎng)站建立 分頁查詢數(shù)據(jù)功能(七)
MVC新聞網(wǎng)站建立,完成分頁查詢數(shù)據(jù)功能。
1、在Model里面建立NewInfo(里面存放的是新聞信息的實體信息)
然后在DAL層中建立NewInfoDal (里面存放對新聞信息的操作)
寫入分頁查詢的代碼
/// <summary> /// 分頁查詢 /// </summary> /// <param name="start">分頁開始條數(shù)</param> /// <param name="end">分頁結(jié)束條數(shù)</param> /// <returns>返回查詢到的list集合</returns> public List<NewInfo> GetPageEntityList(int start,int end) { string sql = "select * from(select row_number()over(order by id)as num,*from T_News)as t where t.num>=@start and t.num<=@end"; SqlParameter[] pms = { new SqlParameter("@start",SqlDbType.Int), new SqlParameter("@end",SqlDbType.Int), }; pms[0].Value = start; pms[1].Value = end; DataTable dt = SqlHelper.ExcuteDataTable(sql,CommandType.Text,pms); List<NewInfo> newList = null; if (dt.Rows.Count>0) { newList = new List<NewInfo>(); NewInfo newinfo = null; foreach (DataRow item in dt.Rows) { newinfo = new NewInfo(); LoadEntity(item,newinfo); newList.Add(newinfo); } } return newList; } /// <summary> /// 查詢出頁面條數(shù) /// </summary> /// <returns></returns> public int GetRecordCount() { string sql = "select count(*) from T_News"; int count = Convert.ToInt32(SqlHelper.ExecuteScalar(sql,CommandType.Text)); return count; }
在BLL層中建立NewInfoServices(里面存放對新聞信息的邏輯處理)
DAL.NewInfoDal NewInfoDal = new DAL.NewInfoDal(); /// <summary> /// 分頁查詢數(shù)據(jù) /// </summary> /// <param name="pageIndex">當(dāng)前頁碼值</param> /// <param name="pageSize">一個多少條數(shù)據(jù)</param> /// <returns></returns> public List<NewInfo> GetPageEntityList(int pageIndex, int pageSize) { int start = (pageIndex - 1) * pageSize + 1; int end = pageSize * pageIndex; return NewInfoDal.GetPageEntityList(start,end); } /// <summary> /// 查詢出頁面的記錄數(shù) /// </summary> /// <returns></returns> public int GetRecordCount() { return NewInfoDal.GetRecordCount(); }
我們把新聞管理的url指定為/NewInfo/Index
那么就要新建NewInfo控制器 Index視圖就是新聞管理頁面的主頁了。
新聞管理主頁的布局很簡單就是一個表格,所以就先在body里面寫了一表格
<body> <div> <table id="tt"></table> </div> </body/>
這里用到的是easyui的框架,所以先引用文件。
然后就是通過寫js代碼來顯示出表格的行和列
<script type="text/javascript"> $(function () { //初始化表格 initTable(); }); //初始化表格 function initTable() { $("#tt").datagrid({ //指向一個地址,當(dāng)表格加載完成后自動請求該地址 //自動向后臺發(fā)送 rows 當(dāng)前頁多少條數(shù)據(jù) page:當(dāng)前頁 //要求返回的數(shù)據(jù)json對象 {total:200,rows:[{},{}]} url: '/NewInfo/ShowNewsList', title: "新聞管理", fitColumns: true, height: $(window).height()-10, idField: 'Id', //后臺返回數(shù)據(jù)中的主鍵列。一定注意大小寫。 loadMsg: "正在加載新聞信息........", pagination: true, //啟用分頁 singleSelect: true, //只允許選中一行 pageSize: 10, //一頁默認(rèn)多少條 pageNumber: 1, //默認(rèn)頁 rownumbers: true,//行號 pageList: [10, 20, 30], //允許一頁多少條數(shù)據(jù) queryParams: {}, //異步請求可以額外傳遞的數(shù)據(jù) columns: [[ { field: 'ck', checkbox: true, align: 'left', width: 50 }, // 設(shè)置cheakbox { field: 'Title', title: '標(biāo)題', width: 120 }, { field: 'SubDateTime', title: '發(fā)布時間', width: 80, formatter: ChangeDateFormat, }, { field: 'Author', title: '作者', width: 80 }, { field: 'operate', title: '操作', align: 'center', width: $(this).width() * 0.1, formatter: function (value, row, index) { var str = ""; str += '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="detail" id="detail" class="easyui-linkbutton" onclick="showDetail('+row.Id+')"></a>'; str += ' ', str += '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="update" id="update" class="easyui-linkbutton" onclick="updateNewInfo(' + row.Id + ')" ></a>'; str += ' ', str += '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="delete" id="delete" class="easyui-linkbutton" onclick="deleteNewInfo(' + row.Id + ')" ></a>'; return str; } } ]], onLoadSuccess: function (data) { $("a[name='detail']").linkbutton({ text: '詳情', plain: true, iconCls: 'icon-more' }); $("a[name='update']").linkbutton({ text: '編輯', plain: true, iconCls: 'icon-edit' }); $("a[name='delete']").linkbutton({ text: '刪除', plain: true, iconCls: 'icon-cancel' }); ////點擊詳情按鈕 //clickDetail(); }, toolbar: [{ id: 'btnAdd', text: '添加', iconCls: 'icon-add', handler: function () { addBtnClick(); //添加新聞 } }], }); }
要完成數(shù)據(jù)的顯示則還需要查詢數(shù)據(jù)庫。
根據(jù) url: '/NewInfo/ShowNewsList', 所以需要在NewInfo控制器下建立ShowNewsList方法
/// <summary> /// 分頁展示數(shù)據(jù) /// </summary> /// <returns></returns> public JsonResult ShowNewsList() { //要求返回的數(shù)據(jù)json對象 {total:200,rows:[{},{}]} int pageSize = int.Parse(Request["rows"]??"10"); int pageIndex = int.Parse(Request["page"]??"1"); List<NewInfo> newInfoList= NewInfoBll.GetPageEntityList(pageIndex, pageSize); //查詢所有數(shù)據(jù) var allNews = NewInfoBll.GetRecordCount(); //把totle和rows:[{},{}]一起返回 //先建立一個匿名類 var dataJson = new { total = allNews, rows = newInfoList }; var json = Json(dataJson, JsonRequestBehavior.AllowGet); return json; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- EasyUi中的Combogrid 實現(xiàn)分頁和動態(tài)搜索遠(yuǎn)程數(shù)據(jù)
- EasyUI學(xué)習(xí)之DataGird分頁顯示數(shù)據(jù)
- jQuery EasyUI API 中文文檔 - Pagination分頁
- jQuery EasyUI datagrid實現(xiàn)本地分頁的方法
- jQuery EasyUI Pagination實現(xiàn)分頁的常用方法
- EasyUi datagrid 實現(xiàn)表格分頁
- EasyUI Pagination 分頁的兩種做法小結(jié)
- SSh結(jié)合Easyui實現(xiàn)Datagrid的分頁顯示
- 淺談MVC+EF easyui dataGrid 動態(tài)加載分頁表格
- EasyUi+Spring Data 實現(xiàn)按條件分頁查詢的實例代碼
- easyUI使用分頁過濾器對數(shù)據(jù)進(jìn)行分頁操作實例分析
相關(guān)文章
關(guān)于DDD:管理"工作單元實例"的兩種模式的使用方法
本篇文章介紹了,關(guān)于DDD:管理"工作單元實例"的兩種模式的使用方法。需要的朋友參考下2013-04-04ASP.NET郵件發(fā)送system.Net.Mail案例
這篇文章主要為大家詳細(xì)介紹了ASP.NET郵件發(fā)送system.Net.Mail案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05.net 讀取非標(biāo)準(zhǔn)配置文件的小例子
這篇文章介紹了.net 讀取非標(biāo)準(zhǔn)配置文件的小例子,有需要的朋友可以參考一下2013-07-07一步步教你在Asp.net Mvc中使用UEditor編輯器
大家都知道ueditor是百度編輯器,目前使用也比較廣泛,下面這篇文章主要是通過一步步的步驟教大家在Asp.net Mvc中使用UEditor編輯器,需要的朋友可以參考借鑒,下面來一起看看吧。2016-12-12.net core日志系統(tǒng)相關(guān)總結(jié)
多年的經(jīng)驗,日志記錄是軟件開發(fā)的重要組成部分。沒有日志記錄機制的系統(tǒng)不是完善的系統(tǒng)。在開發(fā)階段可以通過debug附件進(jìn)程進(jìn)行交互調(diào)試,可以檢測到一些問題,但是在上線之后,日志的記錄起到至關(guān)重要的作用。本文講解下日志系統(tǒng)的相關(guān)使用2021-06-06Asp.net TreeView來構(gòu)建用戶選擇輸入的方法 推薦
選擇優(yōu)于輸入,這是一般人的共識,面對繁多的數(shù)據(jù),提供良好的選擇界面,一方面增強用戶的界面體驗,一方面也提高了數(shù)據(jù)的準(zhǔn)確性,更節(jié)省了用戶的寶貴時間。2009-12-12詳解Asp.Net Core 發(fā)布和部署( MacOS + Linux + Nginx )
這篇文章主要介紹了詳解Asp.Net Core 發(fā)布和部署( MacOS + Linux + Nginx ) ,具有一定的參考價值,有興趣的可以了解一下。2016-12-12基于.NET的FluentValidation數(shù)據(jù)驗證實現(xiàn)
這篇文章主要介紹了基于.NET的FluentValidation數(shù)據(jù)驗證實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11