CI_Table::generate PHP Method

generate() public method

Generate the table
public generate ( mixed $table_data = NULL ) : string
$table_data mixed
return string
    public function generate($table_data = NULL)
    {
        // The table data can optionally be passed to this function
        // either as a database result object or an array
        if (!empty($table_data)) {
            if ($table_data instanceof CI_DB_result) {
                $this->_set_from_db_result($table_data);
            } elseif (is_array($table_data)) {
                $this->_set_from_array($table_data);
            }
        }
        // Is there anything to display? No? Smite them!
        if (empty($this->heading) && empty($this->rows)) {
            return 'Undefined table data';
        }
        // Compile and validate the template date
        $this->_compile_template();
        // Validate a possibly existing custom cell manipulation function
        if (isset($this->function) && !is_callable($this->function)) {
            $this->function = NULL;
        }
        // Build the table!
        $out = $this->template['table_open'] . $this->newline;
        // Add any caption here
        if ($this->caption) {
            $out .= '<caption>' . $this->caption . '</caption>' . $this->newline;
        }
        // Is there a table heading to display?
        if (!empty($this->heading)) {
            $out .= $this->template['thead_open'] . $this->newline . $this->template['heading_row_start'] . $this->newline;
            foreach ($this->heading as $heading) {
                $temp = $this->template['heading_cell_start'];
                foreach ($heading as $key => $val) {
                    if ($key !== 'data') {
                        $temp = str_replace('<th', '<th ' . $key . '="' . $val . '"', $temp);
                    }
                }
                $out .= $temp . (isset($heading['data']) ? $heading['data'] : '') . $this->template['heading_cell_end'];
            }
            $out .= $this->template['heading_row_end'] . $this->newline . $this->template['thead_close'] . $this->newline;
        }
        // Build the table rows
        if (!empty($this->rows)) {
            $out .= $this->template['tbody_open'] . $this->newline;
            $i = 1;
            foreach ($this->rows as $row) {
                if (!is_array($row)) {
                    break;
                }
                // We use modulus to alternate the row colors
                $name = fmod($i++, 2) ? '' : 'alt_';
                $out .= $this->template['row_' . $name . 'start'] . $this->newline;
                foreach ($row as $cell) {
                    $temp = $this->template['cell_' . $name . 'start'];
                    foreach ($cell as $key => $val) {
                        if ($key !== 'data') {
                            $temp = str_replace('<td', '<td ' . $key . '="' . $val . '"', $temp);
                        }
                    }
                    $cell = isset($cell['data']) ? $cell['data'] : '';
                    $out .= $temp;
                    if ($cell === '' or $cell === NULL) {
                        $out .= $this->empty_cells;
                    } elseif (isset($this->function)) {
                        $out .= call_user_func($this->function, $cell);
                    } else {
                        $out .= $cell;
                    }
                    $out .= $this->template['cell_' . $name . 'end'];
                }
                $out .= $this->template['row_' . $name . 'end'] . $this->newline;
            }
            $out .= $this->template['tbody_close'] . $this->newline;
        }
        $out .= $this->template['table_close'];
        // Clear table class properties before generating the table
        $this->clear();
        return $out;
    }

Usage Example

Example #1
0
 /**
  * Setup columns
  *
  * @access	public
  * @param	string	data callback function
  */
 public function generate($table_data = NULL)
 {
     if (!$this->jq_template) {
         return parent::generate($table_data);
     }
     $this->_compile_template();
     $open_bak = $this->template['table_open'];
     // prep the jquery template
     $temp = $this->template['row_start'];
     foreach ($this->column_config as $column => $config) {
         $html = FALSE;
         if (is_array($config)) {
             $html = isset($config['html']) ? (bool) $config['html'] : FALSE;
         }
         // handle data of array('data' => 'content', 'attr' => 'value')
         $temp .= '{{if $.isPlainObject(' . $column . ')}}';
         $temp .= substr($this->template['cell_start'], 0, -1);
         $temp .= '{{each ' . $column . '}}';
         $temp .= '{{if $index != "data"}} ${$index}="${$value}" {{/if}}';
         $temp .= '{{/each}}';
         $temp .= '>';
         $temp .= $html ? '{{html ' . $column . '.data}}' : '${' . $column . '.data}';
         $temp .= '{{else}}';
         $temp .= $this->template['cell_start'];
         $temp .= $html ? '{{html ' . $column . '}}' : '${' . $column . '}';
         $temp .= '{{/if}}';
         $temp .= $this->template['cell_end'] . "\n";
     }
     $temp .= $this->template['row_end'];
     $template = $temp;
     // add data to our headings for the sort mechanism
     $column_k = array_keys($this->column_config);
     foreach ($this->heading as $k => &$heading) {
         if (!is_array($heading)) {
             $heading = array('data' => $heading);
         }
         if (!$this->column_config[$column_k[$k]]['sort']) {
             $heading['class'] = 'no-sort';
         }
         $heading['data-table_column'] = $column_k[$k];
     }
     if (!$this->base_url) {
         $this->base_url = $this->EE->cp->get_safe_refresh();
     }
     $jq_config = array('base_url' => $this->base_url, 'columns' => $this->column_config, 'template' => $template, 'empty_cells' => $this->empty_cells, 'no_results' => $this->no_results, 'pagination' => $this->pagination_tmpl, 'uniqid' => $this->uniqid, 'sort' => $this->sort, 'rows' => $this->raw_data);
     $table_config_data = 'data-table_config="' . form_prep($this->EE->javascript->generate_json($jq_config, TRUE)) . '"';
     $this->template['table_open'] = str_replace('<table', '<table ' . $table_config_data, $open_bak);
     $table = parent::generate();
     $this->template['table_open'] = $open_bak;
     return $table;
 }
All Usage Examples Of CI_Table::generate