用户
 找回密码
 入住 CI 中国社区
搜索
查看: 8473|回复: 7
收起左侧

[库 Library] Jquery.DataTables 插件(Json驱动)

[复制链接]
发表于 2011-4-14 09:27:39 | 显示全部楼层 |阅读模式
附件就是

使用的时候直接在controller中写一个方法

function userlist_json()
{
        $columns = array('user.uid','password','active_time');               
        echo $this->json->get('user','uid', $columns,"delete <> 1");
}

然后在DataTables中直接调用(ajax请求)这个方法即可。

json.rar

2.14 KB, 下载次数: 117

json

评分

参与人数 1威望 +5 收起 理由
Hex + 5 原创内容

查看全部评分

发表于 2011-4-14 09:30:33 | 显示全部楼层
呵呵,只用用CI的db生成的结果,然后json_encode不行么?

点评

不错.这个论坛怎么没加分的功能?  发表于 2011-11-19 15:12
 楼主| 发表于 2011-4-14 09:56:06 | 显示全部楼层
因为dataTables本身还有模糊搜索和分页,需要接收和处理一些$_GET参数,另外,加了一个事件,可以通过该事件对数据进行进一步的处理。比如数据库中是0和1 ,然后换成 男和女之类的。
当然,这些处理都不复杂。这个类只是把这些功能通用化了,每次调用的时候,只写最少的代码是我的目标。

其实最后调用的也是json_encode
有现成的就不重写:)
 楼主| 发表于 2011-4-14 10:40:24 | 显示全部楼层
直接贴出来得了。
PHP复制代码
 
<?php   if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
/**
 * DataTables AJAX json 数据驱动
 *
 * 针对DataTables提供指定格式的json数据返回。
 *
 * TheSmallCar@Gmail.com
 *
 * 版权声明:      该份代码完全开放源代码,任何人可以在任何场合和环境下使用、分发、修改。
 *                      但请保留该头部注释的内容(作者、Email、版权以及其他注释内容)。
 *
 * ---------------------------------[更新列表]---------------------------------
 * 2011-04-11    完成基本结构。
 * 2011-04-12    使用CI的db驱动的Active Record 类修改了一部分对数据库的调用。
 *                       同时支持了简单的表连接
 * 2011-04-13    增加 $where='' 参数。
 * 2011-04-14    增加 event_json_row_data_serializing 事件,以支持对数据的特殊处理。
 *
 *
 * ---------------------------------[bug列表]---------------------------------
 * 1. 2011-04-13        发现   关于delete数据的搜索,存在问题
        2011-04-13      关闭   在使用时,通过参数$where来传入 `delete` <> 1 这样的条件。
 。
 */

 
class json {
       
        public $table = null;           //表
        public $columns = null; //列
        public $prime = null;           //主键
        public $db = null;              //数据库类
       
        private $limit = '';   
        private $order = '';
        private $where = '';
       
        //////////////[事件定义]//////////////
        /*
        * 数据输出
        * 可以使用本事件来修改数据库中查询出来的数据。
        * 参数 $row
        * 返回值可以修改 $row
        *
        * 示例:
        *       $this->json->event_json_row_data_serializing = array($this,'row_data_serializing');
        *       然后在row_data_serializing中处理$row参数并返回结果即可。
        */

        public $event_json_row_data_serializing = null;
       
        //////////////[方法定义]//////////////
       
        public function __construct($db){
                $this->db = $db['db'];
        }
       
        public function get($table, $prime, $columns, $where='', $join='', $join_condition='', $join_type='')
        {
                $this->table = $this->db->dbprefix.$table;
                $this->prime = $prime;
                $this->columns = $columns;
                $this->build();
               
                $this->db->select(implode(", ", $columns));
                $this->db->from($this->table);
               
                if($where != '')
                        $this->db->where($where);
               
                if($join != null)
                {
                        if($join_condition == '')
                                throw new Exception( '表连接缺少连接条件!' );
                       
                        $this->db->join($join, $join_condition, $join_type);
                }
               
                if($this->where != '')
                        $this->db->where($this->where);
                       
                if($this->order != '')
                        $this->db->order_by($this->order);
               
                if($this->limit != '')
                        $this->db->limit($this->limit);
               
                $query = $this->db->get();
                $data = $query->result_array();
               
                //echo $this->db->last_query();        
               
                $count = count($data);
               
                $sQuery = "     SELECT COUNT(".$prime.") FROM `$this->table`";
                $display = $this->db->query($sQuery);
                $display = $display->result_array();
                $display = $display[0];
 
                return $this->serialize($data, $count, $display);
        }
       
        private function build()
        {
                //limit
                $this->limit = "";
                if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
                {
                        $this->limit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
                                mysql_real_escape_string( $_GET['iDisplayLength'] );
                }
               
                //order
                if ( isset( $_GET['iSortCol_0'] ) )
                {
                        $this->order = "ORDER BY  ";
                        for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
                        {
                                if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
                                {
                                        $this->order .= $this->columns[ intval( $_GET['iSortCol_'.$i] ) ]." ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
                                }
                        }
                       
                        $this->order = substr_replace( $this->order, "", -2 );
                       
                        if ( $this->order == "ORDER BY" )
                        {
                                $this->order = "";
                        }
                }
 
                //where
                $this->where = "";
                if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
                {
                        $this->where = "WHERE (";
                        for ( $i=0 ; $i<count($this->columns) ; $i++ )
                        {
                                $this->where .= $this->columns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
                        }
                        $this->where = substr_replace( $this->where, "", -3 );
                        $this->where .= ')';
                }
 
                for ( $i=0 ; $i<count($this->columns) ; $i++ )
                {
                        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
                        {
                                if ( $this->where == "" )
                                {
                                        $this->where = "WHERE ";
                                }
                                else
                                {
                                        $this->where .= " AND ";
                                }
                                $this->where .= $this->columns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
                        }
                }
        }
       
        private function serialize($data, $count, $display)
        {
                $output = array(
                        "sEcho" => isset($_GET['sEcho'])?intval($_GET['sEcho']):0,
                        "iTotalRecords" => $count,
                        "iTotalDisplayRecords" => $display,
                        "aaData" => array()
                );
               
                foreach ($data as $row)
                {
                        //event_select_begin事件
                        if( isset($this->event_json_row_data_serializing) )
                        {
                                $row = call_user_func($this->event_json_row_data_serializing, $row);
                        }
                       
                        $line = array();
                        foreach($row as $key=>$col)
                        {
                                $line[] = $row[$key];
                        }
                        $output['aaData'][] = $line;
                }
               
                $this->event_json_row_data_serializing = null;
               
                return json_encode( $output );
        }
}
 
复制代码
 楼主| 发表于 2011-4-14 10:41:04 | 显示全部楼层
~~~额,贴出来的效果不太理想
发表于 2011-11-19 18:48:38 | 显示全部楼层
ci最新版需要修正下.
PHP复制代码
                if (isset ( $_GET ['iSortCol_0'] )) {
                        if ($_GET ['iSortCol_0'] != 0) {
                                $this->order = "ORDER BY  ";
                                for($i = 0; $i < intval ( $_GET ['iSortingCols'] ); $i ++) {
                                        if ($_GET ['bSortable_' . intval ( $_GET ['iSortCol_' . $i] )] == "true") {
                                                $this->order .= $this->columns [intval ( $_GET ['iSortCol_' . $i] )] . " " . mysql_real_escape_string ( $_GET ['sSortDir_' . $i] ) . ", ";
                                        }
                                }
                               
                                $this->order = substr_replace ( $this->order, "", - 2 );
                               
                                if ($this->order == "ORDER BY") {
                                        $this->order = "";
                                }
                        }
                }
 
 
复制代码


发表于 2011-11-20 00:00:10 | 显示全部楼层
mark一下
发表于 2013-6-14 10:32:59 | 显示全部楼层
本帖最后由 poqiuhaha 于 2013-6-14 11:19 编辑

不知道能不能再2.1.3中使用哇 尝试下
有没有完整的源码哇 请联系qq191766302

本版积分规则