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 ...
随机推荐
- iOS----自定义UIView,绘制一个UIView
绘制一个UIVIew最灵活的方式就是由它自己完成绘制.实际上你不是绘制一个UIView,你只是子类化了UIView并赋予子类绘制自己的能力.当一个UIVIew需要执行绘图操作的时,drawRect:方 ...
- iOS-Runtime在开发中的使用及相关面试题
OC语言中最为强大的莫过于OC的运行时机制-Runtime,但因其比较接近底层,一旦使用Runtime出现bug,将很难调试,所以Runtime在开发中能不用就不用.下面我将介绍一些Runtime在开 ...
- 使用Ado.net执行SP很慢,而用SSMS执行很快
今天遇到一个问题,有用户反应,在site上打开报表,一直loading,出不来结果. 遇到这种问题,我立刻simulate用户使用Filter Condition,问题repro,看来不是偶然事件,通 ...
- 前端工程师技能之photoshop巧用系列第三篇——切图篇
× 目录 [1]切图信息 [2]切图步骤 [3]实战 前面的话 前端工程师除了使用photoshop进行测量之外,更重要的是要使用该软件进行切图.本文是photoshop巧用系列的第三篇——切图篇 切 ...
- Android图片缓存之Glide进阶
前言: 前面学习了Glide的简单使用(Android图片缓存之初识Glide),今天来学习一下Glide稍微复杂一点的使用. 图片缓存相关博客地址: Android图片缓存之Bitmap详解 And ...
- GoldenGate碎碎念
1. 在启动mgr进程的过程中报如下错误 GGSCI (node1.being.com) > start mgr Cannot - No such file or directory Canno ...
- 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception
使用Spire组件抛出异常The type initializer for 'spr857' threw an exception 我使用免费的Spire.Xls组件尝试去转换Excel文档到PDF文 ...
- 解决ajax跨域请求 (总结)
ajax跨域请求,目前已用几种方法实现: 1)用原生js的xhr对象实现. var url="http://freegeoip.net/json/" ...
- 相克军_Oracle体系_随堂笔记006-日志原理
简单来说,学习Oracle数据库就两个目标: 保证数据库数据的一致性: 提高数据库的性能(这个和日志没关系). 日志的功能: 只是保证数据库数据的一致性: 1.Oracle日志原理 ...
- 花几分钟搭建一个自已的GIT服务器
安装软件: 1.下载GIT服务 For Windows 64 https://git-scm.com/download/win 选中所有功能默认安装便可 2.下载GOGS服务 GOGS具有GITHU ...