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

[库 Library] 发个自己扩展的CI HTML表格类

[复制链接]
发表于 2011-4-24 13:11:28 | 显示全部楼层 |阅读模式
本帖最后由 nicklung 于 2011-4-24 13:33 编辑

细读了CI的表格类,发现可扩展性还是比较强的,他自身不带列控制的方法,故扩展了一个便于使用
PHP复制代码
 
<?php
class MY_Table extends CI_Table
{
 var $table_attr   = array( 'class'   => 'tbstyle',
          'width'   => '100%',
          'cellspacing' => 2);   // table属性
 var $heading_class  = 'thstyle';        // 标题自定义class
 var $body_class1  = 'tdstyle1';        // 表格体奇数行class
 var $body_class2  = 'tdstyle2';        // 表格体偶数行class
 var $body_attr   = array();         // 表格体td扩展属性
    function __construct()
    {
        parent::__construct();
    }
    function _init_table()
    {
  $temp = '<table>';
  foreach ($this->table_attr as $key => $val) {
   $temp = str_replace('<table', "<table $key=\"$val\"", $temp);
  }
  $this->template['table_open'] = $temp;
  $this->template['heading_cell_start'] = "<th class=\"$this->heading_class\">";
    }
 function set_body($key = '', $data = array())
 {
  $this->body_attr[$key] = $data;
 }
 function _set_body($body)
 {
     if(count($body) == 0) {
   return;
     }
  $i = 0;
  foreach($body as $row) {
   $class = $i % 2 == 0 ? $this->body_class1 : $this->body_class2;
   $j = 0;
   foreach($row as $cell) {
    if(is_array($cell)){
     foreach($this->body_attr as $key => $val) {
      $body1[$i][$j][$key] = $val[$j];
     }
    }
    else {
     $body1[$i][$j]['data'] = $cell;
     $body1[$i][$j]['class'] = $class;
     foreach($this->body_attr as $key => $val) {
      $body1[$i][$j][$key] = $val[$j];
     }
    }
    $j++;
   }
   $i++;
  }
  return $body1;
 }
 function generate($table_data = NULL)
 {
  $this->_init_table();
     $table_data = $this->_set_body($table_data);
  return parent::generate($table_data);
 }
}
?>
 
 
复制代码


下面是调用示例:
PHP复制代码
 
  $this->load->library('table');
  $this->table->set_caption('系统登陆日志');
  $this->table->set_heading('用户名','是否成功','登陆IP地址','登陆时间');
  $this->table->set_body('width', array(0, 120, 100, 150));
  $this->table->set_body('align', array('center', 'center', 'center', 'center'));
  $data = $this->table->generate($rss);
 
 
 
复制代码


可以使用set_body方法给单列设置属性(列宽、对齐方式是最常用的,其他属性也可以)
$this->table->set_body('width', array(0, 120, 100, 150)); //列宽为0的表示撑满表格宽度,也是窗体宽度变化时的宽度可变列

效果图(配合CSS)
QQ截图未命名.png

欢迎大家拍砖,并继续扩展,有了好的扩展方案请通知我交流
发表于 2011-5-2 08:50:20 | 显示全部楼层
学习了
发表于 2011-5-15 23:56:11 | 显示全部楼层
学习了
发表于 2011-8-14 22:56:41 | 显示全部楼层
学习中
发表于 2013-3-26 21:20:38 | 显示全部楼层
我试了不行,有例子下载吗?

本版积分规则