Skip to content

Commit

Permalink
Merge pull request #5 from jupitern/development
Browse files Browse the repository at this point in the history
Added support for row attributes and callable cell attributes
  • Loading branch information
jupitern authored Jun 19, 2019
2 parents c395c82 + 253d373 commit 29547d6
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 95 deletions.
50 changes: 29 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ soon...

## Requirements

PHP 5.4 or higher.
PHP 5.6 or higher.

## Installation

Expand Down Expand Up @@ -49,12 +49,20 @@ Include jupitern/table in your project, by adding it to your composer.json file.
->setData($data)

// add attributes to the <table> html tag one by one
->attr('id', 'demoTable')
->attr('class', 'table table-bordered table-striped table-hover')
->attr('cellspacing', '0')
->attr('table', 'id', 'demoTable')
->attr('table', 'class', 'table table-bordered table-striped table-hover')
->attr('table', 'cellspacing', '0')

// or add all <table> attributes at once
->attrs(['class' => 'table table-bordered', 'cellspacing' => '0']);
->attrs('table', ['class' => 'table table-bordered', 'cellspacing' => '0'])

// add attributes to the table rows
->css('tr', 'background-color', 'red')

// add attributes to the table rows using a callable
->attr('tr', 'data-id', function($row) {
return 'row-' . $row['id'];
})

// add a new column for array data
->column()
Expand Down Expand Up @@ -112,10 +120,10 @@ Include jupitern/table in your project, by adding it to your composer.json file.
->column()
->title('Name')
->value('name')
->attr('data-val', 'foo', true) // add attributes to <th>
->css('background-color', '#f5f5f5', true) // add css to <th>
->attr('data-val', 'bar') // add attributes to <td>
->css('background-color', '#f5f5f5') // add css to <td>
->attr('th', 'data-val', 'foo') // add attributes to <th>
->css('th', 'background-color', '#f5f5f5') // add css to <th>
->attr('td', 'data-val', 'bar') // add attributes to <td>
->css('td', 'background-color', '#f5f5f5') // add css to <td>
->add()

// echo table output
Expand All @@ -140,38 +148,38 @@ $filterData = $db->query("SELECT name as val, name FROM persons limit 10")->fetc

\Jupitern\Table\Table::instance()
->setData($data)
->attr('id', 'demoTable')
->attr('class', 'table table-bordered table-striped table-hover')
->attr('cellspacing', '0')
->attr('width', '100%')
->attr('table', 'id', 'demoTable')
->attr('table', 'class', 'table table-bordered table-striped table-hover')
->attr('table', 'cellspacing', '0')
->attr('table', 'width', '100%')
->column()
->title('Name')
->value(function ($row) {
return rand(1,10)%2 ? '<b>'.$row->name.'</b>' : $row->name;
})
->filter($filterData)
->css('color', 'green')
->css('width', '50%')
->css('background-color', '#ccc', true)
->css('td', 'color', 'green')
->css('td', 'width', '50%')
->css('td', 'background-color', '#ccc', true)
->add()
->column()
->title('Age')
->value('age')
->filter()
->css('color', 'red')
->css('width', '20%')
->css('td', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column('Phone')
->filter()
->value('phone')
->css('color', 'red')
->css('width', '20%')
->css('td', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column()
->value(function ($row) {
return '<a href="country/'.$row->id.'">edit</a>';
})
->css('width', '10%')
->css('td', 'width', '10%')
->add()
->render();
?>
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"issues": "https://github.com/jupitern/table/issues"
},
"require" :{
"php":">=5.4"
"php": ">=5.6",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
31 changes: 19 additions & 12 deletions src/Examples/test1.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,42 @@
['id' => 4, 'name' => 'Clark', 'age' => '34', 'phone' => '169 574 741'],
['id' => 5, 'name' => 'Alex', 'age' => '65', 'phone' => '732 753 467'],
])
->attr('id', 'demoTable')
->attr('class', 'table table-bordered table-striped table-hover')
->attr('cellspacing', '0')
->attr('width', '100%')
// ->attrs('table', ['class' => 'table table-bordered', 'cellspacing' => '0'])
->attr('table', 'id', 'demoTable')
->attr('table', 'class', 'table table-bordered table-striped table-hover')
->attr('table', 'cellspacing', '0')
->attr('table', 'width', '100%')
->attr('tr', 'data-text', 'bla bla bla bla bla')
->attr('tr', 'data-id', function($row) {
return 'row-' . $row['id'];
})
->css('tr', 'background-color', 'red')
->column()
->title('Name')
->value(function ($row) {
return rand(1,10)%2 ? '<b>'.$row['name'].'</b>' : $row['name'];
})
->css('color', 'green')
->css('width', '50%')
->css('background-color', '#ccc', true)
->attr('td', 'data-text', 'bla bla bla')
->css('th', 'color', 'green')
->css('td', 'width', '50%')
->css('td', 'background-color', '#ccc', true)
->add()
->column()
->title('Age')
->value('age')
->css('color', 'red')
->css('width', '20%')
->css('th', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column('Phone')
->value('phone')
->css('color', 'red')
->css('width', '20%')
->css('td', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column()
->value(function ($row) {
return '<a href="country/'. $row['id'] .'">edit</a>';
})
->css('width', '10%')
->css('td', 'width', '10%')
->add();
?>

Expand Down
24 changes: 12 additions & 12 deletions src/Examples/test2.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,38 @@

\Jupitern\Table\Table::instance()
->setData($data)
->attr('id', 'demoTable')
->attr('class', 'table table-bordered table-striped table-hover')
->attr('cellspacing', '0')
->attr('width', '100%')
->attr('table', 'id', 'demoTable')
->attr('table', 'class', 'table table-bordered table-striped table-hover')
->attr('table', 'cellspacing', '0')
->attr('table', 'width', '100%')
->column()
->title('Name')
->value(function ($row) {
return rand(1,10)%2 ? '<b>'.$row['name'].'</b>' : $row['name'];
})
->filter($filterData)
->css('color', 'green')
->css('width', '50%')
->css('background-color', '#ccc', true)
->css('td', 'color', 'green')
->css('td', 'width', '50%')
->css('td', 'background-color', '#ccc', true)
->add()
->column()
->title('Age')
->value('age')
->filter()
->css('color', 'red')
->css('width', '20%')
->css('td', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column('Phone')
->filter()
->value('phone')
->css('color', 'red')
->css('width', '20%')
->css('td', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column()
->value(function ($row) {
return '<a href="country/'. $row['id'] .'">edit</a>';
})
->css('width', '10%')
->css('td', 'width', '10%')
->add()
?>

Expand Down
32 changes: 27 additions & 5 deletions src/Table/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,49 @@ class Properties
{
private $properties;

public function add($property, $value)
/**
* @param $property
* @param $value
* @return $this
*/
public function add($property, $value)
{
$this->properties[$property] = $value;

return $this;
}

public function addAll($properties)
/**
* @param $properties
* @return $this
*/
public function addAll($properties)
{
if (is_array($properties)) {
$this->properties = array_merge((array)$this->properties, $properties);
}

return $this;
}

public function render($template)
/**
* @param $elem
* @param $template
* @return string
*/
public function render($template, $context = null)
{
$output = '';
foreach ((array)$this->properties as $prop => $val) {
foreach ((array)$this->properties as $prop => $value) {
if (is_callable($value)) {
$val = $value($context);
} else {
$val = $value;
}
$output .= str_replace(['{prop}', '{val}'], [$prop, $val], $template);
}

return $output;
}

}
}
Loading

0 comments on commit 29547d6

Please sign in to comment.