jquery.datatable.js与CI整合 异步加载(大数据量处理)
http://blog.csdn.net/kingsix7/article/details/38928685
1、CI 控制器添加方法
$this->show_fields_array=array(
"truename"=>"列1",
"item_goods"=>"列2",
"item_store"=>"列3",
"post_status"=>"列4",
"post_date"=>"列5",
"goods_from"=>"列6"
);
$this->select_fields=implode(",",array_keys($this->show_fields_array));
/**
* 加载列表页
*/
public function index()
{
$result=$this->menu;
$result["all_table"]=$this->create_table("all_table");
$result["internal_table"]=$this->create_table("internal_table");
$result["overseas_table"]=$this->create_table("overseas_table");
$result["show_fields"]=$this->select_fields;
$this->Header('public/header', $result);
$this->Left_Menu('public/left_menu', $result);
$this->Template('bl/data_statistics', $result);
$this->Footer('public/footer', $result);
}
/**
* 获取列表数据
*/
public function get_data_tables(){
$param["sEcho"] = $this->input->get('sEcho',true); // DataTables 用来生成的信息
$param["start"] = $this->input->get('iDisplayStart',true); //显示的起始索引
$param["length"] = $this->input->get('iDisplayLength',true);//显示的行数
$param["sort_th"] = $this->input->get('iSortCol_0',true);//被排序的列
$param["sort_type"] = $this->input->get('sSortDir_0',true);//排序的方向 "desc" 或者 "asc".
$param["search"] = $this->input->get('sSearch',true);//全局搜索字段
$param["select_fields"]=$this->select_fields;
$param["show_fields_array"]=$this->show_fields_array;
$output=$this->bl_ds->fetch_more($param);
$output=$this->handle_list($output);
echo json_encode($output); //最后把数据以json格式返回
}
/**
* 处理列表数据
* @param $output
* @return mixed
*/
private function handle_list($output){
if($output["iTotalRecords"]<1) return $output;
return $output;
}
/**
* 创建静态表格
* @param $class
* @return mixed
*/
private function create_table($class){
$this->load->library('table');
$this->table->set_heading(array_values($this->show_fields_array));
$tmpl = array ( 'table_open' => '<table id="'.$class.'" cellpading="0" cellspacing="0" border="0" class="dTable '.$class.' table table-bordered table-striped">' );
$this->table->set_template($tmpl);
$table=$this->table->generate();
return $table;
}
2、模型
public function fetch_more($param){
$select_fields=isset($param["select_fields"])?$param["select_fields"]:"";
$show_fields_array=isset($param["show_fields_array"])?$param["show_fields_array"]:"";
$sEcho=isset($param["sEcho"])?$param["sEcho"]:"";
$start=isset($param["start"])?$param["start"]:0;
$length=isset($param["length"])?$param["length"]:30;
$sort_th=isset($param["sort_th"])?$param["sort_th"]:0;
$sort_type=isset($param["sort_type"])?$param["sort_type"]:"asc";
$search=isset($param["search"])?$param["search"]:"";
$select_fields=implode(",",$show_fields_array);
#按照排序的列 查询
$fields_key=array_values($show_fields_array);
$order_key=$fields_key[$sort_th];
#生成sql语句查询
$sql="select {$select_fields} from some_table where 1=1 {$another_where}";
$query=$this->sm_db->query($sql);
$total=$query->num_rows();
$sql="select {$select_fields} from some_table where 1=1 {$another_where} {$group_by} {$order_by} limit {$start},{$length}";
$query=$this->sm_db->query($sql);
$aaData=$query->result_array();
$output['sEcho'] = $sEcho;
$output['iTotalDisplayRecords'] = $total;
$output['iTotalRecords'] = $total; //总共有几条数据
$output['aaData'] = $aaData;
return $output;
}
3、最后是视图
我使用的是bootstrap样式框架
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="/resources/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="/resources/datatables/media/css/jquery.dataTables.css" />
<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="/resources/bootstrap/css/bootstrap-theme.min.css">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script type="text/javascript" charset="utf-8" src="/resources/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/resources/datatables/media/js/jquery.dataTables.js"></script>
当然最前面要加载jquery.js的不然bootstrap没法使用
html代码:
<div id="main-content">
<div class="content-box">
<div class="content-box-header"><h3 style="margin: -5px 0 0">统计</h3></div>
<div class="content-box-content">
<div class="bs-example bs-example-tabs">
<ul role="tablist" class="nav nav-tabs" id="myTab">
<li class="active"><a data-toggle="tab" role="tab" href="#all">全部统计</a></li>
<li class=""><a data-toggle="tab" role="tab" href="#internal">国内</a></li>
<li class=""><a data-toggle="tab" role="tab" href="#overseas">国外</a></li>
</ul>
<div class="tab-content" id="myTabContent" style="display: block">
<br>
<div id="all" class="tab-pane fade active in">
<?php if(isset($all_table)) echo $bl_all_table;?>
<div class="clear"></div>
</div>
<div id="internal" class="tab-pane fade">
<?php if(isset($internal_table)) echo $bl_internal_table;?>
</div>
<div id="overseas" class="tab-pane fade">
<?php if(isset($overseas_table)) echo $bl_overseas_table;?>
</div>
</div>
</div>
</div>
</div>
</div>
javascript代码:
var show_fields='<?php if(isset($show_fields)) echo $show_fields;?>';
var files_array=show_fields.split(",");
var aoColumns=[];
$.each(files_array,function(key,val){
aoColumns.push({"mData": val});
});
var table_config={
"sPaginationType": "full_numbers", //分页风格,full_number会把所有页码显示出来(大概是,自己尝试)
"sDom":"<'row-fluid inboxHeader'<'span6'<'dt_actions'>l><'span6'f>r>t<'row-fluid inboxFooter'<'span6'i><'span6'p>>", //待补充
"iDisplayLength": 30,//每页显示10条数据
"bAutoWidth": true,//宽度是否自动,感觉不好使的时候关掉试试
"bLengthChange":false,
"bFilter": true,
"oLanguage": {//下面是一些汉语翻译
"sSearch": "搜索",
"sLengthMenu": "每页显示 _MENU_ 条记录",
"sZeroRecords": "没有检索到数据",
"sInfo": "显示 _START_ 至 _END_ 条 共 _TOTAL_ 条",
"sInfoFiltered": "(筛选自 _MAX_ 条数据)",
"sInfoEmtpy": "没有数据",
"sProcessing": "<img src='/resources/admin/images/loading.gif' />", //这里是给服务器发请求后到等待时间显示的 加载gif
"oPaginate":
{
"sFirst": "首页",
"sPrevious": "前一页",
"sNext": "后一页",
"sLast": "末页"
}
},
"bProcessing": true, //开启读取服务器数据时显示正在加载中……特别是大数据量的时候,开启此功能比较好
"bServerSide": true, //开启服务器模式,使用服务器端处理配置datatable。注意:sAjaxSource参数也必须被给予为了给datatable源代码来获取所需的数据对于每个画。 这个翻译有点别扭。开启此模式后,你对datatables的每个操作 每页显示多少条记录、下一页、上一页、排序(表头)、搜索,这些都会传给服务器相应的值。
"sAjaxSource": "", //给服务器发请求的url
"aoColumns":aoColumns,
"fnDrawCallback": function( oSettings ) {//加载新一页完成之后 调用方法
},
"fnRowCallback": function(nRow, aData, iDisplayIndex) {// 当创建了行,但还未绘制到屏幕上的时候调用,通常用于改变行的class风格
$('td:eq(0)', nRow).css({width:"60px"});
$('td:eq(1)', nRow).css({width:"300px"});
$('td:eq(2)', nRow).css({width:"60px"});
$('td:eq(3)', nRow).css({width:"60px"});
$('td:eq(4)', nRow).css({width:"120px"});
$('td:eq(5)', nRow).css({width:"60px"});
return nRow;
},
"fnInitComplete": function(oSettings, json) { //表格初始化完成后调用 在这里和服务器分页没关系可以忽略
$("input[aria-controls='all_table'],input[aria-controls='internal_table'],input[aria-controls='overseas_table']").attr("placeHolder", "搜索列默认显示关键字");
$(".dTable").css("width","100%");
var me_id=$(this).attr("id");
me_id=me_id.replace("_table","");
$("#myTab a[href='#"+me_id+"']").html($("#myTab a[href='#"+me_id+"']").html()+' <span class="badge">'+json.iTotalRecords+'</span>');
}
};
//全部列表
table_config.sAjaxSource="<?php echo base_url("aa/get_data_tables")?>";
$('#all_table').dataTable(table_config);
//国内爆料列表
table_config.sAjaxSource="<?php echo base_url("aa/get_data_tables")?>";
$('#internal_table').dataTable(table_config);
//国外海淘爆料列表
table_config.sAjaxSource="<?php echo base_url("aa/get_data_tables")?>";
$('#overseas_table').dataTable(table_config);
这次是生成了三个列表
jquery.datatable.js与CI整合 异步加载(大数据量处理)的更多相关文章
- 参考 ZTree 加载大数据量。加载慢问题解析
参考 ZTree 加载大数据量. 1.一次性加载大数据量加载说明 1).zTree v3.x 针对大数据量一次性加载进行了更深入的优化,实现了延迟加载功能,即不展开的节点不创建子节点的 DOM. 2) ...
- WPF DataGrid 性能加载大数据
WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...
- 【转载】WPF DataGrid 性能加载大数据
作者:过客非归 来源:CSDN 原文:https://blog.csdn.net/u010265681/article/details/76651725 WPF(Windows Presentatio ...
- ztree插件的使用及列表项拖拽的实现(jQuery)+异步加载节点数据
为了实现如图所示的树状结构图,并使列表项可拖动到盒子里,研究了ztree这个插件的使用,并仔细研究了列表项的拖动事件.完成了预期需求,对jQuery的运用得到了提高.这个插件的功能非常强大,除了基本的 ...
- Angular.JS + Require.JS + angular-async-loader 来实现异步加载 angular 模块
传统的 angular 应用不支持异步加载模块,必须在 module 启动的时候,所有模块必须预加载进来. 通过使用 angular-async-loader 库,我们可以使用 requirejs 等 ...
- Android-LoaderManager异步加载数据库数据
LoaderManager异步加载数据库数据,是在(Activity/fragment/其他UI等) 加载大量的本地Database库表数据,由于数据大在加载过程中会导致UI线程阻塞,导致用户体验不好 ...
- Python爬虫爬取异步加载的数据
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:努力努力再努力 爬取qq音乐歌手数据接口数据 https://y.qq ...
- Jquery zTree结合Asp.net实现异步加载数据
zTree结合Asp.net实现异步加载数据 实现简单操作 zTree 下载 api 访问 :http://www.ztree.me/v3/main.php 例子中用到json数据转化 newtons ...
- jquery和js的几种页面加载函数的方法以及执行顺序
参考博客:http://www.cnblogs.com/itslives-com/p/4646790.html https://www.cnblogs.com/james641/p/783837 ...
随机推荐
- "org.eclipse.wst.validation" has been removed 导入maven 项目出错。
在谷歌中找到解决方案: 右键关闭项目,在打开,将项目刷新,选中项目右键----->Maven4myeclipse------->Update maven project 错误消失. 若还有 ...
- Zabbix实现微信报警
一. 申请企业微信账号,申请地址 https://qy.weixin.qq.com/ 二. 登陆企业微信账 图一 图二 2.添加微信账号 图一 图二 完成以上步骤后 就完成了微信账号的添加 三.新建 ...
- iOS-SDWebimage底层实现原理
其实有些框架的实现原理,并没有想象中那么难,思想也很简单,主要是更新第三方框架的作者对自己写的代码,进行了多层封装,使代码的可读性降低,也就使得框架看起来比较难.我来实现以下SDWebimage的的曾 ...
- Java 循环中标签的作用
continue和break可以改变循环的执行流程,但在多重循环中,这两条语句无法直接从内层循环跳转到外层循环.在C语言中,可以通过goto语句实现多重循环的跳转,但在非循环结构中使用goto语句会使 ...
- 判断手机端用户打开页面时是android还是ios,并将判断结果通过ajax返回给url接口,传递回去
首先判断页面是android还是ios,然后利用ajax将结果通过接口url返回回去,记录到log日志中,以统计android和ios用户访问该页面的数量(数据统计) <script type= ...
- ExecuteReader在执行有输出参数的存储过程时拿不到输出参数
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 后期会在博客首发更新:http://dnt.dkill.net/Article/D ...
- SQL Server 解读【已分区索引的特殊指导原则】(2)- 唯一索引分区
一.前言 在MSDN上看到一篇关于SQL Server 表分区的文档:已分区索引的特殊指导原则,如果你对表分区没有实战经验的话是比较难理解文档里面描述的意思.这里我就里面的一些概念进行讲解,方便大家的 ...
- LINQ系列:Linq to Object聚合操作符
聚合函数在一系列的值上执行特定的运算,并返回单个值,如在给定元素的值上执行求和或计数运算. LINQ共有7种聚合操作符:Aggregate.Average.Count.LongCount.Max.Mi ...
- .Net 转战 Android 4.4 日常笔记(5)--新软件Android Studio 0.5.8安装与配置及问题解决
说真心话,Eclipse跟我们.net的VS比起来就是屌丝比高富帅,一切都是那么的难用,速度慢得我无法忍受 于是想试试Google钦点的Android Studio IDE工具,这跟ADT一样也是一套 ...
- 如何用Node编写命令行工具
0. 命令行工具 当全局安装模块之后,我们可以在控制台下执行指定的命令来运行操作,如果npm一样.我把这样的模块称之为命令行工具模块(如理解有偏颇,欢迎指正) 1.用Node编写命令行工具 在Node ...