Codeigniter DataGrid类

时间:2014-03-07 20:55:44

标签: php codeigniter datagrid

所以,我正在为我的CodeIgniter应用程序构建一个简单的(ish)数据网格类。

我想知道的是,我有一些我希望“格式化”的列,我的意思是,有些列可能只包含10,但我想将它们转换为分别为YesNo

我该怎么做?换句话说,我希望能够传递另一个参数......如:

$this->_columnCallBack将类似array(column_number=>'NameOfCallBackFunction')

的数组传递给它

我假设我会这样做,就像我做_columnclass,我传入列号,类作为数组......但我不知道怎么会得到发射功能以进行更换...

代码

class O7thDG {

    public function __construct($params){
        $this->_table = $params['table'];
        $this->_pk = $params['pk'];
        $this->_fields = (isset($params['fields'])) ? $params['fields'] : null;
        $this->_where = (isset($params['where'])) ? $params['where'] : null;
        $this->_order = (isset($params['order'])) ? $params['order'] : null;
        $this->_extras = (isset($params['extras'])) ? $params['extras'] : null;
        $this->_add = (isset($params['add'])) ? $params['add'] : FALSE;
        $this->_edit = (isset($params['edit'])) ? $params['edit'] : FALSE;
        $this->_delete = (isset($params['delete'])) ? $params['delete'] : FALSE;
        $this->_editlink = (isset($params['editlink'])) ? $params['editlink'] : null;
        $this->_deletelink = (isset($params['deletelink'])) ? $params['deletelink'] : null;
        $this->_editlinkextras = (isset($params['editlinkextras'])) ? $params['editlinkextras'] : null;
        $this->_deletelinkextras = (isset($params['deletelinkextras'])) ? $params['deletelinkextras'] : null;
        $this->_tableid = (isset($params['tableid'])) ? $params['tableid'] : null;
        $this->_tableclass = (isset($params['tableclass'])) ? $params['tableclass'] : null;
        $this->_columnclass = (isset($params['columnclass'])) ? $params['columnclass'] : null;
        $this->_includeheader = (isset($params['includeheader'])) ? $params['includeheader'] : TRUE;
        $this->_allowpaging = (isset($params['allowpaging'])) ? $params['allowpaging'] : FALSE;
        $this->_sorting = (isset($params['sorting'])) ? $params['sorting'] : null;
        $this->_columncallback = (isset($params['columncallback'])) ? $params['columncallback'] : null;
    }

    public function BuildIt($responsive = TRUE){
        $_ci =& get_instance();
        $_ci->load->database();
        $_ci->load->library('table');
        $_ci->load->library('TKCommon', null, 'comm');
        $fldlist = $this->_buildSelectFieldList();
        $_ci->db->select($fldlist);
        $cols = $this->_buildColumnFieldList();
        $ret = '';
        if($this->_where != null){
            // build the where

        }
        if($this->_order != null){
            // build the order

        }
        if($this->_extras != null){
            // build the extras

        }           
        // Query the specified table
        $qry = $_ci->db->get($this->_table);
        if($cols == null){
            $cols = $_ci->db->list_fields($this->_table);
            $fldlist = $cols;
        }else{
            $fldlist = explode(', ', $fldlist);
        }
        if($qry){
            // throw the results into an associative array
            $rs = $qry->result_array();
            if($rs){
                $rCt = count($rs);
                $cCt = $qry->num_fields();
                // add our responsive wrapper
                if($responsive){
                    $ret .= '<div class="table-responsive">';
                }
                // fire up our table
                $tid = '';
                $tc = '';
                if($this->_tableid != null){$tid = ' id="' .$this->_tableid . '"';}
                if($this->_tableclass != null){$tc = ' class="' .$this->_tableclass . '"';}
                $_ci->table->set_template(array('table_open'=>'<table' . $tid . $tc . '>'));
                // build our header row, but only if we need to
                if($this->_includeheader && $cCt > 0){
                    // see if we need to include the admin column
                    if($this->_edit || $this->_delete){
                        $_ci->table->set_heading(array_merge($cols, array('Admin')));
                    }else{
                        $_ci->table->set_heading($cols);
                    }
                }
                // build each records row
                for($r = 0; $r < $rCt; ++$r){
                    $ca = array();
                    for($c = 0; $c < $cCt; ++$c){
                        if(($this->_columnclass != null) && ($c == key($this->_columnclass))){
                            // figure out which column needs the class, and what class needs to be applied
                            $ca[] = $this->_columnCallback($c, array('data'=>$rs[$r][$fldlist[$c]], 'class'=>$this->_columnclass[key($this->_columnclass)]));
                        }else{
                            $ca[] = $this->_columnCallback($c, $rs[$r][$fldlist[$c]]);  
                        }
                    }
                    // see if we need to include the admin column
                    if(($this->_edit || $this->_delete) && ($this->_editlink != null || $this->_deletelink != null)){
                        $txt = '';
                        if($this->_edit &&($this->_editlink != null)){
                            $txt .= '<a href="' . $this->_editlink . '?id=' . $rs[$r][$this->_pk] . '"><span class="fa fa-pencil fa-lg"></span></a>&nbsp;&nbsp;&nbsp;';
                        }
                        if($this->_delete &&($this->_deletelink != null)){
                            $txt .= '<a href="' . $this->_deletelink . '?id=' . $rs[$r][$this->_pk] . '"><span class="fa fa-trash-o fa-lg"></span></a>';
                        }
                        if(($this->_columnclass != null) && ($cCt == key($this->_columnclass))){
                            $ca[] = array('data'=>$txt, 'class'=>$this->_columnclass[key($this->_columnclass)]);
                        }else{
                            $ca[] = $txt;
                        }
                    }
                    $_ci->table->add_row($ca);
                }                   
                $ret .= $_ci->table->generate();
                // close our responsive wrapper
                if($responsive){
                    $ret .= '</div>';
                }
            }else{
                $ret .= $_ci->comm->ErrorBox('There was an issue running the query, please make sure at least your primary key, and table are correct.');
            }
        }else{
            $ret .= $_ci->comm->ErrorBox('There was an issue running the query, please make sure at least your primary key, and table are correct.');
        }
        return $ret;
    }

    // build our select's field list
    private function _buildSelectFieldList(){
        if($this->_fields == null){
            return '*'; 
        }else{
            $flds = array_map(function($item){return $item['field'];}, $this->_fields);
            return implode(', ', $flds);
        }
    }

    // build our tables column list
    private function _buildColumnFieldList(){
        if($this->_fields == null){
            return null;
        }else{
            return array_map(function($item){return $item['label'];}, $this->_fields);
        }
    }

    private function _columnCallback($col, $val){
        if($this->_columncallback != null){
            if($col == key($this->_columncallback))
                return $this->_columncallback[key($this->_columncallback)]($val);
        }else{
            return $val;
        }
    }

}

我可能想要使用的外部函数只是:

// Format boolean value to Yes or No
public function YesNo($val){
    return ((bool)$val) ? 'Yes' : 'No' ;    
}

表类的CI文档有$this->table->function,但是,传递的函数适用于整个表

3 个答案:

答案 0 :(得分:0)

这些功能可以按您的需要工作,但您需要根据需要重写一下。

function callback1($array, $row_id, $function)
{
    if(function_exists($function))
        return $function($array[$row_id]);
    else
        return 0;
}

function YesNo($val) 
{
    return intval($val) ? 'Yes' : 'No' ;    
}

#example of array and usage of script.
$array = array('id' => '0', 'id2' => '1');
$ok = callback1($array, 'id2', 'YesNo');

答案 1 :(得分:0)

制作过滤器数组

$this->filters = ['column_name' => 'function_name', 'column_name2' => 'otherFunction'];

然后您只需将过滤器应用于数据

if ( array_key_exists($column_name, $this->filters) ) {
   // Add row that contains filtered data
   $row_data = $this->filters[$column_name]($column_val);
}else{
   $row_data = $column_val;
}

如果您的功能在全球范围内不可用(不是在帮助程序或内置的php中),那么您必须使用以下内容:

call_user_func(array($this, $this->filters[$column_name]), $column_val);

OR

$this->{$this->filters[$column_name]}($column_val);

这将从库O7 ...

中运行一个函数

答案 2 :(得分:0)

使用和/或匿名函数怎么样:

http://docs.php.net/manual/es/function.func-get-args.php

http://php.net/manual/en/functions.anonymous.php

更新2&#34;重新加载&#34;

您还可以检查杂货店crud,它的CI库并做一些对您有用的有趣事项:

http://www.grocerycrud.com/

Grocery crud 使用PHP中的call_user_func,而允许您使用控制器中声明的任何函数,如果这是你需要的那么,那么&#39;只是时间问题,检查杂货店的代码。

在库中为每个回调声明一个受保护的属性,例如(第3386行v1.4.1行)

/* Callbacks */
protected $callback_before_insert = null;

所以你/我/任何人都可以在需要的时候设置回调函数,然后检查所做的任何回调,例如(第878行GroceryCrud 1.4.1)

if($this->callback_before_insert !== null)
{
    $callback_return = call_user_func($this->callback_before_insert, $post_data);

        if(!empty($callback_return) && is_array($callback_return))
            $post_data = $callback_return;
       elseif($callback_return === false)
            return false;
}

当然有一种设置回调的方法(第4.51行第4518行):

public function callback_before_insert($callback = null)
{
    $this->callback_before_insert = $callback;
    return $this;
}

用户/ dev设置回调:

$crud->callback_before_insert(array($this,'my_callback'));

和/或这种技术允许你使用类似的东西:

$this->load->model('Customers');
$crud->callback_before_insert(array($this->Customers,'getCustomersCallback'));

即使您使用php 5.3或更高版本,也可以使用匿名方法:

$crud->callback_before_insert(function($post_array){
    $post_array['user_id'] = $this->session->userdata('user_id');
    return $post_array;
});

更多信息:http://www.grocerycrud.com/documentation/tutorial_using_callbacks

关于Grocery Crud:

作者:John Skoumbourdismore about author/library

网址:GroceryCrud

许可证:使用GPL v3和MIT许可证以双重许可发布。

更新3 更新我已经仔细阅读了您的问题,思考函数变量可以为您服务:

http://php.net/manual/es/functions.variable-functions.php

请在此处查看第一个和第二个示例:http://www.php.net/manual/en/language.oop5.php

仍然不知道他们是否会在外面工作&#34;你的班级,但它可以。