1+ <?php
2+
3+ namespace App \Library ;
4+
5+ use Illuminate \Support \Str ;
6+
7+ class Datatable
8+ {
9+ protected $ headers = [];
10+
11+ /**
12+ | To make the headers more easier and accommodating to several styles, I have made them accept
13+ | uni and multi-dimensional arrays (to level 1). In cases where a value is hidden inside
14+ | a relationship e.g. props.item.patient.full_name, you can specify the 'value' as 'patient.full_name'
15+ | and the search will just work!
16+ |
17+ | Here is a sample header member
18+ |
19+ | protected $headers = [
20+ | ['text' => "patient Name", 'value' => 'patient.full_name'],
21+ | ['text' => "Admission Type", 'sortable' => false, 'value' => 'admission type'],
22+ | "Authorized Amount", "Date", "actions"
23+ | ];
24+ |
25+ | or even
26+ | 'name', ['text' => 'amount', 'align' => 'right'], 'type', ['text' => 'used?', 'value' => 'used'], 'actions'
27+ |
28+ | Available Header attributes - From Vuetify v2
29+ | {
30+ | text: string
31+ | value: string
32+ | align?: 'start' | 'center' | 'end'
33+ | sortable?: boolean
34+ | filterable?: boolean
35+ | groupable?: boolean
36+ | divider?: boolean
37+ | class?: string | string[]
38+ | width?: string | number
39+ | filter?: (value: any, search: string, item: any) => boolean
40+ | sort?: (a: any, b: any) => number
41+ | }
42+ |
43+ */
44+
45+ /**
46+ * @param string|array $headers
47+ *
48+ * @return array
49+ */
50+
51+ /**
52+ * Prepare the headers. The headers are very friendly to set, and here at the top is sample code
53+ *
54+ * @param array $headers
55+ *
56+ * @return array
57+ */
58+ public function headers (array $ headers ) : array
59+ {
60+ foreach ($ headers as $ header )
61+ {
62+ $ arr = [
63+ 'text ' => $ this ->getText ($ header ),
64+ 'sortable ' => $ this ->getSortable ($ header ),
65+ 'value ' => $ this ->getValue ($ header ),
66+ 'align ' => $ this ->getAlignment ($ header ),
67+ ];
68+
69+ isset ($ header ['filterable ' ]) ? $ arr = array_merge ($ arr , ['filterable ' => $ this ->getBoolean ($ header , 'filterable ' )]) : null ;
70+ isset ($ header ['sortable ' ]) ? $ arr = array_merge ($ arr , ['sortable ' => $ this ->getBoolean ($ header , 'sortable ' )]) : null ;
71+ isset ($ header ['groupable ' ]) ? $ arr = array_merge ($ arr , ['groupable ' => $ this ->getBoolean ($ header , 'groupable ' )]) : null ;
72+ isset ($ header ['divider ' ]) ? $ arr = array_merge ($ arr , ['divider ' => $ this ->getBoolean ($ header , 'divider ' )]) : null ;
73+
74+ $ this ->headers [] = $ arr ;
75+ }
76+
77+ return $ this ->headers ;
78+ }
79+
80+ /**
81+ * get the text for the datatable. This is the display
82+ *
83+ * @param string|array $header
84+ *
85+ * @return string
86+ */
87+ private function getText ($ header )
88+ {
89+ $ value = $ header ;
90+
91+ if (is_array ($ header ) and isset ($ header ['text ' ])) {
92+ $ value = $ header ['text ' ];
93+ }
94+
95+ return is_array ($ value ) ? ucwords ($ value [1 ]) : ucwords ($ value );
96+ }
97+
98+ /**
99+ * get the value. This value is used in searching the vuetify datatable when no ajax search is required
100+ *
101+ * @param string|array $header
102+ *
103+ * @return string
104+ */
105+ private function getValue ($ header )
106+ {
107+ $ value = $ header ;
108+
109+ if (is_array ($ header ) and isset ($ header ['value ' ])) {
110+ $ value = $ header ['value ' ];
111+ }
112+
113+ return is_array ($ value ) ? Str::snake (strtolower (collect ($ value )->first ())) : Str::snake (strtolower ($ value ));
114+ }
115+
116+ /**
117+ * get the boolean value.
118+ *
119+ * @param string|array $header
120+ *
121+ * @return bool
122+ */
123+ private function getBoolean ($ header , $ entity )
124+ {
125+ $ bool = $ header ;
126+
127+ if (is_array ($ header ) and isset ($ header [$ entity ])) {
128+ $ bool = $ header [$ entity ];
129+ }
130+
131+ return is_array ($ bool ) ? (bool )(strtolower (collect ($ bool )->first ())) : (bool )(strtolower ($ bool ));
132+ }
133+
134+ /**
135+ * get the sortable attribute
136+ *
137+ * @param string|array $header
138+ *
139+ * @return bool|mixed
140+ */
141+ private function getSortable ($ header )
142+ {
143+ $ value = true ;
144+
145+ if (is_array ($ header ) and isset ($ header ['sortable ' ])) {
146+
147+ $ value = $ header ['sortable ' ];
148+
149+ } else if (! is_array ($ header ) and str_contains ($ header , 'action ' )) {
150+ // check if string is action
151+ $ value = false ;
152+ }
153+
154+ return $ value ;
155+ }
156+
157+ /**
158+ * Get allignment attr
159+ *
160+ * @param string|array $header
161+ *
162+ * @return mixed|string
163+ */
164+ private function getAlignment ($ header )
165+ {
166+ $ value = 'left ' ;
167+
168+ if (is_array ($ header ) and isset ($ header ['align ' ])) {
169+ $ value = $ header ['align ' ];
170+ }
171+
172+ return $ value ;
173+ }
174+
175+ /**
176+ * function to get the value for a certain index.
177+ *
178+ * @param string|array $header
179+ * @param int $index
180+ *
181+ * @return mixed
182+ */
183+ private function getValueFromIndex ($ header , $ index = 0 )
184+ {
185+ return isset (array_keys ($ header )[$ index ]) ? $ header [array_keys ($ header )[$ index ]] : $ header [array_keys ($ header )[0 ]];
186+ }
187+ }
0 commit comments