Skip to content

Commit d336314

Browse files
committed
Added orthogonal sort and filter.
Signed-off-by: Dmitry Mazurov <[email protected]>
1 parent b0b9e2c commit d336314

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

src/CollectionDataTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function columnSearch()
100100
{
101101
$columns = $this->request->get('columns', []);
102102
for ($i = 0, $c = count($columns); $i < $c; $i++) {
103-
$column = $this->getColumnName($i);
103+
$column = $this->getColumnName($i, 'filter');
104104

105105
if (! $this->request->isColumnSearchable($i) || $this->isBlacklisted($column)) {
106106
continue;
@@ -230,7 +230,7 @@ protected function globalSearch($keyword)
230230

231231
$data = $this->serialize($row);
232232
foreach ($this->request->searchableColumnIndex() as $index) {
233-
$column = $this->getColumnName($index);
233+
$column = $this->getColumnName($index, 'sort');
234234
$value = Arr::get($data, $column);
235235
if (! $value || is_array($value)) {
236236
if (! is_numeric($value)) {
@@ -285,7 +285,7 @@ protected function getSorter(array $criteria)
285285
{
286286
$sorter = function ($a, $b) use ($criteria) {
287287
foreach ($criteria as $orderable) {
288-
$column = $this->getColumnName($orderable['column']);
288+
$column = $this->getColumnName($orderable['column'], 'sort');
289289
$direction = $orderable['direction'];
290290
if ($direction === 'desc') {
291291
$first = $b;

src/DataTableAbstract.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,12 +882,13 @@ protected function setupKeyword($value)
882882
* Get column name to be use for filtering and sorting.
883883
*
884884
* @param int $index
885+
* @param string $type
885886
* @param bool $wantsAlias
886887
* @return string
887888
*/
888-
protected function getColumnName($index, $wantsAlias = false)
889+
protected function getColumnName($index, $type, $wantsAlias = false)
889890
{
890-
$column = $this->request->columnName($index);
891+
$column = $this->request->columnName($index, $type);
891892

892893
// DataTables is using make(false)
893894
if (is_numeric($column)) {

src/QueryDataTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function columnSearch()
280280
$columns = $this->request->columns();
281281

282282
foreach ($columns as $index => $column) {
283-
$column = $this->getColumnName($index);
283+
$column = $this->getColumnName($index, 'filter');
284284

285285
if (! $this->request->isColumnSearchable($index) || $this->isBlacklisted($column) && ! $this->hasFilterColumn($column)) {
286286
continue;
@@ -635,7 +635,7 @@ protected function defaultOrdering()
635635
{
636636
collect($this->request->orderableColumns())
637637
->map(function ($orderable) {
638-
$orderable['name'] = $this->getColumnName($orderable['column'], true);
638+
$orderable['name'] = $this->getColumnName($orderable['column'], 'sort', true);
639639

640640
return $orderable;
641641
})
@@ -713,7 +713,7 @@ protected function globalSearch($keyword)
713713
$this->query->where(function ($query) use ($keyword) {
714714
collect($this->request->searchableColumnIndex())
715715
->map(function ($index) {
716-
return $this->getColumnName($index);
716+
return $this->getColumnName($index, 'filter');
717717
})
718718
->reject(function ($column) {
719719
return $this->isBlacklisted($column) && ! $this->hasFilterColumn($column);

src/Utilities/Request.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,29 @@ public function keyword()
213213
* Get column identity from input or database.
214214
*
215215
* @param int $i
216+
* @param string $type
216217
* @return string
217218
*/
218-
public function columnName($i)
219+
public function columnName($i, $type)
219220
{
220221
$column = $this->request->input("columns.$i");
221222

223+
if (isset($column['data']) && is_array($column['data'])) {
224+
if (isset($column['data'][$type]) && $column['data'][$type] != '') {
225+
return $column['data'][$type];
226+
}
227+
228+
if (isset($column['data']['display']) && $column['data']['display'] != '') {
229+
return $column['data']['display'];
230+
}
231+
232+
if (isset($column['data']['_']) && $column['data']['_'] != '') {
233+
return $column['data']['_'];
234+
}
235+
236+
return $column['name'];
237+
}
238+
222239
return isset($column['name']) && $column['name'] != '' ? $column['name'] : $column['data'];
223240
}
224241

tests/Unit/RequestTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ public function test_searchable_column_index()
112112
$this->assertEquals('bar', $request->columnName(1));
113113
}
114114

115+
public function test_searchable_with_filter_column_index()
116+
{
117+
$_GET['columns'] = [];
118+
$_GET['columns'][] = ['name' => 'foo', 'data' => ['_' => 'foo', 'filter' => 'foo.sort'], 'searchable' => 'true', 'search' => ['value' => 'foo']];
119+
$_GET['columns'][] = ['name' => 'bar', 'data' => ['_' => 'bar', 'filter' => 'bar.sort'], 'searchable' => 'false', 'search' => ['value' => 'foo']];
120+
request()->merge($_GET);
121+
$request = $this->getRequest();
122+
$this->assertEquals([0], $request->searchableColumnIndex());
123+
124+
$this->assertTrue($request->isColumnSearchable(0, false));
125+
$this->assertFalse($request->isColumnSearchable(1, false));
126+
127+
$this->assertTrue($request->isColumnSearchable(0, true));
128+
$this->assertFalse($request->isColumnSearchable(1, false));
129+
130+
$this->assertEquals('foo.sort', $request->columnName(0, 'filter'));
131+
$this->assertEquals('bar.sort', $request->columnName(1, 'filter'));
132+
}
133+
115134
public function test_keyword()
116135
{
117136
$_GET['search'] = [];

0 commit comments

Comments
 (0)