how to implement search field in the page
- kolaloka
- Member | 69
Thank you, the first part is clear. I make a form that ends in
<?php
...
$form->onSuccess[] = [$this, 'klientFormSearchSucceeded'];
return $form
}
?>
And I render it as you suggested. Then I have the response function:
<?php
public function klientFormSearchSucceeded($form, $values)
{
$klient = $this->database->table('people')
->where ('surname',$values);
}
?>
but I do not know how/where to put the “redirect to results”, could you please show me a hint?
- jiri.pudil
- Nette Blogger | 1028
I usually only redirect to some SearchPresenter on form success and then let it do the rest. That's another way you can take:
$form->onSuccess[] = function ($form, $values) {
$this->redirect('Search:', ['query' => $values->query]);
};
class SearchPresenter extends Presenter
{
public function renderDefault($query)
{
$this->template->results = $this->repository->search($query);
}
}
- kolaloka
- Member | 69
Thank you CZechBoY, I kinda got it working by now, but have a problem with rendering. The result renders itself on the top of the whole page.
Here is the code from the HomePresenter:
<?php
...
protected function createComponentKlientSearchForm() {
$form = new Form;
$form->addText('value', 'Hledat:')
->setRequired(TRUE);
$form->addSubmit('send', 'Hledat');
$form->onSuccess[] = [$this, 'klientSearchFormSucceeded'];
return $form;
}
public function klientSearchFormSucceeded($form, $values) {
$tofind = $values->value;
$rows = $this->database->table('nekrolog')->select('surname')->where('surname = ?', $tofind);
foreach ($rows as $row) {
echo $row->surname;
}
}
?>
Here is the latte:
<?php
<div id="content">
<table><tr>
<td><a class="bluewhite" n:href="Homepage:klient">Záznamy</a></td>
<td><a n:href="Homepage:uzivatel">Uživatelé</a></td>
<td><a n:href="Klient:create">Přidat Záznam</a></td>
{* <td>{control klientHide} </td> *}
</tr>
</table>
{control klientSearchForm}
<br>
// here follows the general content
?>
Any idea how to render in the right place? Somewhat lower, e.g. btw the search field and the regular default content?
- kolaloka
- Member | 69
Ok, so I try to render Search on its own page, but I must be doing something wrong. Here is where I got stuck, this is HomePresenter:
<?php
protected function createComponentKlientSearchForm() {
$form = new Form;
$form->addText('search', 'Hledat:')
->setRequired(TRUE);
$form->addSubmit('send', 'Hledat');
$form->onSuccess[] = [$this, 'renderSearch'];
return $form;
}
public function renderSearch($form,$values) {
$tofind = $values->surname;
$this->template->klient = $this->database->table('nekrolog')
->where('surname','$tofind')
->order('surname DESC');
}
?>
And after pressing search, it does not go to the search results page, it does not start rendering the latte, it just stays there on the home page and sends results somewhere…
Last edited by kolaloka (2017-08-21 09:56)
- kolaloka
- Member | 69
SOLVED – WORKS FINE!
I missed a handler between search form and the rendering, now it is:
In the Home presenter:
<?php
protected function createComponentKlientSearchForm() {
$form = new Form;
$form->addText('search', 'Hledat:')
->setRequired(TRUE);
$form->addSubmit('send', 'Hledat');
$form->onSuccess[] = [$this, 'klientSearchFormSucceeded'];
return $form;
}
public function klientSearchFormSucceeded(Nette\Application\UI\Form $form) {
$this->redirect('Search:default', $form->getValues()->search);
}
?>
And in the Search presenter:
<?php
class SearchPresenter extends Nette\Application\UI\Presenter {
/** @var Nette\Database\Context */
private $database;
public function __construct(Nette\Database\Context $database) {
$this->database = $database;
}
public function renderDefault($search) {
$this->template->klient = $this->database->table('nekrolog')
->where('surname', $search)
->order('surname DESC');
}
?>
Thanks to CZechBoY and old.gandalf, who described the handler here:
https://forum.nette.org/…-singel-page