Last active
January 15, 2018 22:54
-
-
Save miroslav-mrazek/1c4075fac69acfa74a3224b70f2a0f2f to your computer and use it in GitHub Desktop.
Závislé inputy v Nette 2.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{block content} | |
{snippetArea testFormArea} | |
{form testForm} | |
<table> | |
<tr> | |
<th>{label country}</th> | |
<td data-link="{link invalidateTestForm!}">{input country}</td> | |
</tr> | |
<tr> | |
<th>{label city}</th> | |
<td>{snippet inputCity}{input city}{/snippet}</td> | |
</tr> | |
<tr> | |
<th></th> | |
<td>{input ok}</td> | |
</tr> | |
</table> | |
{/form} | |
{/snippetArea} | |
{/block content} | |
{block scripts} | |
<script> | |
$(function () { | |
var link = $('#frm-testForm-country').parent().data('link'); | |
$('#frm-testForm-country').on('change', function () { | |
$.nette.ajax({ | |
url: link, | |
data: { | |
'country': $('#frm-testForm-country').val() | |
} | |
}); | |
}); | |
}); | |
</script> | |
{/block scripts} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Presenters; | |
use Nette\Application\UI\Presenter; | |
use Nette\Application\UI\Form; | |
class HomepagePresenter extends Presenter | |
{ | |
var $countries = ['cs' => 'Česká Republika', 'sr' => 'Slovenská republika']; | |
var $cities = [ | |
'cs' => ['jbc' => 'Jablonec nad Nisou', 'lbc' => 'Liberec', 'pha' => 'Praha'], | |
'sr' => ['ko' => 'Košice', 'po' => 'Poprad'] | |
]; | |
protected function createComponentTestForm($name) | |
{ | |
$form = new Form($this, $name); | |
$form->addSelect('country', 'Země:', $this->countries) | |
->setPrompt('- vyberte zemi -') | |
->setRequired(); | |
$cities = $form['country']->value ? $this->cities[ $form['country']->value ] : []; | |
$form->addSelect('city', 'Město:', $cities) | |
->setPrompt('- vyberte město -') | |
->setRequired(); | |
$form->addSubmit('ok', 'Odeslat'); | |
$form->onSuccess[] = [$this, 'testFormSuccess']; | |
$form->onError[] = [$this, 'testFormError']; | |
return $form; | |
} | |
public function testFormSuccess(Form $form, $values) | |
{ | |
$this->flashMessage('Data odeslána: ' . implode(', ', array_values($form->getValues(TRUE))) . '.'); | |
$this->redirect('this'); | |
} | |
public function testFormError(Form $form) | |
{ | |
$this->flashMessage('Data nebyla odeslána: ' . implode(', ', array_values($form->getValues(TRUE))) . '.'); | |
$this->redirect('this'); | |
} | |
public function handleInvalidateTestForm($country) | |
{ | |
if( in_array($country, array_keys($this->countries)) ) { | |
$this['testForm']['city']->setItems($this->cities[$country]); | |
} | |
else { | |
$this['testForm']['city']->setItems([]); # uživatel vybere prompt (nulovou) hodnotu | |
} | |
$this->redrawControl('testFormArea'); | |
$this->redrawControl('inputCity'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Samozřejmě je nutné si načíst JS knihovny a inicializovat
nette.ajax.js
.