An open API service indexing awesome lists of open source software.

https://github.com/andydune/htmltable

This code will simplify or improve your work with html tables.
https://github.com/andydune/htmltable

html-table html-table-generator

Last synced: about 1 year ago
JSON representation

This code will simplify or improve your work with html tables.

Awesome Lists containing this project

README

          

# HtmlTable
This code will simplify or improve your work with html tables.

[![Build Status](https://travis-ci.org/AndyDune/HtmlTable.svg?branch=master)](https://travis-ci.org/AndyDune/HtmlTable)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Packagist Version](https://img.shields.io/packagist/v/andydune/html-table.svg?style=flat-square)](https://packagist.org/packages/andydune/html-table)
[![Total Downloads](https://img.shields.io/packagist/dt/andydune/html-table.svg?style=flat-square)](https://packagist.org/packages/andydune/html-table)

Requirements
------------

PHP version >= 5.6

Installation
------------

Installation using composer:

```
composer require andydune/html-table
```
Or if composer was not installed globally:
```
php composer.phar require andydune/html-table
```
Or edit your `composer.json`:
```
"require" : {
"andydune/html-table": "^1"
}

```
And execute command:
```
php composer.phar update
```

Example
-----------

Here is small part of code for drawing html table with dynamic data.

```php



ID
User
STATUS
Created
Updated
Uploaded
Link
Data








= $file->getId(); ?>
= $user['EMAIL'] ?> (= $user['XML_ID'] ?>)
= $showStatus($file->getStatus()); ?>
= $file->getData('DATETIME'); ?>
= $file->getData('DATETIME_UPDATE'); ?>
= $file->getData('DATETIME_LAST_REQUEST'); ?>
= $file->getFileName(); ?>.= $file->getFileType() ?>
$showArray($file->getMeta()) ?>

повтор
удаление


```
It is simple to accidentally break html by removing single tag. And it is difficult to read and change.

There is better code down below:
```php
use AndyDune\HtmlTable\Builder;
use AndyDune\HtmlTable\Table;

$table = new Table();
$head = $table->head();
$head->cell()->setContent('ID');
$head->cell()->setContent('User');
$head->cell()->setContent('STATUS');
$head->cell()->setContent('Created');
$head->cell()->setContent('Updated');
$head->cell()->setContent('Uploaded');
$head->cell()->setContent('Uploaded');
$head->cell()->setContent('Data');
// Empty cells will be added automatically depends on max cell count for next rows.
foreach ($list as $file) {
$row = $table->row();
if ($file->getStatus() == 2) {
$row->addClass('table-success')
}
$row->cell()->setContent($file->getId());
$row->cell()->setContent($user['EMAIL'] . $user['XML_ID']);
$row->cell()->setContent($showStatus($file->getStatus()));
$row->cell()->setContent($file->getData('DATETIME'));
$row->cell()->setContent($file->getData('DATETIME_UPDATE'));
$row->cell()->setContent($file->getData('DATETIME_LAST_REQUEST'));
$row->cell()->setContent($file->getFileName() . '.' . $file->getFileType());
$row->cell()->setContent($showArray($file->getMeta()));
}

$buider = new Builder($table);
$builder->setGroupingSections(true);
echo $buider->getHtml();
```

Class structure
----------

Table structure is reflect by classes:

- `AndyDune\HtmlTable\Table` - a root of the structure
- `AndyDune\HtmlTable\Element\Row` - it implements a table row
- `AndyDune\HtmlTable\Element\Head` - it implements a special table row (head). It can be only one.
- `AndyDune\HtmlTable\Element\Cell` - it implements a table cell. It is a part of `Row` (`Head`)

- `AndyDune\HtmlTable\Builder` - the root class for building html code for table.
It receives `Table` instance as a construct parameter.

- There are many assistive classes for building table, but you don't need to know about their.

Describe table
-------

Table may have attributes, data rows, head row. Rows and cells may have attributes too.

### Table, row, cell class attribute

Use method `addClass` to inject class into table element. Element may have many classes:

```php
use AndyDune\HtmlTable\Table;

// Set classes *useful* and *one* to table.
$table = new Table();
$table->addClass('useful')->addClass('one');
$table->getClasses(); ['useful', 'one]
//

// Set class *active* to row.
$row = $table->row();
$row->addClass('active');
//

// Set class *left* to cell.
$cell = $row->cell();
$row->addClass('left');
//
```

### Table, row, cell id attribute

Use method `setId` to inject id into table element. Element may have only one id:

```php
use AndyDune\HtmlTable\Table;

//
$table = new Table();
$table->setId('top');
$table->getId(); // top

//
$row = $table->row();
$row->setId('active');
$row->getId(); // active

//
$cell = $row->cell();
$row->setId('left');
$row->getId(); //left
```