how to implement search field in the page

kolaloka
Member | 69
+
0
-

I am looking for a way to implement a search field within the main page of my web.
Any ideas or links to solutions please?
Thank you.

CZechBoY
Member | 3608
+
+2
-
  • create form with method get
    • render it in @layout.latte as component {control searchForm}
  • create respository which returns data
    • on form success filter in reposiry
  • create results page
    • on form success redirect to results

Last edited by CZechBoY (2017-08-16 20:17)

kolaloka
Member | 69
+
0
-

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
+
+3
-

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);
	}
}
CZechBoY
Member | 3608
+
0
-

That should be render results because render will do loop to the first step :D

kolaloka
Member | 69
+
0
-

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?

CZechBoY
Member | 3608
+
+1
-

Because you use echo in form component. You should save it to template and render it in template.
Maybe you should create another component for results rendering or pass results to presenter.

kolaloka
Member | 69
+
0
-

oh, you're lightning fast :-)
Thank you, I am going to try it.

kolaloka
Member | 69
+
0
-

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
+
0
-

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

namphan1998
Member | 3
+
0
-

This is amazing.
THank you so much for the information.