標籤:分頁取資料
本文通過擷取首頁內容來講解Web頁面拖拽擷取當前頁資料,功能類似在google查看圖片,這裡面涉及如下五個步驟:
(1)頁面首次載入時讀取第一頁資料
(2)觸發捲軸拖拽動作
(3)Ajax非同步讀取下一頁資料
(4)服務端把資料封裝為Json返回
(5)把頁面資料通過DOM元素繪製在頁面上
接下來就按這五個步驟依次進行650) this.width=650;" src="http://img.baidu.com/hi/bobo/B_0059.gif" alt="B_0059.gif" />
1、頁面首次載入時會載入main.js檔案。在此檔案中增加CURRENT_ITEM_INDEX變數,用於標識當前話題的索引值;增加方法getPageContent(),用於頁面載入時調用,擷取第一頁資料。
/** * 按時間降序排列後標識頁面最後一條記錄的索引值 */var CURRENT_ITEM_INDEX = 0;$(document).ready(function(){ // 擷取首頁內容 getPageContent(CURRENT_ITEM_INDEX);});function getPageContent(currentIndex){ var data = {"currentIndex": currentIndex}; asyncRequest("mainContent.data", data, function(result) { // 由第3步實現 });}
2、觸發捲軸拖拽動作。在main.js載入時對window綁定scroll事件
$(document).ready(function(){ // 略 getBreifUserInfo(); // 監聽捲軸拖拽事件 bindScrollEvent();});
【備忘】:由於涉及到不斷地下拉捲軸,所以需要增加一個標識isQueryFlag,若正在尋找資料時則不再響應下拉事件
/**
* 是否正在查詢資料標識
*/
var isQueryFlag = false;
3、Ajax非同步讀取下一頁資料。實現bindScrollEvent()方法:
/** * 綁定捲軸拖拽事件 */function bindScrollEvent(){ var scrollBarDistance = 0; // 捲軸的當前位置 var documentHeight = 0; // 文檔高度 var windowHeight = $(window).height(); // 當前視窗高度 $(window).scroll(function(){ scrollBarDistance = $(document).scrollTop(); documentHeight = $(document).height(); if(scrollBarDistance + windowHeight >= documentHeight) { if(isQueryFlag === true) { return; } // 擷取頁面內容 getPageContent(CURRENT_ITEM_INDEX); } });}
4、服務端把資料封裝為Json返回
(1)修改設定檔system-data.xml,把拖拽動作方法與服務端讀取資料的業務處理邏輯關聯起來
<!--擷取系統首頁內容資訊--><business name="mainContent" business-class="com.medical.server.data.MainDataAction" />
(2)定義服務端讀取資料的業務處理類com.medical.server.data.MainDataAction
@Overridepublic String execute() throws FrameException{ // 擷取目前記錄索引值 String currentIndex = getParameter("currentIndex"); if (FrameUtil.isEmpty(currentIndex)) { TopicResultBean result = new TopicResultBean(); result.setErrorCode(FrameErrorCode.REQUEST_PARAM_ERROR); return gson.toJson(result); } // 從資料庫中讀取資料 int index = Integer.valueOf(currentIndex); List<TopicDAO> topicList = TopicUtil.getTopicList(index); if (FrameUtil.isEmpty(topicList)) { TopicResultBean result = new TopicResultBean(); result.setErrorCode(FrameErrorCode.REQUEST_CONTENT_EMPTY); return gson.toJson(result); } // 組裝介面所需要內容 List<TopicBean> topicBeanList = new ArrayList<TopicBean>(); for (TopicDAO element : topicList) { UserDAO author = UserUtil.getUserByName(element.getUserId()); TopicBean bean = new TopicBean(); bean.setUserName(author.getUserId()); bean.setUserIcon(author.getIconPath()); bean.setNickName(author.getUserSign()); bean.setTopicId(element.getTopicId()); bean.setTopicTitle(element.getTopicTitle()); bean.setCommentNum(CommentUtil.getCommentNum(element.getTopicId())); bean.setTopicSummary(getTopicSummary(element.getPrescript())); bean.setTopicTime(element.getTopicTime()); topicBeanList.add(bean); } // 返回JSON結果 TopicResultBean result = new TopicResultBean(); result.setErrorCode(0); result.setTopicList(topicBeanList); return gson.toJson(result);}
【備忘】:為了使內容簡潔,中間省略了一些處理函數
5、把頁面資料通過DOM元素繪製在頁面上
(1)非同步讀取資料,並對資料合法性進行判斷
// 設定查詢標識isQueryFlag = true;asyncRequest("mainContent.data", data, function(result){ // 若讀取資料未成功直接返回 var resultJson = eval(result); if(resultJson.errorCode != 0) { return; } var topicList = resultJson.topicList; if(!topicList){ return; } // 重設資料當前索引值 CURRENT_ITEM_INDEX = CURRENT_ITEM_INDEX + topicList.length; // 操縱DOM把資料繪製到頁面上 $.each(topicList, function(i, item){ appendData(i, item); }); // 重設查詢標識 isQueryFlag = false;});
(2)DOM元素繪製的實現
/** * 操縱DOM把資料繪製到頁面上 */function appendData(i, item){ var topicItem = $("<div />").attr("class", "main_item"); topicItem.appendTo($("#main_content_dynamic_id")); // 添加帳戶圖片 var userIcon = $("<i />").attr("class", "main_item_icon"); userIcon.appendTo(topicItem); // 添加挑戰內容 var content = $("<div />").attr("class", "main_item_content"); content.appendTo(topicItem); // >>>>>>設定挑戰話題標題 var topWrapper = $("<div />").attr("class", "main_item_wrapper"); var topicTitle = $("<div />").attr("class", "main_item_title"); var titleLink = $("<a />").attr("href", "#").text(item.topicTitle); titleLink.appendTo(topicTitle); topicTitle.appendTo(topWrapper); topWrapper.appendTo(content); // >>>>>>設定挑戰話題標題 var topicTime = $("<div />").attr("class", "main_item_time").text(item.topicTime); topicTime.appendTo(topWrapper); // >>>>>>設定使用者名稱稱和暱稱 var centerWrapper = $("<div />").attr("class", "main_item_wrapper"); var userName = $("<label />").attr("class", "main_item_author").text(item.userName + ","); var userNick = $("<label />").attr("class", "main_item_nickname").text(item.nickName); userName.appendTo(centerWrapper); userNick.appendTo(centerWrapper); centerWrapper.appendTo(content); // >>>>>>設定話題摘要資訊 var middleWrapper = $("<div />").attr("class", "main_item_wrapper"); var topicSummary = $("<div />").attr("class", "main_item_substance").html(item.topicSummary); topicSummary.appendTo(middleWrapper); middleWrapper.appendTo(content); // >>>>>>設定話題附屬資訊 var bottomWrapper = $("<div />").attr("class", "main_item_wrapper"); var topicAttach = $("<div />").attr("class", "main_item_attach"); topicAttach.appendTo(bottomWrapper); bottomWrapper.appendTo(content); // >>>>>>>>>>>>設定話題關注資訊 var followLink = $("<a />").attr("href", "#"); var followIcon = $("<i />").attr("class", "main_item_attention"); followIcon.appendTo(followLink); followLink.appendTo(topicAttach); followLink.text("關注"); // >>>>>>>>>>>>設定話題關注資訊 var commentLink = $("<a />").attr("href", "#"); var commentIcon = $("<i />").attr("class", "main_item_discuss"); commentIcon.appendTo(commentLink); commentLink.appendTo(topicAttach); commentLink.text(item.commentNum + "人評論"); // >>>>>>>>>>>>設定話題分享資訊 var shareLink = $("<a />").attr("href", "#"); var shareIcon = $("<i />").attr("class", "main_item_share"); shareIcon.appendTo(shareLink); shareLink.appendTo(topicAttach); shareLink.text("分享"); // >>>>>>>>>>>>設定話題收藏資訊 var favoriteLink = $("<a />").attr("href", "#"); var favoriteIcon = $("<i />").attr("class", "main_item_collection"); favoriteIcon.appendTo(favoriteLink); favoriteLink.appendTo(topicAttach); favoriteLink.text("收藏");}
6、效果示範
(1)向資料表topictable中增加兩條記錄
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/47/5F/wKiom1P56pHDhGuzAAD-vLZtWng669.jpg" title="資料記錄.png" alt="wKiom1P56pHDhGuzAAD-vLZtWng669.jpg" />
(2)在瀏覽器輸入http://localhost:8080/medical,效果如下:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/47/60/wKioL1P57AyREnk5AAMyG2VYpuI000.jpg" title=".png" alt="wKioL1P57AyREnk5AAMyG2VYpuI000.jpg" />
【備忘】:
1、可以多插入些記錄,就可以觀察拖拽效果了 650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0057.gif" alt="j_0057.gif" />
2、這裡的拖拽事件沒有考慮視窗的縮小與放大情況
3、為了簡潔起見,沒有增加返回頂端功能
本文出自 “青客” 部落格,請務必保留此出處http://qingkechina.blog.51cto.com/5552198/1544928
【鬥醫】【18】Web應用開發20天