Skip to content

Commit 29547d6

Browse files
authored
Merge pull request #5 from jupitern/development
Added support for row attributes and callable cell attributes
2 parents c395c82 + 253d373 commit 29547d6

File tree

7 files changed

+155
-95
lines changed

7 files changed

+155
-95
lines changed

README.md

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ soon...
1717

1818
## Requirements
1919

20-
PHP 5.4 or higher.
20+
PHP 5.6 or higher.
2121

2222
## Installation
2323

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

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

5656
// or add all <table> attributes at once
57-
->attrs(['class' => 'table table-bordered', 'cellspacing' => '0']);
57+
->attrs('table', ['class' => 'table table-bordered', 'cellspacing' => '0'])
58+
59+
// add attributes to the table rows
60+
->css('tr', 'background-color', 'red')
61+
62+
// add attributes to the table rows using a callable
63+
->attr('tr', 'data-id', function($row) {
64+
return 'row-' . $row['id'];
65+
})
5866

5967
// add a new column for array data
6068
->column()
@@ -112,10 +120,10 @@ Include jupitern/table in your project, by adding it to your composer.json file.
112120
->column()
113121
->title('Name')
114122
->value('name')
115-
->attr('data-val', 'foo', true) // add attributes to <th>
116-
->css('background-color', '#f5f5f5', true) // add css to <th>
117-
->attr('data-val', 'bar') // add attributes to <td>
118-
->css('background-color', '#f5f5f5') // add css to <td>
123+
->attr('th', 'data-val', 'foo') // add attributes to <th>
124+
->css('th', 'background-color', '#f5f5f5') // add css to <th>
125+
->attr('td', 'data-val', 'bar') // add attributes to <td>
126+
->css('td', 'background-color', '#f5f5f5') // add css to <td>
119127
->add()
120128

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

141149
\Jupitern\Table\Table::instance()
142150
->setData($data)
143-
->attr('id', 'demoTable')
144-
->attr('class', 'table table-bordered table-striped table-hover')
145-
->attr('cellspacing', '0')
146-
->attr('width', '100%')
151+
->attr('table', 'id', 'demoTable')
152+
->attr('table', 'class', 'table table-bordered table-striped table-hover')
153+
->attr('table', 'cellspacing', '0')
154+
->attr('table', 'width', '100%')
147155
->column()
148156
->title('Name')
149157
->value(function ($row) {
150158
return rand(1,10)%2 ? '<b>'.$row->name.'</b>' : $row->name;
151159
})
152160
->filter($filterData)
153-
->css('color', 'green')
154-
->css('width', '50%')
155-
->css('background-color', '#ccc', true)
161+
->css('td', 'color', 'green')
162+
->css('td', 'width', '50%')
163+
->css('td', 'background-color', '#ccc', true)
156164
->add()
157165
->column()
158166
->title('Age')
159167
->value('age')
160168
->filter()
161-
->css('color', 'red')
162-
->css('width', '20%')
169+
->css('td', 'color', 'red')
170+
->css('td', 'width', '20%')
163171
->add()
164172
->column('Phone')
165173
->filter()
166174
->value('phone')
167-
->css('color', 'red')
168-
->css('width', '20%')
175+
->css('td', 'color', 'red')
176+
->css('td', 'width', '20%')
169177
->add()
170178
->column()
171179
->value(function ($row) {
172180
return '<a href="country/'.$row->id.'">edit</a>';
173181
})
174-
->css('width', '10%')
182+
->css('td', 'width', '10%')
175183
->add()
176184
->render();
177185
?>

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"issues": "https://github.com/jupitern/table/issues"
1717
},
1818
"require" :{
19-
"php":">=5.4"
19+
"php": ">=5.6",
20+
"ext-json": "*"
2021
},
2122
"autoload": {
2223
"psr-4": {

src/Examples/test1.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,42 @@
1010
['id' => 4, 'name' => 'Clark', 'age' => '34', 'phone' => '169 574 741'],
1111
['id' => 5, 'name' => 'Alex', 'age' => '65', 'phone' => '732 753 467'],
1212
])
13-
->attr('id', 'demoTable')
14-
->attr('class', 'table table-bordered table-striped table-hover')
15-
->attr('cellspacing', '0')
16-
->attr('width', '100%')
13+
// ->attrs('table', ['class' => 'table table-bordered', 'cellspacing' => '0'])
14+
->attr('table', 'id', 'demoTable')
15+
->attr('table', 'class', 'table table-bordered table-striped table-hover')
16+
->attr('table', 'cellspacing', '0')
17+
->attr('table', 'width', '100%')
18+
->attr('tr', 'data-text', 'bla bla bla bla bla')
19+
->attr('tr', 'data-id', function($row) {
20+
return 'row-' . $row['id'];
21+
})
22+
->css('tr', 'background-color', 'red')
1723
->column()
1824
->title('Name')
1925
->value(function ($row) {
2026
return rand(1,10)%2 ? '<b>'.$row['name'].'</b>' : $row['name'];
2127
})
22-
->css('color', 'green')
23-
->css('width', '50%')
24-
->css('background-color', '#ccc', true)
28+
->attr('td', 'data-text', 'bla bla bla')
29+
->css('th', 'color', 'green')
30+
->css('td', 'width', '50%')
31+
->css('td', 'background-color', '#ccc', true)
2532
->add()
2633
->column()
2734
->title('Age')
2835
->value('age')
29-
->css('color', 'red')
30-
->css('width', '20%')
36+
->css('th', 'color', 'red')
37+
->css('td', 'width', '20%')
3138
->add()
3239
->column('Phone')
3340
->value('phone')
34-
->css('color', 'red')
35-
->css('width', '20%')
41+
->css('td', 'color', 'red')
42+
->css('td', 'width', '20%')
3643
->add()
3744
->column()
3845
->value(function ($row) {
3946
return '<a href="country/'. $row['id'] .'">edit</a>';
4047
})
41-
->css('width', '10%')
48+
->css('td', 'width', '10%')
4249
->add();
4350
?>
4451

src/Examples/test2.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@
1313

1414
\Jupitern\Table\Table::instance()
1515
->setData($data)
16-
->attr('id', 'demoTable')
17-
->attr('class', 'table table-bordered table-striped table-hover')
18-
->attr('cellspacing', '0')
19-
->attr('width', '100%')
16+
->attr('table', 'id', 'demoTable')
17+
->attr('table', 'class', 'table table-bordered table-striped table-hover')
18+
->attr('table', 'cellspacing', '0')
19+
->attr('table', 'width', '100%')
2020
->column()
2121
->title('Name')
2222
->value(function ($row) {
2323
return rand(1,10)%2 ? '<b>'.$row['name'].'</b>' : $row['name'];
2424
})
2525
->filter($filterData)
26-
->css('color', 'green')
27-
->css('width', '50%')
28-
->css('background-color', '#ccc', true)
26+
->css('td', 'color', 'green')
27+
->css('td', 'width', '50%')
28+
->css('td', 'background-color', '#ccc', true)
2929
->add()
3030
->column()
3131
->title('Age')
3232
->value('age')
3333
->filter()
34-
->css('color', 'red')
35-
->css('width', '20%')
34+
->css('td', 'color', 'red')
35+
->css('td', 'width', '20%')
3636
->add()
3737
->column('Phone')
3838
->filter()
3939
->value('phone')
40-
->css('color', 'red')
41-
->css('width', '20%')
40+
->css('td', 'color', 'red')
41+
->css('td', 'width', '20%')
4242
->add()
4343
->column()
4444
->value(function ($row) {
4545
return '<a href="country/'. $row['id'] .'">edit</a>';
4646
})
47-
->css('width', '10%')
47+
->css('td', 'width', '10%')
4848
->add()
4949
?>
5050

src/Table/Properties.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,49 @@ class Properties
55
{
66
private $properties;
77

8-
public function add($property, $value)
8+
/**
9+
* @param $property
10+
* @param $value
11+
* @return $this
12+
*/
13+
public function add($property, $value)
914
{
1015
$this->properties[$property] = $value;
16+
1117
return $this;
1218
}
1319

14-
public function addAll($properties)
20+
/**
21+
* @param $properties
22+
* @return $this
23+
*/
24+
public function addAll($properties)
1525
{
1626
if (is_array($properties)) {
1727
$this->properties = array_merge((array)$this->properties, $properties);
1828
}
29+
1930
return $this;
2031
}
2132

22-
public function render($template)
33+
/**
34+
* @param $elem
35+
* @param $template
36+
* @return string
37+
*/
38+
public function render($template, $context = null)
2339
{
2440
$output = '';
25-
foreach ((array)$this->properties as $prop => $val) {
41+
foreach ((array)$this->properties as $prop => $value) {
42+
if (is_callable($value)) {
43+
$val = $value($context);
44+
} else {
45+
$val = $value;
46+
}
2647
$output .= str_replace(['{prop}', '{val}'], [$prop, $val], $template);
2748
}
49+
2850
return $output;
2951
}
3052

31-
}
53+
}

0 commit comments

Comments
 (0)