Ajax+smarty技术实现无刷新分页

 更新时间:2016年03月11日 08:55:43   作者:IT小小海  
这篇文章主要介绍了Ajax+smarty技术实现无刷新分页的相关资料,需要的朋友可以参考下

这里运用Smarty模板,更简单

本文主要的技术:AJAX,PHP,Smarty,另外自己封装了一个很简单的类

类:

  (function(){
function $(id) {
return document.getElementById(id);
}
$.init=function() {
try{return new XMLHttpRequest();}catch(e){};
try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
alert('请更换浏览器');
}
$.get=function (url,data,callback,type) {
var xhr = this.init();
url += '?' +new Date().getTime();
if(data!=null){
url += '&'+data;
}
xhr.open('get',url);
xhr.onreadystatechange = function () {
if(xhr.readyState == 4 && xhr.status == 200){
if(type==null){
callback(xhr.responseText);
}
if(type == 'text'){
callback(xhr.responseText);
}
if(type == 'xml'){
callback(xhr.responseXML);
}
if(type == 'json'){
callback(eval("("+xhr.responseText+")"));
}
}
}
xhr.send(null);
}

$.post = function (url,data,callback,type) {
var xhr = this.init();
xhr.open('post',url);
xhr.setRequestHeader('Content-Type','Application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if(xhr.readyState == 4 && xhr.status == 200){
if(type==null){
callback(xhr.responseText);
}
if(type == 'text'){
callback(xhr.responseText);
}
if(type == 'xml'){
callback(xhr.responseXML);
}
if(type == 'json'){
callback(eval("("+xhr.responseText+")"));
}
}
}; 
xhr.send(data);
}

 类很简单,不介绍了

HTML:

  window.onload=function () {
init(2);
}
function del(id,p){
$.get('del.php','id='+id,function(msg){
if(msg==1){
init(p);
}
})
}
function init(p) {
$.get('page.php','page='+p,function(msg){
$('result').innerHTML=msg;
});
}

PHP:

<?php
header('content-type:text/html;charset=utf-8');
mysql_connect('localhost','root','root');
mysql_select_db('db2');
mysql_query('set names utf8');
$re=mysql_query('select count(*) as num from catgory');
$hang=mysql_fetch_assoc($re);
$row1=$hang['num'];
$page=isset($_GET['page'])? $_GET['page']:1;
$pagesize=2;
$total=ceil($row1/$pagesize);
$prev=$page-1;
$next=$page+1;
if($prev<1){$prev=1;}
if($next>$total){$next=$total;}
$offset = ($page-1)*$pagesize;
$result=mysql_query("select * from catgory limit ".$offset.','.$pagesize);
$ct=mysql_num_rows($result);
$data=array();
for($i=0;$i<$ct;$i++){
$r=mysql_fetch_assoc($result);
$data[]=$r;
}
include('libs/Smarty.class.php');
$Smarty=new Smarty();
$Smarty->assign('data',$data);
$Smarty->assign('page',$page);
$Smarty->assign('pagesize',$pagesize);
$Smarty->assign('total',$total);
$Smarty->assign('prev',$prev);
$Smarty->assign('next',$next);
$Smarty->assign('row1',$row1);
$str=$Smarty->fetch('page.html');
echo $str;

Smarty:

    <table border="1" rules="all" width="600">
<tr>
<td>编号</td>
<td>品牌</td>
<td>pid</td>
<td>删除</td>
</tr>
{foreach from=$data item='value'}
<tr>
<td>{$value['id']}</td>
<td>{$value['name']}</td>
<td>{$value['pid']}</td>
<td><a href="#", onclick="del({$value['id']},{$page})">删除</td>
</tr>
{/foreach}
<tr>
<td colspan="3">
共{$total}页
第{$page}页
<a href="#", onclick="init({$prev})">上一页</a>
<a href="#", onclick="init({$next})">下一页</a>
<a href="#", onclick="init(1)">第一页</a>
<a href="#", onclick="init({$total})">最末页</a>
共{$row1}条数据
每页{$pagesize}条数据
</td>
</tr>
</table>

要引入smary模板

以上所述是小编给大家介绍的Ajax+smarty技术实现无刷新分页,希望对大家有所帮助!

相关文章

  • $.ajax()常用方法详解(推荐)

    $.ajax()常用方法详解(推荐)

    AJAX 是一种与服务器交换数据的技术,可以在补充在整个页面的情况下更新网页的一部分。接下来通过本文给大家介绍ajax一些常用方法,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧
    2016-07-07
  • 使用ajax实现分页技术

    使用ajax实现分页技术

    这篇文章主要为大家详细介绍了如何使用ajax实现分页技术,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • 基于h5的history改善ajax列表请求体验

    基于h5的history改善ajax列表请求体验

    这篇文章主要介绍了基于h5的history改善ajax列表请求体验的相关资料,需要的朋友可以参考下
    2015-11-11
  • Ajax $.getJSON案例详解

    Ajax $.getJSON案例详解

    ajax中的$.getJSON方法想必大家非常熟悉吧,本文接下来将在为你巩固一下,感兴趣的你可以参考下,希望对你有所帮助
    2013-03-03
  • ASP.NET与Ajax的实现方式小总结

    ASP.NET与Ajax的实现方式小总结

    Ajax 应该不是一项技术,是一种思想而已,跟 ASP.NET 以及其它 Web 开发语言没有什么太大关系,这里只是谈谈 ASP.NET 中目前使用的 Ajax 技术以及其它一些实现 Ajax 的优秀框架。感兴趣的朋友跟着小编一起学习asp.net与ajax的实现方式
    2015-09-09
  • ajax来自动补全表单字段示例

    ajax来自动补全表单字段示例

    这篇文章主要介绍了如何使用ajax来自动补全表单字段,需要的朋友可以参考下
    2014-08-08
  • ajax使用formdata上传文件流

    ajax使用formdata上传文件流

    这篇文章主要为大家详细介绍了ajax使用formdata上传文件流,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Ajax方式删除表格一行数据示例代码

    Ajax方式删除表格一行数据示例代码

    Ajax方式删除信息在某些情况下还是蛮实用的,下面为大家具体介绍下Ajax方式如何删除表格一行数据,感兴趣的朋友可以了解下
    2013-08-08
  • JSP+Ajax 添加、删除多选框

    JSP+Ajax 添加、删除多选框

    这篇文章主要介绍了JSP+Ajax 添加、删除多选框
    2006-10-10
  • AJAX检测用户名是否存在的方法

    AJAX检测用户名是否存在的方法

    这篇文章主要为大家详细介绍了AJAX检测用户名是否存在,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12

最新评论