欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

AJAX相關(guān)

當(dāng)前位置:主頁 > 網(wǎng)絡(luò)編程 > AJAX相關(guān) >

通過Ajax請求動態(tài)填充頁面數(shù)據(jù)的實例

來源:本站原創(chuàng)|時間:2020-01-11|欄目:AJAX相關(guān)|點擊: 次

你可能得預(yù)先了解

實現(xiàn)功能:點擊頁面上的按鈕實現(xiàn)動態(tài)追加數(shù)據(jù)

實現(xiàn)原理:點擊頁面按鈕,通過Ajax提交請求到后臺,后臺接收請求后進行數(shù)據(jù)庫操作,然后返回數(shù)據(jù)到前臺并進行頁面渲染

動態(tài)加載更多數(shù)據(jù)

代碼實現(xiàn)

//1.頁面布局
<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以具體為實現(xiàn)
   type: 'POST',
   dataType: 'html',
   data:jstr,
   timeout: 5000,
   cache: false,
   beforeSend: LoadFunction, //加載執(zhí)行方法
   error: erryFunction, //錯誤執(zhí)行方法
   success: succFunction //成功執(zhí)行方法
  })

  function LoadFunction() {
   $("#more").val('加載中...');
  }
  function erryFunction() {
  alert("獲取數(shù)據(jù)錯誤,請重試!");
  $("#more").val('加載更多');
  }
  function succFunction(data) {
  if(data!=null && data!=""){
   $('.tip').val(++ptip);
   $("#more").val('加載更多');
   $('.mainContent').append(data);
  }else{
   $("#more").val('無更多數(shù)據(jù)');
   $("#more").attr('disabled',true);
  }
 }

//3.后臺代碼
//3.1 java代碼實現(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 模板頁面
//(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>

效果截圖

后臺返回數(shù)據(jù)(帶格式)

片尾留注

1、前臺頁面點擊增加更多后,向后臺發(fā)起請求,后臺進行數(shù)據(jù)庫操作,返回數(shù)據(jù)后填充到數(shù)據(jù)模板,帶格式的數(shù)據(jù)返回到前臺填充頁面

2、代碼中的變量 ptip 指代當(dāng)前獲取次數(shù),也可理解為獲取頁數(shù),后臺設(shè)定每次獲取N條數(shù)據(jù),初次獲取是以頁面已有數(shù)據(jù)數(shù)開始,追加N條數(shù)據(jù),以此循環(huán)

3、本代碼段為項目開發(fā)中使用,因項目使用框架,后臺代碼書寫格式僅作為參考使用

以上這篇通過Ajax請求動態(tài)填充頁面數(shù)據(jù)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。

上一篇:Fly攔截全局Ajax請求的方法

欄    目:AJAX相關(guān)

下一篇:淺析IE瀏覽器關(guān)于ajax的緩存機制

本文標(biāo)題:通過Ajax請求動態(tài)填充頁面數(shù)據(jù)的實例

本文地址:http://mengdiqiu.com.cn/a1/AJAXxiangguan/11296.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有