|
| 1 | +NasExt/FilterFormControl |
| 2 | +=========================== |
| 3 | + |
| 4 | +Data filter control for Nette Framework. |
| 5 | + |
| 6 | +Requirements |
| 7 | +------------ |
| 8 | + |
| 9 | +NasExt/FilterFormControl requires PHP 5.3.2 or higher. |
| 10 | + |
| 11 | +- [Nette Framework](https://github.com/nette/nette) |
| 12 | + |
| 13 | +Installation |
| 14 | +------------ |
| 15 | + |
| 16 | +The best way to install NasExt/FilterFormControl is using [Composer](http://getcomposer.org/): |
| 17 | + |
| 18 | +```sh |
| 19 | +$ composer require nasext/filter-form-control |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +```php |
| 26 | +class FooPresenter extends Presenter |
| 27 | +{ |
| 28 | + |
| 29 | + public function renderDefault() |
| 30 | + { |
| 31 | + /** @var NasExt\Controls\FilterFormControl $filter */ |
| 32 | + $filter = $this['filter']; |
| 33 | + $filterData = $filter->getData(); // data for filter |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * @return NasExt\Controls\FilterFormControl |
| 39 | + */ |
| 40 | + protected function createComponentFilter($name) |
| 41 | + { |
| 42 | + $control = new NasExt\Controls\FilterFormControl($this, $name); |
| 43 | + |
| 44 | + $control->setDefaultValues(array('code' => 'some default value for code input')); |
| 45 | + |
| 46 | + /** @var Form $form */ |
| 47 | + $form = $control['form']; |
| 48 | + |
| 49 | + $form->addText('code', 'Code'); |
| 50 | + $form->addText('name', 'Name'); |
| 51 | + |
| 52 | + return $control; |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +###Use dataFilter callback for filtering data in persistent param in url |
| 59 | +If you need use special filter input use object in input, like Nette\Datetime, |
| 60 | +so you must translate this object to timestamp before set persistent parameter for url, and after load data from persistent parameter |
| 61 | +you need transform timestamp to Nette\Datetime object. For this process use setDataFilter(). |
| 62 | +- FILTER_IN: use when transform object to string in process set persistent parameter for url |
| 63 | +- FILTER_OUT: use when transform string from persistent parameter to object |
| 64 | +```php |
| 65 | + /** |
| 66 | + * @return NasExt\Controls\FilterFormControl |
| 67 | + */ |
| 68 | + protected function createComponentFilter($name) |
| 69 | + { |
| 70 | + $control = new NasExt\Controls\FilterFormControl($this, $name); |
| 71 | + |
| 72 | + $control->setDataFilter(function ($values, $filterType) { |
| 73 | + if (array_key_exists('date', $values) && !empty($values['date'])) { |
| 74 | + if ($filterType == FilterFormControl::FILTER_IN) { |
| 75 | + if ($values['date'] instanceof DateTime) { |
| 76 | + $values['date'] = $values['date']->getTimestamp(); |
| 77 | + }else{ |
| 78 | + $values['date'] = strtotime($values['date']); |
| 79 | + } |
| 80 | + } elseif ($filterType == FilterFormControl::FILTER_OUT) { |
| 81 | + if (!$values['date'] instanceof DateTime) { |
| 82 | + $date = new DateTime(); |
| 83 | + $values['date'] = $date->setTimestamp($values['date']); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return $values; |
| 89 | + }); |
| 90 | + |
| 91 | + /** @var Form $form */ |
| 92 | + $form = $control['form']; |
| 93 | + |
| 94 | + $form->addText('date', 'Date'); |
| 95 | + |
| 96 | + return $control; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +###FilterFormControl with ajax |
| 102 | +For use FilterFormControl with ajax use setAjaxRequest() and use events onFilter[], onReset[] for invalidateControl |
| 103 | +```php |
| 104 | + /** |
| 105 | + * @return NasExt\Controls\FilterFormControl |
| 106 | + */ |
| 107 | + protected function createComponentFilter($name) |
| 108 | + { |
| 109 | + $control = new NasExt\Controls\FilterFormControl($this, $name); |
| 110 | + // enable ajax request, default is false |
| 111 | + $control->setAjaxRequest(); |
| 112 | + |
| 113 | + $that = $this; |
| 114 | + $invalidateControl = function ($component, $values) use ($that) { |
| 115 | + if ($that->isAjax()) { |
| 116 | + $that->invalidateControl(); |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + $control->onFilter[] = $invalidateControl; |
| 121 | + $control->onReset[] = $invalidateControl; |
| 122 | + |
| 123 | + /** @var Form $form */ |
| 124 | + $form = $control['form']; |
| 125 | + |
| 126 | + $form->addText('code', 'Code'); |
| 127 | + $form->addText('name', 'Name'); |
| 128 | + |
| 129 | + return $control; |
| 130 | + } |
| 131 | +``` |
| 132 | + |
| 133 | +###Set templateFile for FilterFormControl |
| 134 | +For set templateFile use setTemplateFile() |
| 135 | +```php |
| 136 | + /** |
| 137 | + * @return NasExt\Controls\FilterFormControl |
| 138 | + */ |
| 139 | + protected function createComponentFilter($name) |
| 140 | + { |
| 141 | + $control = new NasExt\Controls\FilterFormControl($this, $name); |
| 142 | + $control->setTemplateFile('myTemplate.latte'); |
| 143 | + |
| 144 | + /** @var Form $form */ |
| 145 | + $form = $control['form']; |
| 146 | + |
| 147 | + $form->addText('code', 'Code'); |
| 148 | + $form->addText('name', 'Name'); |
| 149 | + |
| 150 | + return $control; |
| 151 | + } |
| 152 | +``` |
| 153 | + |
| 154 | +----- |
| 155 | + |
| 156 | +Repository [http://github.com/nasext/filterformcontrol](http://github.com/nasext/filterformcontrol). |
0 commit comments