CodeIgniter(CI3)框架Grid_View库类

魏康安
2023-12-01
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class Grid_View
 * Implements a simple encapsulation of CodeIgniter tables.
 * Can be used in views.
 */
class Grid_View
{
	private $app = NULL;
	function __construct()
	{
		$this->app = &get_instance();
	}

	/**
	 * Attributes that the model does not own will be retrieved from the CI application.
	 * Equivalent to inheriting CI_Model classes.
	 */
	public function __get($key)
	{
		return $this->app->$key;
	}

	/**
	 * Config template of table.
	 * @param array $tmpl
	 * @return $this
	 */
	public function template($tmpl = array())
	{
		$tmpl['table_open'] = element('table_open', $tmpl, '<div class="table-responsive"><table class="table table-hover" style="table-layout:fixed;word-break:break-all;">');
		$tmpl['heading_cell_start'] = element('heading_cell_start', $tmpl, '<th class="bg-success">');
		$tmpl['table_close'] = element('table_close', $tmpl, '</table></div>');
		$this->table->set_template($tmpl);
		return $this;
	}

	/**
	 * Config heading of table.
	 * @param array $heading
	 */
	public function heading($heading = array())
	{
		$this->table->set_heading($heading);
        return $this;
	}

	/**
	 * Config heading and template of table.
	 * @param $heading
	 * @param array $tmpl
	 * @return $this
	 */
	public function config($heading, $tmpl = array())
	{
		$this->template($tmpl)->heading($heading);
		return $this;
	}

	/**
	 * Use callback function to add table rows.
	 * @param array $rows
	 * @param $call_back //get $row and return $processed_row
	 * @return $this
	 */
	public function load($rows = array(), $call_back)
	{
		foreach ($rows as $row)
		{
			$this->table->add_row(call_user_func($call_back, $row));
		}
		return $this;
	}
	public function generate()
	{
		return $this->table->generate();
	}
}

 

 类似资料: