Skip to content

Commit

Permalink
fix Grid _q GET key /wo component name
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 8, 2022
1 parent c45a786 commit 368a126
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
7 changes: 5 additions & 2 deletions js/src/plugins/js-search.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ export default class JsSearch extends atkPlugin {
* @param options
*/
doSearch(uri, query, options, cb = function () {}) {
const queryKey = this.settings.uri_query_key;

if (query) {
options = $.extend(options, { _q: query });
options = $.extend(options, { [queryKey]: query });
}

if (this.settings.useAjax) {
Expand All @@ -207,7 +209,7 @@ export default class JsSearch extends atkPlugin {
onComplete: cb,
});
} else {
uri = $.atkRemoveParam(uri, '_q');
uri = $.atkRemoveParam(uri, queryKey);
if (options.__atk_reload) {
delete options.__atk_reload;
}
Expand All @@ -220,6 +222,7 @@ export default class JsSearch extends atkPlugin {
JsSearch.DEFAULTS = {
uri: null,
uri_options: {},
uri_query_key: null,
q: null,
autoQuery: false,
timeOut: 300,
Expand Down
2 changes: 1 addition & 1 deletion public/atkjs-ui.min.js

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,21 @@ protected function init(): void
$this->stickyGet($this->paginator->name);
}

$this->stickyGet('_q');
// TODO dirty way to set stickyGet - add addQuickSearch to find the expected search input component ID and then remove it
if ($this->menu !== false) {
$appUniqueHashesBackup = $this->getApp()->unique_hashes;
$menuElementNameCountsBackup = \Closure::bind(fn () => $this->_element_name_counts, $this->menu, AbstractView::class)();
try {
$menuRight = $this->menu->addMenuRight(); // @phpstan-ignore-line
$menuItemView = View::addTo($menuRight->addItem()->setElement('div'));
$quickSearch = JsSearch::addTo($menuItemView);
$this->stickyGet($quickSearch->name . '_q');
$this->menu->removeElement($menuRight->short_name);
} finally {
$this->getApp()->unique_hashes = $appUniqueHashesBackup;
\Closure::bind(fn () => $this->_element_name_counts = $menuElementNameCountsBackup, $this->menu, AbstractView::class)();
}
}
}

protected function initTable(): Table
Expand Down Expand Up @@ -314,16 +328,16 @@ public function addQuickSearch($fields = [], $hasAutoQuery = false)
$view = View::addTo($this->menu
->addMenuRight()->addItem()->setElement('div'));

$q = trim($this->stickyGet('_q') ?? '');
$this->quickSearch = JsSearch::addTo($view, ['reload' => $this->container, 'autoQuery' => $hasAutoQuery]);
$q = trim($this->stickyGet($this->quickSearch->name . '_q') ?? '');
if ($q !== '') {
$scope = Model\Scope::createOr();
foreach ($fields as $field) {
$scope->addCondition($field, 'like', '%' . $q . '%');
}
$this->model->addCondition($scope);
}

$this->quickSearch = JsSearch::addTo($view, ['reload' => $this->container, 'autoQuery' => $hasAutoQuery, 'initValue' => $q]);
$this->quickSearch->initValue = $q;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/JsSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ protected function renderView(): void
$this->js(true)->atkJsSearch([
'uri' => $this->reload->jsUrl(),
'uri_options' => array_merge(['__atk_reload' => $this->reload->name], $this->args),
'uri_query_key' => $this->name . '_q',
'autoQuery' => $this->autoQuery,
'q' => $this->initValue,
'useAjax' => $this->useAjax,
Expand Down

0 comments on commit 368a126

Please sign in to comment.