From c273a04cfa8255ba7423c2834b9dc75061cf8b81 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Fri, 6 Sep 2024 16:17:43 +0000 Subject: [PATCH] Dev: Initial work on ColumnSearch support. Probably not the final structure, but an initial pass. --- Editor.php | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/Editor.php b/Editor.php index 7898e78..357c5d3 100644 --- a/Editor.php +++ b/Editor.php @@ -20,6 +20,7 @@ use DataTables\Database\Query; use DataTables\Editor\Field; use DataTables\Editor\Join; +use DataTables\Editor\ColumnSearch; /** * DataTables Editor base class for creating editable tables. @@ -1049,6 +1050,8 @@ private function _process($data) } } + $this->_columnSearch(); + if ($this->_transaction) { $this->_db->commit(); } @@ -1072,6 +1075,38 @@ private function _process($data) } } + /** + * Get information for display in the client-side search fields + */ + private function _columnSearch() + { + if (isset($this->_processData['columnSearch'])) { + $columnSearch = $this->_processData['columnSearch']; + + foreach ($columnSearch as $colIdx => $search) { + if ($search === '') { + continue; + } + + if ($search['type'] === 'select' && $search['fetch'] === 'true') { + // Get a list of options for the select list + + $dataSrc = $_POST['columns'][$colIdx]['data']; + $dbSrc = $this->_ssp_field($this->_processData, $colIdx); + $opts = $this->db() + ->selectDistinct('candidates_view', $dataSrc, null, [$dbSrc . ' asc']) + ->fetchAll(); + + if (! isset($this->_out['columnSearch'])) { + $this->_out['columnSearch'] = []; + } + + $this->_out['columnSearch'][$colIdx] = array_column($opts, $dbSrc); + } + } + } + } + /** * Get an array of objects from the database to be given to DataTables as a * result of an sAjaxSource request, such that DataTables can display the information @@ -2025,7 +2060,14 @@ private function _ssp_filter($query, $http) $search = $column['search']['value']; if ($search !== '' && $column['searchable'] == 'true') { - $query->where($this->_ssp_field($http, $i), '%' . $search . '%', 'like'); + $fieldName = $this->_ssp_field($http, $i); + $field = $this->_find_field($fieldName, 'name'); + + $handled = ColumnSearch::process($query, $field, $i, $search, $http); + + if (! $handled) { + $query->where($fieldName, '%' . $search . '%', 'like'); + } } } }