1 | <?php |
||
4 | Class TableColumn |
||
5 | { |
||
6 | public $value; |
||
7 | |||
8 | private $tableInstance; |
||
9 | private $title; |
||
10 | private $attrs; |
||
11 | private $css; |
||
12 | private $filter = false; |
||
13 | private $filterData = null; |
||
14 | |||
15 | /** |
||
16 | * create a new TableColumn instance |
||
17 | * |
||
18 | * @param Table $tableInstance |
||
19 | */ |
||
20 | public function __construct(Table &$tableInstance) |
||
26 | |||
27 | public function __get( $prop ){ |
||
30 | |||
31 | /** |
||
32 | * set column title |
||
33 | * |
||
34 | * @param $title |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function title($title) |
||
38 | { |
||
39 | $this->title = $title; |
||
40 | return $this; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * bind colunm value. $value can be: |
||
45 | * integer index for none associative array or json |
||
46 | * string index for associative array, json, PDO or ORM result |
||
47 | * a closure that returns a string |
||
48 | * |
||
49 | * @param $value |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function value($value) |
||
53 | { |
||
54 | $this->value = $value; |
||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * add a attribute to table <td> or <th> |
||
60 | * |
||
61 | * @param $attr |
||
62 | * @param $value |
||
63 | * @param bool $header |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function attr($attr, $value, $header = false) |
||
71 | |||
72 | /** |
||
73 | * add css to table <td> or <th> |
||
74 | * |
||
75 | * @param $attr |
||
76 | * @param $value |
||
77 | * @param bool $header |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function css($attr, $value, $header = false) |
||
85 | |||
86 | /** |
||
87 | * add a filter to this column. |
||
88 | * $data can be array (associative or not), json, PDO or ORM result |
||
89 | * |
||
90 | * @param null $data |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function filter($data = null) |
||
94 | { |
||
95 | $this->filterData = $this->isJson($data) ? json_decode($data) : $data; |
||
96 | $this->filter = true; |
||
97 | $this->tableInstance->hasFilters = true; |
||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * add this column to the table |
||
103 | * |
||
104 | * @return Table |
||
105 | */ |
||
106 | public function add() |
||
110 | |||
111 | /** |
||
112 | * render column header cell <th> |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | public function renderHeader() |
||
128 | |||
129 | /** |
||
130 | * render column filter |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function renderFilter() |
||
155 | |||
156 | /** |
||
157 | * render column body cell <td> |
||
158 | * |
||
159 | * @param $row |
||
160 | * @return mixed |
||
161 | */ |
||
162 | public function renderBody( &$row ) |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Check if string if json |
||
185 | * |
||
186 | * @param $string |
||
187 | * @return bool |
||
188 | */ |
||
189 | private function isJson($string) |
||
195 | |||
196 | |||
197 | /** |
||
198 | * @param string $str |
||
199 | * @return mixed |
||
200 | */ |
||
201 | private function camelToTitle($str) |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @param string $str |
||
212 | * @return string |
||
213 | */ |
||
214 | private function underscoreToTitle($str) |
||
220 | |||
221 | |||
222 | } |
||
223 |