Skip to content

Commit 0ead0a8

Browse files
author
halytskyi
authored
Actions added
1 parent 7c6a2a5 commit 0ead0a8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/AbstractDataTable.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abstract class AbstractDataTable
3030
*/
3131
private $columns = [];
3232

33+
private $actions = [];
34+
3335
/**
3436
* AbstractDataTable constructor.
3537
*
@@ -79,6 +81,43 @@ public function addColumn(array $spec)
7981
$this->columns[] = new Column($jsName, $dbName, $options);
8082
}
8183

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+
82121
/**
83122
* Get column
84123
*
@@ -124,6 +163,8 @@ public function getData(Request $request = null): array
124163
}, $this->getColumns())
125164
);
126165

166+
$this->injectActions($items);
167+
127168
// prepare output
128169
// $data = array_map(function (array $row) {
129170
// $row['DTRowId'] = $row['id'];

0 commit comments

Comments
 (0)