Database filter with form

12 days ago

hegerworld
Member | 3
+
0
-

Hello,
I've been sitting around for a long time. My template has a form and a list of objects. On the left are filters which I can click. Now the object list is to be filtered. But it does not work. All are displayed.
Here is my source:

<?php

namespace App\Presenters;

use App\Model;
use Nette\Application\UI;
use Nette\Security\Passwords;
use Tracy\Debugger;

class FincasPresenter extends \App\Presenters\BasePresenter {

    /** @var Model\ObjektRepository */
    private $objekte;

    public function __construct(Model\ObjektRepository $objekte) {
        $this->objekte = $objekte;
    }

    public function renderDefault() {
        $this->template->objekte = $objekte;
    }


    // called after form is successfully submitted
    public function formSucceeded(UI\Form $form, $vals) {
        dump($vals);
        $this->template->objekte = $this->objekte->findAll()->where('anzahlPersonen  >= ?', 7);
    }

    protected function createComponentFilterForm() {

        $form = new UI\Form;
        $form->getElementPrototype()->class('form-horizontal');

        $form->addGroup('Größe');
        $form->addCheckboxList('personen', '', [
            '0' => 'Alle',
            '1' => 'bis 3 Personen',
            '2' => '4 bis 5 Personen',
        ]);

        $form->addSubmit('suche', 'Suchen');
        $form->onSuccess[] = [$this, 'formSucceeded'];

        $form->setDefaults([
            'personen' => '0']);

        return $form;
    }

}

Can anyone give me a hint?

THH!

12 days ago

CZechBoY
Member | 2452
+
0
-

You should filter by query string (http method GET).

But in this case – you are replacing value in renderDefault, which is filled in formSucceeded before.

11 days ago

hegerworld
Member | 3
+
0
-

CZechBoY wrote:

You should filter by query string (http method GET).

But in this case – you are replacing value in renderDefault, which is filled in formSucceeded before.

If I did not use renderdefault, will it work?
I do not know what I can do

11 days ago

CZechBoY
Member | 2452
+
0
-

You can add condition which will check if you have filled data already, if not then fill.

if ($this->template->data === null) {
    $this->template->data = $this->model->getAllData();
}

10 days ago

hegerworld
Member | 3
+
0
-

Unfortunately, this does not work either. Now comes the error message: The variable ‘objects’ does not exist in template

20:        /** @var Model\ObjektRepository */
21:        private $objekte;
22:
23:        public function __construct(Model\ObjektRepository $objekte) {
24:            parent::__construct($objekte->getDatabase());
25:            $this->objekte = $objekte;
26:        }
27:
28:        public function beforeRender() {
29:
30:            if ($this->template->objekte === null) { // <== ERROR
31:                $this->template->objekte = $this->objekte->findAll();
32:            }
33:
34:            $this->template->objekte = $this->objekte->findAll()->order('titel')->limit(10);

Screenshot

10 days ago

nightfish
Member | 33
+
-1
-

Use if (isset($this->template->objekte)) { instead of if ($this->template->objekte === null) {.

10 days ago

CZechBoY
Member | 2452
+
0
-

It's the same…
Try this

public function renderDefault() {
    if ($this->template->objekte === null) {
        $this->template->objekte = $objekte;
    }
}


// called after form is successfully submitted
public function formSucceeded(UI\Form $form, $vals) {
    dump($vals);
    $this->template->objekte = $this->objekte->findAll()->where('anzahlPersonen  >= ?', 7);
}