Používání DependentSelectBox ve formuláři
- jAkErCZ
- Člen | 322
Zdravím
už nějakou dobu používám DependentSelectBox od NasExt\Forms\DependentData ale narazil jsem na problém kdy z databáze chci vrátit důvody.
Náhled DB:
DROP TABLE IF EXISTS `reason`;
CREATE TABLE `reason` (
`reason_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_czech_ci NOT NULL,
`order_no` int(11) NOT NULL,
`hidden` tinyint(4) NOT NULL,
`parent_reason_id` int(11) DEFAULT NULL,
PRIMARY KEY (`reason_id`),
KEY `perrent_reason_id` (`parent_reason_id`),
CONSTRAINT `reason_ibfk_1` FOREIGN KEY (`parent_reason_id`) REFERENCES `reason` (`reason_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
Model:
/**
* @return \Nette\Database\Table\Selection
*/
public function getReason(){
return $this->database->table(self::TABLE_REASON)->where(self::COLUMN_PARRENT_ID, 'IS NULL');
}
/**
* @param $id
* @return \Nette\Database\Table\Selection
*/
public function getReasonParrentID($id) {
return $this->database->table(self::TABLE_REASON)
->where(self::COLUMN_PARRENT_ID, $id);
}
a Formulář:
protected function createComponentStatusProjectMessageForm()
{
$form = new Form;
$form->addHidden('project_id');
$form->addHidden('status');
$form->addHidden('date');
$form->addSelect('reason_name', '', $this->projectRepository->getReason()->fetchPairs('reason_id','title'))
->setPrompt('Vyberte důvod')->setRequired('Důvod je povinný!');
$form->addDependentSelectBox('reason', '', $form['reason_name'])
->setDependentCallback(function ($values){
$data = new \NasExt\Forms\DependentData;
return $data->setItems($this->projectRepository->getReasonParrentID($values['reason_name'])->fetchPairs('reason_id', 'title'));
})
->setPrompt('Vyberte Kategorii')->setRequired('Kategorie je povinná');
$form->addTextArea('message')->setRequired('Vyplňte text statusu!');
$form->addSubmit('submit', 'Uložit');
$form->onSuccess[] = [$this, 'editorStatusProjectMessageSucceeded'];
return $form;
}
Problém je že když si vyberu hlavní Kategorie tudíž ty které v db mají nastavené parrent_reason_id na NULL tak se mi již nevykreslí ty které spadají do této kategorie.
Vykreslení:
{block modalHeader}
{block title} Důvod nezobchodování projektu?{/block}
{/block}
{block modal}
<form n:name="statusProjectMessageForm">
<div class="form-group row">
<label for="normal-field"
class="col-md-4 form-control-label text-md-right">Důvod</label>
<div class="col-md-4">
<div class="form-group">
<select class="form-control" n:name="reason"></select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<select class="form-control" n:name="reason_name" ></select>
</div>
</div>
</div>
<div class="form-group row">
<label for="normal-field"
class="col-md-4 form-control-label text-md-right">Popis důvodu</label>
<div class="col-md-7">
<textarea n:name="message" rows="5" cols="45" placeholder="Vložte důvod nezobchodování..."></textarea>
</div>
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-offset-4 col-md-7">
<button n:name=submit type="submit" class="btn btn-primary">Uložit
</button>
<button type="button" class="btn btn-gray" data-dismiss="modal">Zavřít</button>
</div>
</div>
</div>
</form>
<script src="{$basePath}/js/live-form-validation.min.js"></script>
<script src="{$basePath}/js/dependentSelectBox.js"></script>
<script>
// @param callback a handler to be called when Ajax requests complete
$('[data-dependentselectbox]').dependentSelectBox();
</script>
{/block}
Mohu se zeptat jak bych to mohl řešit?
Díky