侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Ajax获取数据然后显示在页面的实现方法

前端  /  管理员 发布于 5年前   565

主要功能流程介绍

循环获取列表数据

点击列表数据进入详情页

点击报名参加弹出报名成功提示框

点击提示框中的确定按钮,跳回列表页

代码实现流程和解说

一、列表页

1、访问链接list.php时判断是pc端还是客户端

$user_agent_arr = mall_get_user_agent_arr();if(MALL_UA_IS_PC == 1){  //****************** pc版 ******************  include_once './list-pc.php';}else{  //****************** wap版 ******************  include_once './list-wap.php';} 

2、如果是wap版就跳转到 list-wap.php 页面,载入 list.tpl.htm页面

$pc_wap = 'wap/';$tpl = $my_app_pai->getView(TASK_TEMPLATES_ROOT.$pc_wap.'trade/list.tpl.htm');

3、list.tpl.htm 页面进行渲染模板

HTML

<div class="page-view " data-role="page-container">    <div class="sales-list-page">      <div id="render-ele"></div>    </div>  </div>

JS

$(function()  // 渲染模块  {    //请求php的url    var TRADE_AJAX_URL = window.$__ajax_domain + 'get_trade_list.php';    //获取已经封装在list.js里面的一个对象list_item_class    var list_item_class = require('../../../../modules/list/list.js');    //获取模板块    var template = __inline('./list-item.tmpl');    var list_obj = new list_item_class({      ele : $("#render-ele"),//渲染数据到id为render-ele中      url : TRADE_AJAX_URL,//请求数据连接      template : template //渲染的模板    });  });

list-item.tmpl模板内容(循环的列表内容)

<div class="item-wrap">  {{#each list}}  {{#if is_enroll}}  <a href="//article/./detail.php?topic_id={{id}}&state=is_enter">  {{else}}  <a href="//article/./detail.php?topic_id={{id}}&state=no_enter">  {{/if}}    <div class="item ui-border-b" >      <div class="img-item">        <i class="img" style="background-image: url({{img}});">        </i>      </div>      <div class="text-item">        <div class="txt-con-1">          <h3 class="title f14">{{title}}</h3>          <p class="txt f10 color-999">所属品类:{{type}}</p>        </div>        <div class="txt-con-2">          <span class="color-333 join-in ">            {{ enroll_text }}           </span>        </div>      </div>    </div>  </a>  {{/each}}</div>

4、list.js进行数据处理,仅是对象的部分方法,具体的方法请自行写。

 _self.ajax_obj = utility.ajax_request ({  url : self.send_url,  data : self.ajax_params,  beforeSend : function()  {self._sending = true;_self.$loading = $.loading({  content:'加载中...'});  },  success : function(data)  {self._sending = false;//获取数据var list_data = data.result_data.list;console.log(data);//渲染前处理事件self.$el.trigger('list_render:before',[self.$list_container,data]);_self.$loading.loading("hide");//是否有分页self.has_next_page = data.result_data.has_next_page;// 无数据处理 if(!list_data.length && page == 1){  abnormal.render(self.$render_ele[0],{});  self.$load_more.addClass('fn-hide');  return;}else{  self.$load_more.removeClass('fn-hide');}//把数据放入模板var html_str = self.template({  list : list_data});//插入渲染列表self.$list_container.append(html_str);//渲染后处理事件self.$el.trigger('list_render:after',[self.$list_container,data,$(html_str)]);self.setup_event();  },  error : function()  {self._sending = false;_self.$loading.loading("hide");$.tips  ({content:'网络异常',stayTime:3000,type:'warn'  });  }})

5、get_trade_list.php接收到前端页面发过来的请求,然后进行数据收集处理最终返回数据给前台页面

// 接收参数$page = intval($_INPUT['page']);if(empty($page)){  $page = 1;}// 分页使用的page_count$page_count = 5;if($page > 1){  $limit_start = ($page - 1)*($page_count - 1);}else{  $limit_start = ($page - 1)*$page_count;}$limit = "{$limit_start},{$page_count}";//请求数据库的借口$sales_list_obj = POCO::singleton ( 'pai_topic_class' );$ret = $sales_list_obj-> get_task_list(false, '', 'id DESC', $limit);// 输出前进行过滤最后一个数据,用于真实输出$rel_page_count = 4;$has_next_page = (count($ret)>$rel_page_count);if($has_next_page){  array_pop($ret);}$output_arr['page'] = $page;$output_arr['has_next_page'] = $has_next_page;$output_arr['list'] = $ret;// 输出数据mall_mobile_output($output_arr,false);

6、前端页面接收到get_trade_list.php返回的数据,从而进行判断将数据库的内容显示在前台页面中。模板输出

$tpl->output();

详情页

1、点击列表页进入详情页(detail.php)

detail.php页面 接收 列表传过来的数据

//接收list传过来的参数$topic_id = intval($_INPUT['topic_id']);$state = $_INPUT['state'];if (empty($topic_id)) {  header("location: ".'./list.php');}//数据库借口$trade_detail_obj = POCO::singleton ( 'pai_topic_class' );$ret = $trade_detail_obj->get_task_detail($topic_id,$yue_login_id);

2、判断是pc端还是客户端(类似列表页)

3、跳转到detail-wap.php加载模板detail.tpl.htm同时也带参数过去

$pc_wap = 'wap/';$tpl = $my_app_pai->getView(TASK_TEMPLATES_ROOT.$pc_wap.'trade/detail.tpl.htm');//模板附带以下三个参数到detail.tpl.htm中$tpl->assign('ret', $ret);$tpl->assign('topic_id', $topic_id);$tpl->assign('state', $state);

4、页面引用对象ret中的字段

<div class="sales-detail-page">  <div class="item-wrap"><div class="item-1 item">  <div class="img-item"><i class="img" ><img src="//article/{ret.img}"/></i>  </div>   <div class="txt-item"><h3 class="title f16 color-333 fb">{ret.title}</h3><p class="sign-in-txt color-666">  {ret.enroll_text}</p>  </div></div><div class="item-3 item">  <div class="txt-item"><h3 class="title f14 color-333 fb">生意机会详情</h3><div class="txt-con f14 color-666">  <p class="txt">{ret.content}</p></div>  </div></div>  </div>  <div class="sign-name-item">  <!-- IF state = "is_enter" --><button class="ui-button-submit had-joined">  <span class="ui-button-content">已参加</span></button>  <!-- ELSE -->  <button class="ui-button-submit" id="submit"><span class="ui-button-content">报名参加</span>  </button>  <!-- ENDIF -->  </div></div>

5、点击报名参加按钮进行数据处理

var _self = {};$btn.on('click', function() {  var data =   {topic_id : {ret.id}  }  utility.ajax_request({url : window.$__ajax_domain+'add_task_enroll_trade.php',data : data,type : 'POST',cache : false,beforeSend : function() {  _self.$loading = $.loading({content : '发送中.....'  });},success : function(data) {  _self.$loading.loading("hide");  //请求成功后显示成功报名提示框,点击报名提示框确定按钮跳回列表页面if (data.result_data.result==1) {var dialog = utility.dialog({  "title" : '' ,  "content" : '提交成功,点击确定返回',  "buttons" : ["确定"]}); dialog.on('confirm',function(event,args)   {   window.location.href = document.referrer;   });  return;   }},  error : function() {  _self.$loading.loading("hide");  $.tips({content : '网络异常',stayTime : 3000,type : 'warn'  });}  });});

以上这篇Ajax获取数据然后显示在页面的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    IE下Ajax提交乱码的快速解决方法
    下一条:
    ajax同步验证单号是否存在的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客