Php html table framework

README

You can see much of the functionality in the above example. More detailed explanations are below.

Reference

Table

The table object represents a top level HTML table.

See Classable below for information on setting the rows’ classes.

See Attributable below for information on setting other attributes on the cell element.

It has four other main methods.

$table->render()

This is a helpful shortcut to doing the following:

(new TableRenderer)->renderElement($table);

If you want to pretty print the HTML in your table, you can set it like so:

(new TableRenderer)->prettyPrint()->renderElement($table);

These all return a table section object (see below).

These will be initialised the first time you call the function, so if you never call ->header() , then a section will not be rendered.

You can call them multiple times, and they will return the same object.

Section ( thead , tbody , tfoot )

These objects represent sections in a table.

See Classable below for information on setting the rows’ classes.

See Attributable below for information on setting other attributes on the cell element.

There are two other methods to talk about here.

$section->row()

This initialises and returns a new Row object. Calling it multiple times will add multiple rows to the table.

$section->nextRow(Row $current)

Returns the next row in its children from the one passed to it.

Throws an error if passed a row that is not one of its children.

See the code example for the logic of how this works.

$one = $section->row(); $two = $section->row(); $section->nextRow($one) === $two; // true $section->nextRow($two); // a new row

Row

These objects represent rows in a table section.

See Classable below for information on setting the rows’ classes.

See Attributable below for information on setting other attributes on the cell element.

$row->addCell(CellInterface $cell)

This adds a new cell to the row. You can use this to add a custom cell to the row.

The following helper methods use this method to add the basic included cell types.

Cells

These objects represent cells in a table row.

See Spannable below for information on setting the column spanning.

See Classable below for information on setting the cells’ classes.

See Attributable below for information on setting other attributes on the cell element.

Content Cell

This is the default cell. It is fairly versatile, although basic.

You can set the content on initialiastaion, or by calling the ->content(string $content) method.

The default behaviour is to escape the content. If you want to override this, chain in the ->dontEscape() or ->raw() methods.

There is a ->wrapContent(string $open, string $content, string $close) method. This lets you enter some unescaped HTML that wrap some escaped content. Essentially, all this does is escape the $content value, then concatenate them in order, and set the cell to ->raw() or ->dontEscape() . Nevertheless, it can still be helpful — allowing you to mix your own unescaped HTML with some content that should be escaped.

As with any unescaped content, it is up to you to ensure the HTML is valid.

See below for some examples.

$row->cell('Cat'); // Cat $row->cell(''); // <Cat> $row->cell('')->raw(); // $row->cell()->content('Cat'); // Cat $row->cell()->wrapContent('', '', ''); // <Cat>

A link cell is a cell with a link in it. Other than the link argument to its constructor, it behaves like a ContentCell

$row->linkCell('https://google.com')->content('Visit Google.'); // Visit Google. $cell = new LinkCell('localhost')->content('
some preformatted text

')->raw();

Image Cell

$row->imageCell('cat.jpg'); // 

Traits

Spannable

On the Cell objects, you can call:

// setting rowspan $row->cell('A double height column')->spanRows(2); // setting colspan $row->cell('A triple width column')->spanColumns(3);

This returns the element object for chaining.

Classable

On any of the element objects — Table , Section , Row , Cell , you can set the class. The following three examples all result in :

// This is the semantic ui CSS framework table class $table->class('ui table'); // You don't have to set the classes at the same time. $table->class('ui')->class('table'); // You can pass an array of string $table->class(['ui', 'table']);

This returns the element object for chaining.

Attributable

You can set any other HTML attribute:

This returns the element object for chaining.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

An HTML table generator written in PHP

License

GunnarEriksson/chtmltable

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Scrutinizer Code Quality Code Coverage

CHTMLTable is an PHP class for generating HTML tables with data from an array of data objects. An example of an array of data objects, is when fetching data from an MySQL database with the fetch style PDO::FETCH_OBJ.

It can to be used with Anax MVC, but is not dependent on it. It can be included in any other project.

To install the package, add the row below to your composer.json file:

If the Anax-MVC framework is used, copy the webroot/table-anax-mvc.php file and the webroot/css/html-table.css file to each folder in the Anax-MVC webroot directory

Get the data to the table

To display values in the table, the HTMLTable uses an array of data objects. Each key each key represents a column with the corresponding value as the table value.

$data = [ [0] => stdClass Object ( [Column1] => Table Cell 1 [Column2] => Table Cell 2 [Column3] => Table Cell 3 [Column4] => Table Cell 4 [Column5] => Table Cell 5 ) [1] => stdClass Object ( [Column1] => Table Cell 6 [Column2] => Table Cell 7 [Column3] => Table Cell 8 [Column4] => Table Cell 9 [Column5] => Table Cell 10 ) ]; 

To set the CSS id/class and the caption for the table, use the table specification array.

$tableSpecification = [ 'id' => 'test-table', 'class' => 'test-table', 'caption' => 'The table' ]; 

If no CSS id or class is set, the default id is «html-table».

It is possible to set a number of specifications for a column. To set column specifications, use the associative column specification array.

$columnSpecification = [ $Column1 => [ ], $Column3 => [ 'title' => 'Table Header 2', ], $Column2 => [ 'title' => 'Table Header 3', 'function' => function($link) < return '' . htmlentities($link, null, 'UTF-8') . ''; > ], $object1 => [ 'title' => 'Table Header 4', 'function' => function($object) < return htmlentities($object->Column4, null, 'UTF-8'); > ], $Column5 => [ 'title' => 'Table Header 5', 'function' => function($isPresent) < return empty($isPresent) ? 'Not present' : 'Present'; >], 'tablefoot1' => [ 'type' => 'footer', 'colspan' => '4', 'value' => 'Footer Cell 1', ], 'tablefoot2' => [ 'type' => 'footer', 'function' => function() < return 'Link'; > ], ]; 

In the column specification it is possible to determine the number of columns in the table and the settings of the columns. The column specification sets the number of columns and in which order they are shown in the table. Top to bottom in the column specification corresponds to left to right in the table.

To get a value from the data object, the key name in the column specification must correspond to the key name in the data object. For example the key Column1, in the column specification fetches the value in the data object with the key Column1.

To fetch the whole data object, the name of the key in the column specification must start with object (case insensitive), for example object, object1 and so on. If two or more columns wants to fetch the data object, the key name must be unique. You can not use the name object1 twice or more. Notice! It is only possible to fetch a data object in combination with a function defined.

If no title is set, the CHTMLTable uses the name of the keys in the first object as the name of the titles in the table.

It is possible to use the function for more advanced settings of the column. The CHTMLTable uses the call_user_func function to get settings from an anonymous function. This examples shows how to add HTML tags, to all cells in the column, to create a link and a function to return ‘Not present’ or ‘Present’ depending if the value for the cell is included in the input array (data). It is also possible to fetch the object with the key name starting with the name «object». Note! CHTMLTable does NOT use the function htmlentities() when using a function for the data. The protection against harmful data must be added in the specified function.

To add a footer, use the type tag and set the value to ‘footer’. If no type is added, the setting is regarded to be column setting. If only a simple value should be added to the footer, use the tag ‘value’. Otherwise use the function tag for more advanced settings. For a footer, it is also possible to set the tag ‘colspan’ to span the columns in the footer row.

Create and get the table in HTML

$table = new \Guer\HTMLTable\CHTMLTable(); $table = $table->create($tableSpecification, $data, columnSpecification); $table->getHTMLTable(); 

To generate a not specified table, just exclude the table specifications. The data is presented as it is in number of columns according to the number of keys in the data objects. The title of the columns are the name of the keys in the data object. CHTMLTable uses the function htmlentities() to show data in the table cells.

$table = $table->create([], $data, []); $table->getHTMLTable(); 

This software is free software and carries a MIT license.

Источник

Читайте также:  Css not selector sass
Оцените статью