通過(guò)Ajax請(qǐng)求動(dòng)態(tài)填充頁(yè)面數(shù)據(jù)的實(shí)例
你可能得預(yù)先了解
實(shí)現(xiàn)功能:點(diǎn)擊頁(yè)面上的按鈕實(shí)現(xiàn)動(dòng)態(tài)追加數(shù)據(jù)
實(shí)現(xiàn)原理:點(diǎn)擊頁(yè)面按鈕,通過(guò)Ajax提交請(qǐng)求到后臺(tái),后臺(tái)接收請(qǐng)求后進(jìn)行數(shù)據(jù)庫(kù)操作,然后返回?cái)?shù)據(jù)到前臺(tái)并進(jìn)行頁(yè)面渲染

動(dòng)態(tài)加載更多數(shù)據(jù)
代碼實(shí)現(xiàn)
//1.頁(yè)面布局
<div style="padding: 0 0 20px 0;">
<input type="hidden" class="tip" value="1">
<input style="background:#01affe;color: #FFF;cursor: pointer;
text-align:center;height:30px;vertical-align: middle;padding:0 5px;
type="button" name="more" id="more" value="加載更多" onclick="moreData();"/>
</div>
//2.js代碼
function moreData(){
var ptip = $('.tip').val();
var jstr = {pageNo:ptip};
$.ajax({
url: '${rc.getContextPath()}/publicity/more.do',//url以具體為實(shí)現(xiàn)
type: 'POST',
dataType: 'html',
data:jstr,
timeout: 5000,
cache: false,
beforeSend: LoadFunction, //加載執(zhí)行方法
error: erryFunction, //錯(cuò)誤執(zhí)行方法
success: succFunction //成功執(zhí)行方法
})
function LoadFunction() {
$("#more").val('加載中...');
}
function erryFunction() {
alert("獲取數(shù)據(jù)錯(cuò)誤,請(qǐng)重試!");
$("#more").val('加載更多');
}
function succFunction(data) {
if(data!=null && data!=""){
$('.tip').val(++ptip);
$("#more").val('加載更多');
$('.mainContent').append(data);
}else{
$("#more").val('無(wú)更多數(shù)據(jù)');
$("#more").attr('disabled',true);
}
}
//3.后臺(tái)代碼
//3.1 java代碼實(shí)現(xiàn)
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.appmoudle.base.Consts;
import com.appmoudle.model.ssdj.Publicity;
import com.appmoudle.service.PublicityService;
@Controller
@RequestMapping("/publicity")
public class MoreData {
private String ftlURL = ".../publicity/MoreData.ftl";
@Autowired
private PublicityService publicityService;
@RequestMapping(value="more",method=RequestMethod.POST)
public String getMoreData(HttpServletRequest request,ModelMap map){
Integer start = 0;
String pageNo = request.getParameter("pageNo");
if(pageNo!=null){
start = Integer.parseInt(pageNo) * 20;
}
List<Publicity> dataList = publicityService.findList(start, Consts.PAGE_SIZE, null, "1", null, null);
map.put("index_number", start);
map.put("dataList", dataList);
return ftlURL;
}
}
//3.2 模板頁(yè)面
//(MoreData.ftl)
<#if dataList??>
<#list dataList as dataItem>
<tr>
<td class='f-blue'>${dataItem_index+1+index_number}</td>
<td>
<#if dataItem.comp_name?length > 12>
${dataItem.comp_name?substring(0,12)}..
<#else>
${dataItem.comp_name}
</#if>
</td>
<td>${dataItem.license_number}</td>
<td>
<#if dataItem.license_name?length > 10>
${dataItem.license_name?substring(0,10)}..
<#else>
${dataItem.license_name}
</#if>
</td>
<td>
<#if dataItem.validaty_start?has_content>
${dataItem.validaty_start?date}
</#if>
</td>
<td>
<#if dataItem.validaty_end?has_content>
${dataItem.validaty_end?date}
</#if>
</td>
<td>
<#if dataItem.license_content?length > 20>
${dataItem.license_content?substring(0,20)}..
<#else>
${dataItem.license_content}
</#if>
</td>
</tr>
</#list>
</#if>
效果截圖

后臺(tái)返回?cái)?shù)據(jù)(帶格式)
片尾留注
1、前臺(tái)頁(yè)面點(diǎn)擊增加更多后,向后臺(tái)發(fā)起請(qǐng)求,后臺(tái)進(jìn)行數(shù)據(jù)庫(kù)操作,返回?cái)?shù)據(jù)后填充到數(shù)據(jù)模板,帶格式的數(shù)據(jù)返回到前臺(tái)填充頁(yè)面
2、代碼中的變量 ptip 指代當(dāng)前獲取次數(shù),也可理解為獲取頁(yè)數(shù),后臺(tái)設(shè)定每次獲取N條數(shù)據(jù),初次獲取是以頁(yè)面已有數(shù)據(jù)數(shù)開(kāi)始,追加N條數(shù)據(jù),以此循環(huán)
3、本代碼段為項(xiàng)目開(kāi)發(fā)中使用,因項(xiàng)目使用框架,后臺(tái)代碼書寫格式僅作為參考使用
以上這篇通過(guò)Ajax請(qǐng)求動(dòng)態(tài)填充頁(yè)面數(shù)據(jù)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Ajax引擎 ajax請(qǐng)求步驟詳細(xì)代碼
- vue項(xiàng)目使用axios發(fā)送請(qǐng)求讓ajax請(qǐng)求頭部攜帶cookie的方法
- 為jquery的ajax請(qǐng)求添加超時(shí)timeout時(shí)間的操作方法
- ajax請(qǐng)求后臺(tái)得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹(shù)形下拉框的方法
- 通過(guò)jquery的ajax請(qǐng)求本地的json文件方法
- jQuery中ajax請(qǐng)求后臺(tái)返回json數(shù)據(jù)并渲染HTML的方法
- ajax請(qǐng)求后臺(tái)接口數(shù)據(jù)與返回值處理js的實(shí)例講解
- jquery 通過(guò)ajax請(qǐng)求獲取后臺(tái)數(shù)據(jù)顯示在表格上的方法
- Python基于分析Ajax請(qǐng)求實(shí)現(xiàn)抓取今日頭條街拍圖集功能示例
- 關(guān)于Ajax異步請(qǐng)求后臺(tái)數(shù)據(jù)進(jìn)行動(dòng)態(tài)分頁(yè)功能
- 爬取今日頭條Ajax請(qǐng)求
相關(guān)文章
使用HTML5中postMessage知識(shí)點(diǎn)解決Ajax中POST跨域問(wèn)題
這篇文章主要介紹了使用HTML5中postMessage知識(shí)點(diǎn)解決Ajax中POST跨域問(wèn)題的相關(guān)資料,需要的朋友可以參考下2015-10-10
使用加載圖片解決在Ajax數(shù)據(jù)加載中頁(yè)面出現(xiàn)短暫空白的問(wèn)題(推薦)
在項(xiàng)目中用ajax異步獲取數(shù)據(jù)后有時(shí)會(huì)因?yàn)閿?shù)據(jù)問(wèn)題或者網(wǎng)絡(luò)問(wèn)題,頁(yè)面一直顯示空白,現(xiàn)在用加載圖片來(lái)過(guò)渡這種狀態(tài),具體實(shí)例代碼通過(guò)本文一起學(xué)習(xí)吧2016-12-12
Ajax 無(wú)刷新在注冊(cè)用戶名時(shí)判斷是否為空是否被使用
這篇文章主要介紹了Ajax 無(wú)刷新在注冊(cè)用戶名時(shí)判斷是否為空是否被使用,需要的朋友可以參考下2014-05-05
jQuery ajax json 數(shù)據(jù)的遍歷代碼
最近做了一個(gè)項(xiàng)目,其中有需求要進(jìn)行ajax請(qǐng)求后,后臺(tái)傳遞回來(lái)以下json數(shù)據(jù)。下面小編給大家分享我的實(shí)現(xiàn)思路,需要的朋友參考下2016-06-06
Ajax+Asp源代碼]讀取數(shù)據(jù)庫(kù)內(nèi)容的表格(沒(méi)有用框架)
Ajax+Asp源代碼]讀取數(shù)據(jù)庫(kù)內(nèi)容的表格(沒(méi)有用框架)...2006-11-11
xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)
xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)...2007-09-09
給Ajax返回的HTML標(biāo)簽動(dòng)態(tài)添加樣式的方法
這篇文章主要介紹了給Ajax返回的HTML標(biāo)簽動(dòng)態(tài)添加樣式的方法,需要的朋友可以參考下2017-04-04

