@@ -30,6 +30,8 @@ abstract class AbstractDataTable
30
30
*/
31
31
private $ columns = [];
32
32
33
+ private $ actions = [];
34
+
33
35
/**
34
36
* AbstractDataTable constructor.
35
37
*
@@ -79,6 +81,43 @@ public function addColumn(array $spec)
79
81
$ this ->columns [] = new Column ($ jsName , $ dbName , $ options );
80
82
}
81
83
84
+ public function addAction (array $ spec )
85
+ {
86
+ if (! isset ($ spec ['url ' ]) || ! isset ($ spec ['label ' ])) {
87
+ throw new \Exception ('Missing required options ' );
88
+ }
89
+
90
+ $ this ->actions [] = $ spec ;
91
+ }
92
+
93
+ protected function injectActions (array &$ items )
94
+ {
95
+ foreach ($ this ->actions as &$ action ) {
96
+ foreach ($ items as &$ item ) {
97
+ $ find = [];
98
+ preg_match_all ("/&[a-z1-9]*&/ " , $ action ['url ' ], $ find );
99
+
100
+ foreach ($ find [0 ] as $ actionParamName ) {
101
+ $ clearParamName = trim ($ actionParamName , "& " );
102
+ $ paramExist = false ;
103
+ foreach ($ this ->columns as $ column ) {
104
+ if ($ clearParamName == $ column ->getJsName ()) {
105
+ $ paramValue = $ item [$ clearParamName ];
106
+ $ action ['url ' ] = str_replace ($ actionParamName , $ paramValue , $ action ['url ' ]);
107
+ $ paramExist = true ;
108
+ }
109
+ }
110
+
111
+ if (! $ paramExist ) {
112
+ throw new \Exception ("Url param doesnt exists " );
113
+ }
114
+ }
115
+
116
+ $ item ['actions ' ][] = $ action ;
117
+ }
118
+ }
119
+ }
120
+
82
121
/**
83
122
* Get column
84
123
*
@@ -124,6 +163,8 @@ public function getData(Request $request = null): array
124
163
}, $ this ->getColumns ())
125
164
);
126
165
166
+ $ this ->injectActions ($ items );
167
+
127
168
// prepare output
128
169
// $data = array_map(function (array $row) {
129
170
// $row['DTRowId'] = $row['id'];
0 commit comments