Předávání stringu pomocí session v ajax komponentě

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
Stejk
Člen | 13
+
0
-

Zdravím,
dělám sadu komponent typu DataGrid. Máme jednu hlavní komponentu BaseGrid co samozřejmě dědí od Control.

V této komponentě voláme další komponentu Filter, nad jednotlivímy sloupci

public function createComponentFilter()
        {
            return new Multiplier(function ($columnName) {
                return new \App\Filter\FilterControl($columnName, $this->session, $this->database);
            });
        }

kde je $columnName jméno sloupce

Konstruktor komponenty Filter

public function __construct($column_name, Session $session, $database)
    {
        $this->session = $session;
        $this->sessionSection = $this->session->getSection('filtr');
        $this->column_name = $column_name;
        $this->database = $database;
    }

v komponentě filter máme ajax formulář a po jeho odeslání potřebujem předat string metodě render() v komponentě DataGrid co získáme právě v tom formuláři.

Skoušíme to pomocí sessions, ale session je i po odeslání prázdná. Děláme redraw té komponenty Datagrid i Filter

CZechBoY
Člen | 3608
+
0
-
  1. používej továrničky – vyhneš se všem new Control a šílenýmu předávání závislostí
  2. použij signál ke změně stavu komponenty – potom si ty data můžeš předávat persistentně
class Komponenta extends Control
{
	/**
	 * @var array
	 * @persistent
	 */
	public $mojePersistentniData;
}
Stejk
Člen | 13
+
0
-

Moje komponenta Filter

<?php
 namespace App\Filter;

 use Nette;
 use Nette\Application\UI\Form;
 use Nette\Forms\Controls\SubmitButton;
 use Nette\Forms\Container;
 use Nette\Http\Session;
 use Nette\Http\Context;

 class FilterControl extends Nette\Application\UI\Control{
    /** @var Nette\Http\Session */
    private $session;

    /** @var Nette\Http\SessionSection */
    private $sessionSection;

    /** @var Nette\Database\Context */
    private $database;


    private $column_name;
    //private $user;

    public function __construct($column_name, Session $session, $database)
    {
        $this->session = $session;
        $this->sessionSection = $this->session->getSection('filtr');
        $this->column_name = $column_name;
        $this->database = $database;
        //$this->user = $user;
    }
    protected function createComponentFilterForm()
    {
        $form = new Nette\Application\UI\Form;
        $form->getElementPrototype()->class('ajax');

        $filters = $form->addDynamic('filters', function (Container $filter){
            $filter->addSelect('how', 'What: ', $this->getPossibleExpressions())
                    ->setPrompt("Choose expression");
            $filter->addText('val', 'Value: ');
            $filter->addSelect('operator', "Operator: ", $this->getPossibleOperators());
            $filter->addSubmit('remove', 'Remove')
                ->setValidationScope(FALSE) # disables validation
                ->onClick[] = array($this, 'FilterFormRemoveElementClicked');
        }, 1);

        $filters->addSubmit('add', 'Add new filter')
                ->onClick[] = array($this, 'FilterFormAddElementClicked');
        $form->addSubmit('del', 'Delete filter')
            ->onClick[] = array($this, 'DeleteFilterClicked');
        $form->addSubmit('save', 'Save filter')
            ->onClick[] = array($this, 'SaveFilterClicked');
        $form->addSubmit('load', 'Save filter')
            ->onClick[] = array($this, 'LoadFilterClicked');

        $form->addSubmit('submit', 'Filtrate');
        $form->onSuccess[] = array($this, 'filtrationFormSucceeded');
        return $form;
    }


    public function filtrationFormSucceeded(Form $form)
    {
        $expressions = array(
            "equal_to" => " = ?",
            "not_equal_to" => " != ?",
            "bigger" => " > ?",
            "not_bigger" => " <= ?",
            "smaller" => " < ?",
            "not_smaller" => " >= ?",
            "start_with" => " LIKE ?",
            "not_start_with" => "NOT LIKE ?",
            "end_with" => "LIKE ?",
            "not_end_with" => " NOT LIKE ?",
            "equal" => " LIKE ?",
            "not_equal" => "NOT LIKE ?"
        );
        $parameters = "";
        $finalFilter = array();
        foreach ($form['filters']->values as $filter)
        {
            $parameters .= " (" . $this->column_name . $expressions[$filter->how] . ") " . $filter->operator;
            switch ($filter['how']) {
                case "start_with":
                    $val = $filter['val'] . '%';

                    break;
                case "not_start_with":
                    $val = $filter['val'] . '%';

                    break;
                case "end_with":
                    $val = '%' . $filter['val'];

                    break;
                case "not_end_with":
                    $val = '%' . $filter['val'];

                    break;
                case "equal":
                    $val = '%' . $filter['val'] . '%';

                    break;
                case "not_equal":
                    $val = '%' . $filter['val'] . '%';

                    break;

                default:
                    $val = $filter['val'];
                    break;
            }
            array_push($finalFilter, $val);
        }
        array_unshift($finalFilter, $parameters);
        $this->sessionSection[$this->column_name]=$finalFilter;
        $this->redrawControl('filterForm');
        $this->redrawControl("BaseGrid");
        //$this->presenter->redrawControl("BaseGrid");

    }

    public function getPossibleExpressions()
    {
        $expressions = array(
            "equal_to" => "equal_to",
            "not_equal_to" => "not_equal_to",
            "bigger" => "bigger",
            "not_bigger" => "not_bigger",
            "smaller" => "smaller",
            "not_smaller" => "not_smaller",
            "start_with" => "start_with",
            "not_start_with" => "not_start_with",
            "end_with" => "end_with",
            "not_end_with" => "not_end_with",
            "equal" => "equal",
            "not_equal" => "not_equal"
        );
        return $expressions;
    }
    public function getPossibleOperators() {
        $operators = array(
            "AND"=> "and",
            "OR"=> "or"
        );

        return $operators;
    }

    public function render(){
        $template = $this->template;


        $template->setFile(__DIR__ . '/filter.latte');

        $template->render();
    }
 }

a komponenta DataGrid

<?php

namespace App\Datagrid;

use Nette\Application\UI\Control;
use Nette\Application\UI\Form;
//use Nette\Utils\ArrayHash;
use Nette\Application\Responses\TextResponse;
use Nette\Application\UI\Multiplier;

use App\Datagrid\GridColumn;
use App\Datagrid\GridRow;
use App\Datagrid\Localization;
use App\Datagrid\CsvExporter;
use App\Filter;
use App\Http;

use Tracy\Debugger;

class BaseGrid extends Control
{
        /** @var Nette\Http\Session */
        private $session;

        /** @var Nette\Http\SessionSection */
        private $sessionSection;

        private $data;
        private $dataSource;


        private $database;

	public function __construct($session, $database)
	{

            $this->session->getSection('filtr');

	}

        public function createComponentFilter()
        {
            return new Multiplier(function ($columnName) {
                return new \App\Filter\FilterControl($columnName, $this->session, $this->database);
            });
        }

        public function render()
        {
            $this->data;
                if($this->session->hasSection('filtr'))
                {
                    $sestaveny=$this->session->getSection('filtr');
                    Debugger::barDump($this->session->getSection('filtr'));
                    $this->data = $this->dataSource->where($sestaveny)->fetchAll();
                }
                else{
                    $this->data = $this->dataSource->fetchAll();
                }

            $template = $this->template;
            $template->setTranslator($this->localization);
            $template->setFile(__DIR__ . '/BaseGrid.latte');
            $template->neco = $this->data;

            $template->render();
        }
CZechBoY
Člen | 3608
+
0
-

No, a zkousel si ty moje tipy?