fwhenin
fwhenin

Reputation: 473

Laravel 4: Generate HTML Table Like CodeIgniter

So in CodeIgniter, there was a cool feature of creating an HTML table just by passing it an array and it handles the header, etc. Is there ANYTHING out there for Laravel, has anybody been able to use the CI version in Laravel? just asking around

Upvotes: 1

Views: 4372

Answers (3)

The Alpha
The Alpha

Reputation: 146201

There is nothing like this in Laravel AFAIK but you may create your own, something like (an idea only) this (a php class, not only for Laravel)

class Table {

    protected $table = null;
    protected $header = null;
    protected $attr = null;
    protected $data = null;

    public function __construct($data = null, $attr = null, $header = null)
    {
        if(is_null($data)) return;
        $this->data = $data;
        $this->attr = $attr;
        if(is_array($header)) {
            $this->header = $header;
        }
        else {
            if(count($this->data) && $this->is_assoc($this->data[0]) || is_object($this->data[0])) {
                $headerKeys = is_object($this->data[0]) ? array_keys((array)$this->data[0]) : array_keys($this->data[0]);
                $this->header = array();
                foreach ($headerKeys as $value) {
                    $this->header[] = $value;
                }
            }
        }
        return $this;
    }

    public function build()
    {
        $atts = '';
        if(!is_null($this->attr)) {
            foreach ($this->attr as $key => $value) {
                $atts .= $key . ' = "' . $value . '" ';
            }
        }
        $table = '<table ' . $atts . ' >';

        if(!is_null($this->header)) {
            $table .= '<thead><tr>';
            foreach ($this->header as $value) {
                $table .= '<th>' . ucfirst($value) . '</th>';
            }
            $table .= '</thead></tr>';
        }

        $table .= '<tbody>';
        foreach ($this->data as $value) {
            $table .= $this->createRow($value);
        }
        $table .= '</tbody>';
        $table .= '</table>';
        return $this->table = $table;
    }

    protected function createRow($array = null)
    {
        if(is_null($array)) return false;
            $row = '<tr>';
            foreach ($array as $value) {
                $row .= '<td>' . $value . '</td>';
            }
            $row .= '</tr>';
            return $row;
    }

    protected function is_assoc($array){
        return is_array($array) && array_diff_key($array, array_keys(array_keys($array)));
    }
}

Now, you can use it as given below (An Example Here.)

$data = array(
    array('name' => 'Heera', 'age'=>'35', 'address' =>'Masimpur', 'phone'=>'123456'),
    array('name' => 'Usman', 'age'=>'28', 'address' =>'Kamal Gor', 'phone'=>'654321')
);
$attr = array('class'=>'tbl someClass', 'id'=>'myTbl', 'style'=>'width:400px;color:red', 'border'=>'1');
$t = new Table($data, $attr);
echo $t->build();

Or, set an header using third argument, like

$t = new Table($data, $attr, array('Known As', 'Years', 'Location', 'Contact'));

This is just an idea and could be better. Now just integrate this class with Laravel using Laravel's rule. You may extend Html class or use as an individual class by registering it as a service. Take a look at this answer for extending a class in laravel.

Upvotes: 6

Nayjest
Nayjest

Reputation: 1089

Try nayjest/grids Laravel package.

Upvotes: 2

that0n3guy
that0n3guy

Reputation: 577

You could take the script drupal uses and convert it to laravel: https://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/theme_table/8

Doing a quick look at it, you would just need to replace a couple of functions:

  • Attributes()... I think laravel has something like this you could use. Drupal 8 is using something from symfony2 I think... laravel might be using the same thing.
  • _theme_table_cell(), tablesort_header(), etc... These type of functions have drupal_render() in them... you don't want to try and port that over... so you probably just delete that from the functions (untested) as it doesn't make sense in the context of laravel .

This would make for a very nice implementation of what your looking for.

EDIT: I also just ran into this: http://kohana.keyframesandcode.com/docs/modules/table/ I've not tested it but it was referenced here: http://forumsarchive.laravel.io/viewtopic.php?id=2178

Upvotes: 0

Related Questions