GVKun编程网logo

ecshop前台使用ajax分页分析(前端ajax分页)

13

想了解ecshop前台使用ajax分页分析的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于前端ajax分页的相关问题,此外,我们还将为您介绍关于ajax与json获取数据并在前台使用简单实例

想了解ecshop前台使用ajax分页分析的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于前端ajax分页的相关问题,此外,我们还将为您介绍关于ajax与json 获取数据并在前台使用简单实例、ajax分页、Ajax分页插件Pagination从前台jQuery到后端java总结、ajax验证表单、分页、Yii ajax分页的新知识。

本文目录一览:

ecshop前台使用ajax分页分析(前端ajax分页)

ecshop前台使用ajax分页分析(前端ajax分页)

看到网上两种对AJAX的分析,有两种,摘抄下来:

1.使用和开发ecshop很长时间了。作为研究ecshop的一员,应该知道ecshop前台数据调用。到了局部的时候,有很多数据,但是数据量大的时候,我们必须分页。当然局部的分页,采用跳转方式肯定不行。于是ecshop前台数据的分页方式一般都是采用ecshop的ajax分页

我们来说个例子,比如ecshop的购买记录分页,那么就是采用的ecshop的ajax来分页的。首先我们看到bought_note_guide.lbi,这个就是来显示ecshop默认的购买记录的。当我们进入ecshop商品详细页面的时候,我们看到ecshop的程序会自动的加载bought_note_guide.lbi.,我们找到该代码。他是通过以下代码来实现的。{insert name='bought_notes' id=$id}。我们看到他调用了ecshop的insert方法。该方法位置与includes/lib_insert.PHP文件。既然是ecshop通过ajax分页。那么在数据显示的内容区域。必须指定一个id来接受ajax数据了。在该文件里面。就使用了ECS_BOUGHT方法。ajax的分页函数function insert_bought_notes($arr)该函数就是用来调用某商品购买记录的。

看到分页函数中,存在翻页函数,采用的是js方式.

$pager['page_first'] = "javascript:gotoBuyPage(1,$arr[id])";
$pager['page_prev'] = $page > 1 ? "javascript:gotoBuyPage(" .($page-1). ",$arr[id])" : 'javascript:;';
$pager['page_next'] = $page < $page_count ? 'javascript:gotoBuyPage(' .($page + 1) . ",$arr[id])" : 'javascript:;';
$pager['page_last'] = $page < $page_count ? 'javascript:gotoBuyPage(' .$page_count. ",$arr[id])" : 'javascript:;';

我们来分析ajax的js,他位置于js/common.js

function gotoBuyPage(page,id)
{
Ajax.call('goods.PHP?act=gotopage','page=' + page + '&id=' + id,gotoBuyPageResponse,'GET','JSON');
}

function gotoBuyPageResponse(result)
{
document.getElementById("ECS_BOUGHT").innerHTML = result.result;
}

我们可以看到,这个js就是通过请求,发送到商品详细页面,然后通过商品详细页面的PHP,将数据处理结果用ecshop ajax方式返回到模板中,将结果显示在商品详细页面模板的ECS_BOUGHT的id中。从而达到了动态局部刷新数据功能的作用。ecshop也正式通过这个方式来做到前台的ajax分页。


2.
以购买记录为例,在模板文件里
<!-- #BeginLibraryItem "/library/bought_note_guide.lbi" -->
{insert_scripts files='transport.js,utils.js'}
<div id="ECS_BOUGHT">{* ECSHOP 提醒您:动态载入bought_notes.lbi,显示当前商品的购买记录 *}{insert name='bought_notes' id=$id}</div>
<!-- #EndLibraryItem -->
bought_note_guide.lbi 这个文件用来显示ecshop默认的购买记录,在商品详情页会自动加载这个文件,这个文件再通过 {insert name=’bought_notes’ id=$id} 来实现。

这句话是说调用了ecshop的insert 方法,这个方法在include/lib_insert.PHP 文件里,文件中找到函数
function insert_bought_notes($arr) 这个就是调用商品购买记录的。

其中参数 $arr 是个数组,有两个值,如
array(2) { ["name"]=> string(12) “bought_notes” ["id"]=> int(8) } id就是商品的id。
函数中可以看到,商品购买记录的分页样式,翻页采用了js方式
$pager['page_first'] = “javascript:gotoBuyPage(1,$arr[id])”;
$pager['page_prev'] = $page > 1 ? “javascript:gotoBuyPage(” .($page-1). “,$arr[id])” : ‘javascript:;’;
这个js位于js/common.js
function gotoBuyPage(page,id)
{
Ajax.call('goods.PHP?act=gotopage','JSON');
}
function gotoBuyPageResponse(result)
{
document.getElementById("ECS_BOUGHT").innerHTML = result.result;
}
就是通过这个js请求,发送到商品详情页面,将结果显示在模板的ECS_BOUGHT的id中, 默认会显示在insert_bought_notes函数中限制的数据条数,当用ajax翻页时,再通过goods.PHP页面中
‘商品购买记录ajax处理’
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == ‘gotopage’){}
这块内容再次调用gotoBuyPage 。实现动态局部刷新数据的功能。

ajax与json 获取数据并在前台使用简单实例

ajax与json 获取数据并在前台使用简单实例

用ajax获取后台数据,返回json数据,怎么在前台使用呢?

后台

if (dataType == "SearchCustomer")
        {
          int ID;
          if (Int32.TryParse(CustomerID, out ID))
          {
            string s = GridComputer.GridCustomer.getCustomer(1, 1, ID);
            if (s == null)
            {
              context.Response.ContentType = "text/plain";
              context.Response.Write("[{\"name\":无用户,\"id\":\"0\",\"company\":\"无用户\"}]");
            }
            else { context.Response.Write(s); }
          }
 
        } 

前台

 $(document).ready(function () {
      $("#Button3").click(
    function (SucCallback) {
      $.ajax(
      {
        type: "get",
        url: ''GridDatas.ashx'', //后台处理程序  
        dataType: ''json'',   //接受数据格式  
        data: ''DataType=SearchCustomer&CustomerID='' + document.getElementById("Text3").value,     //要传递的数据  
        success:SucCallback,
        error: function () { alert("error"); }
      });
    })
    })

参考代码

grid.getCustomer(1,2,function (data) {
    var list = ''<p>'' + tree_GridInfo._name + ''的用户有</p><br>'';
    list += ''<table id="customers"><tr><th>姓名</th><th>电话</th></tr> '';
    $.each(data, function (i, n) {
      list += ''<tr onclick="showUser('' + 1 + '')"><td>'';
      list += n.name + ''</td>'' + ''<td>'' + n.company;
      list += ''</td></tr>'';
    });
    $("#SearchResult").html(list)

看你的json数据是列表还是单个了,就一条就无需中括号了

context.Response.Write("{\"name\":无用户,\"id\":\"0\",\"company\":\"无用户\"}");

$(document).ready(function () {
      $("#Button3").click(
    function (SucCallback) {
      $.ajax(
      {
        type: "get",
        url: ''GridDatas.ashx'', //后台处理程序  
        dataType: ''json'',   //接受数据格式  
        data: ''DataType=SearchCustomer&CustomerID='' + document.getElementById("Text3").value,     //要传递的数据  
        function (dataJson) {
           alert(dataJson.Name);
           alert(dataJson.Id);
        },
        error: function () { alert("error"); }
      });
    })
    })

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
  • jQuery使用each方法与for语句遍历数组示例
  • jquery对Json的各种遍历方法总结(必看篇)
  • AJAX和jQuery动态加载数据的实现方法
  • 遍历json获得数据的几种方法小结
  • jquery ajax加载数据前台渲染方式 不用for遍历的方法

ajax分页

ajax分页

一个比较简单,但是需要多一个页面的ajax分页方法
首先请求 传输数据到后台进行处理

$(function(){
    $(".btn").click(function(){
        var phone = $("#phone").val();
        $.ajax({
            dataType:'json',data:{phone:phone},url:"{:U('Index/cltoushus')}",type:'post',success:function(data){
                $(".table").html(data);
                    click();
            }
        });
    });
})
与一般的ajax写法没有什么不同,只是在成功返回的时候对输出的html进行了覆盖,然后调用一个click方法,click方法在后面。

后台处理   注意fetch方法和 $this->ajaxReturn 其他跟一般页面输出一样

 public function cltoushus(){
    $phone = I('phone');
    // 查询该手机的超市
    $cid = M('sup_user')->where("phone=$phone")->getField('id');
    $count = M('complaint')->where("cid=$cid")->count();
    $Page  = new \Think\Page($count,2);
    $show  = $Page->show();// 分页显示输出
    $list  = M('complaint')->where("cid=$cid")->order("time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
    $this->assign('list',$list);
    $this->assign('page',$show);
    $html = $this->fetch('Index/ajaxtou');

    $this->ajaxReturn($html,'JSON');
}
新建html页面  将要输出的表格复制到新页面 做为分页的页面

le>
<tr>
    <td>商品名称</td>
    <td>投诉人电话</td>
    <td>商家店名</td>
    <td>投诉内容</td>
    <td>投诉时间</td>
</tr>
<foreach name="list" item="l">
    <tr>
        <td>{$l.goods_name}</td>
        <td>{$l.phone}</td>
        <td>{$l.sup_name}</td>
        <td>{$l.cpl_content}</td>
        <td>{$l.time}</td>
    </tr>
</foreach>

{$page}
最后 就是之前调用的click方法 这里也需要获取ajax的传值,其实也是一个ajax page1 a是下一页点击

function click(){
    $(".page1 a").click(function(){
        var phone = $("#phone").val();
        var obj = this;
        var url = obj.href;
        $.ajax({
            dataType:'json',url:url,success:function(data){
                $(".table").html(data);
                    click();
            }
        })
    return false;
    });
}

Ajax分页插件Pagination从前台jQuery到后端java总结

Ajax分页插件Pagination从前台jQuery到后端java总结

困惑了我一段时间的网页分页,今天特地整理了一下我完成不久的项目。下面我要分享下我这个项目的分页代码,前后端通吃。希望前辈多多指教。

一、效果图

下面我先上网页前台和管理端的部分分页效果图,他们用的是一套代码。

二、上代码前的一些知识点

此jQuery插件为Ajax分页插件,一次性加载,故分页切换时无刷新与延迟,如果数据量较大不建议用此方法,因为加载会比较慢。

三、前台代码部分

var pageSize =6; //每页显示多少条记录
var total; //总共多少记录
 $(function() {
 Init(0); //注意参数,初始页面默认传到后台的参数,第一页是0; 
 $("#Pagination").pagination(total, { //total不能少 
 callback: PageCallback, 
 prev_text: ''上一页'', 
 next_text: ''下一页'', 
 items_per_page: pageSize, 
 num_display_entries: 4, //连续分页主体部分显示的分页条目数
 num_edge_entries: 1, //两侧显示的首尾分页的条目数 
 }); 
 function PageCallback(index, jq) { //前一个表示您当前点击的那个分页的页数索引值,后一个参数表示装载容器。 
 Init(index); 
 }
 });
 
 function Init(pageIndex){ //这个参数就是点击的那个分页的页数索引值,第一页为0,上面提到了,下面这部分就是AJAX传值了。
 $.ajax({
 type: "post",
 url:"../getContentPaixuServ?Cat="+str+"&rows="+pageSize+"&page="+pageIndex,
 async: false,
 dataType: "json",
 success: function (data) {
 $(".neirong").empty();
/* total = data.total; */
 var array = data.rows;
 for(var i=0;i<array.length;i++){
 var info=array[i];
 
 if(info.refPic != null){
 $(".neirong").append(''<dl><h3><a href="''+info.CntURL+''?ContentId=''+info.contentId+''" title="''+info.caption+''" >''+info.caption+''</a></h3><dt><a href="sjjm.jsp?ContentId=''+info.contentId+''" title="''+info.caption+''" ><img src="<%=basePathPic%>''+info.refPic+''" alt="''+info.caption+'' width="150" height="95""></a></dt> <dd>''+info.text+''</dd><span>发布时间:''+info.createDate+''</span></dl>'') 
 }else{
 $(".neirong").append(''<dl ><h3><a href="''+info.CntURL+''?ContentId=''+info.contentId+''" title="''+info.caption+''" >''+info.caption+''</a></h3><dd>''+info.text+''</dd><span>发布时间:''+info.createDate+''</span></dl>'');
 };
 } 
 },
 error: function () {
 alert("请求超时,请重试!");
 }
 }); 
};

四、后台部分(java)
我用的是MVC 3层模型

servlet部分: (可以跳过)

public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 response.setContentType("text/html;charset=utf-8");
 PrintWriter out = response.getWriter();
 //获取分页参数
 String p=request.getParameter("page"); //当前第几页(点击获取)
 int page=Integer.parseInt(p);
 
 String row=request.getParameter("rows"); //每页显示多少条记录
 int rows=Integer.parseInt(row);
 
 String s=request.getParameter("Cat"); //栏目ID
 int indexId=Integer.parseInt(s);
 JSONObject object=(new ContentService()).getContentPaiXuById(indexId, page, rows); 
 out.print(object);
 out.flush();
 out.close();
 }

Service部分:(可以跳过)

public JSONObject getContentPaiXuById(int indexId, int page, int rows) {
 JSONArray array=new JSONArray();
 List<Content>contentlist1=(new ContentDao()).selectIndexById(indexId);
 List<Content>contentlist=paginationContent(contentlist1,page,rows);
 for(Content content:contentlist){
 JSONObject object=new JSONObject();
 object.put("contentId", content.getContentId());
 object.put("caption", content.getCaption());
 object.put("createDate", content.getCreateDate());
 object.put("times", String.valueOf(content.getTimes()));
 object.put("source", content.getSource());
 object.put("text", content.getText());
 object.put("pic", content.getPic());
 object.put("refPic", content.getRefPic());
 object.put("hot", content.getHot());
 object.put("userId", content.getAuthorId().getUserId());
 int id = content.getAuthorId().getUserId();
 String ShowName = (new UserService()).selectUserById(id).getString("ShowName");
 object.put("showName", ShowName);
 array.add(object);
 
 }
 JSONObject obj=new JSONObject();
 obj.put("total", contentlist1.size());
 obj.put("rows", array);
 return obj;
 }

获取出每页的的起止id(这部分是重点),同样写在Service中,比如说假设一页有6条内容,那么第一页的id是从1到6,第二页的id是从7到12,以此类推

//获取出每页的内容 从哪个ID开始到哪个ID结束。
 private List<Content> paginationContent(List<Content> list,int page,int rows){
 List<Content>small=new ArrayList<Content>();
 int beginIndex=rows*page; //rows是每页显示的内容数,page就是我前面强调多次的点击的分页的页数的索引值,第一页为0,这样子下面就好理解了!
 System.out.println(beginIndex);
 int endIndex;
 if(rows*(page+1)>list.size()){ 
 endIndex=list.size(); 
 }
 else{
 endIndex=rows*(page+1);
 }
 for(int i=beginIndex;i<endIndex;i++){ 
 small.add(list.get(i)); 
 } 
 return small;
 }

Dao层: (可以跳过)

public List selectIndexById(int indexId){
 List<Content>list=new ArrayList<Content>();
 try{
 conn = DBConn.getCon();
 String sql = "select * from T_Content,T_User where T_Content.AuthorId = T_User.UserId and CatlogId=? order by CreateDate desc";
 pstm = conn.prepareStatement(sql);
 pstm.setInt(1, indexId);
 rs = pstm.executeQuery();
 SimpleDateFormat ff=new SimpleDateFormat("yyyy年MM月dd日 hh时mm分");
 while(rs.next()){
 Content content = new Content();
 content.setContentId(rs.getInt("ContentId"));
  content.setCaption(rs.getString("Caption"));
  content.setCreateDate(f.format(rs.getTimestamp("CreateDate")));
  content.setTimes(rs.getInt("Times"));
  content.setSource(rs.getString("Source"));
  content.setText(rs.getString("Text"));
  content.setPic(rs.getString("Pic"));
  content.setRefPic(rs.getString("RefPic"));
  content.setHot(rs.getInt("Hot"));
  User user = new User();
  user.setUserId(rs.getInt("UserId"));
  content.setAuthorId(user);
  Catlog catlog = new Catlog(); //CntURL待开发
  catlog.setCatlogId(rs.getInt("CatlogId"));
  content.setCatlog(catlog);
  list.add(content);
 }
 }catch(Exception e){
 e.printStackTrace();
 }finally{
 DBConn.closeDB(conn, pstm, rs);
 }
 return list;
 }

精彩专题分享:jquery分页功能操作  JavaScript分页功能操作

以上就是网页所实现的分页代码,easy-ui部分的分页也可以参考以上代码。

您可能感兴趣的文章:
  • jQuery Pagination Ajax分页插件(分页切换时无刷新与延迟)中文翻译版
  • jquery分页插件jquery.pagination.js使用方法解析
  • Jquery 分页插件之Jquery Pagination
  • 最实用的jQuery分页插件
  • 分享一个自己动手写的jQuery分页插件
  • jQuery ajax分页插件实例代码
  • 基于bootstrap3和jquery的分页插件
  • jQuery插件分享之分页插件jqPagination
  • jquery ajax分页插件的简单实现
  • jquery+css3打造一款ajax分页插件(自写)
  • 使用JQuery实现的分页插件分享
  • jQuery实现的分页插件完整示例

ajax验证表单、分页、Yii ajax分页

ajax验证表单、分页、Yii ajax分页


一:ajax有专门的Widget,你可以在视图里直接调用,比如用ajax验证表单,写法如下:

[PHP] view plain copy
  1. <?PHP$form=$this->beginWidget('CActiveForm',array(
  2. 'id'=>'user-form',
  3. 'enableAjaxValidation'=>true,
  4. ));?>
  5. ...表单内容...
  6. <?PHP$this->endWidget();?>

分页:

copy

今天的关于ecshop前台使用ajax分页分析前端ajax分页的分享已经结束,谢谢您的关注,如果想了解更多关于ajax与json 获取数据并在前台使用简单实例、ajax分页、Ajax分页插件Pagination从前台jQuery到后端java总结、ajax验证表单、分页、Yii ajax分页的相关知识,请在本站进行查询。

本文标签:

上一篇Ajax请求session超时处理流程(DWZ)(ajax请求超时时间)

下一篇ecshop后台ajax无刷新修改商品数量原理分析(ecshop有哪些bug)