Badtable


BadTable DMZ Extension

BadTable comes with its own Datamapper DMZ edition. This makes drawing a table for any Datamapper model as easy as $object->table()!

table (object &badtable columns)

Basic Example:

This basic example will return a table containing all columns in the models validation array, in whatever order they were declared.

$this->load->model('group');

// create instance of BadTable object
$bt = badtable();

// get first 15 group records
$this->group->get(15);

// generate the table
$this->group->table($this->badtable);

// return table HTML
$data['table'] = $this->badtable->generate(array('class' => 'styled-table'));

Custom Example:

This example shows how you can customise your models table.

$this->group->get(15);
		
$columns = array(
	'Delete' => 'checkbox_input[id, delete]',
	'name',
	'About Group' => 'description',
	'created',
	'anchor[id,controller/edit,Edit Record]'
);

$this->group->table($this->badtable, $columns);

$data['table'] = $this->badtable->generate(array('class' => 'styled-table'));

A brief description of what's going on in the $column array in this example.

    • The first element of the $columns array will construct a checkbox input for each row in the first column.
    • It will call a method in the groups model called checkbox_input passing two arguments: id and delete.
    • The checkbox value will be each set to records id.
    • The name of the checkbox array will be delete
    • The code below shows this method:
      class Group extends Datamapper {
      		
         /**
      	* returns a html checkbox string
      	*
      	* @access	public
      	* @param	string
      	* @param	string
      	* @return	string
      	*/
      	public function checkbox_input($field, $name)
      	{	
      		return '<input type="checkbox" name="'.$name.'[]" value="'.$this->{$field}.'" />';
      	}
      }
  1. The second column will be the name field of each record
  2. The third column will be the description field of each record. The column heading, normally sourced from the label element in the field validation array, will be 'About Group'.
  3. The fourth column will be the created field of each record.
    • The last column will contain a unique anchor link to a edit record page for each row.
    • It calls a method in the groups model called anchor passing three arguments: id, 'controller/edit' and 'Edit Record'.
    • The anchors href will be set to 'controller/edit/' + each records id.
    • The anchor text will be Edit Record
    • As no key has been defined for this element, the column heading will be blank.
    • The code below shows this method:
      class Group extends Datamapper {
      		
         /**
      	* returns a unique anchor for this record
      	*
      	* @access	public
      	* @param	string
      	* @param	string
      	* @param	string
      	* @return	string
      	*/
      	public function anchor($field, $uri, $name)
      	{
      		return anchor($uri.'/'.$field, $name);
      	}
      }

With callback functions, BadTable will always look for a method before a function. Hence the anchor method is called and not the CI anchor helper function.