0

I have a problem with snippet updating. After changing the selection in the select box, I redraw the contents of options in another select box but the snippet doesn't update.

Latte:

<form class="df-checkout">
...

<select n:href="getHraci!" name="domaci" id="domaci" class="form-control">
    <option value="">Vybrat</option>
    <option n:foreach="$tymy as $tym" value="{$tym->getId()}">
        {$tym->getNazev()}
    </option>
</select>

...

<div class="row helpers hidden">
    <select n:snippet="hraciDomaci" class="form-goly-domaci-select form-control">
        <option></option>
        <option n:foreach="$hraciDomaci as $hrac" value="{$hrac->getId()}">
            {$hrac->getPrijmeni()} {$hrac->getJmeno()}
        </option>
    </select>

    <input type="text" class="form-goly-input form-control">
</div>

JS file:

$(document).ready(function(){
    $("#domaci").bind('change', function() {
        var link = $(this).attr("href");

        $.nette.ajax ({
            url: link,
            data: {"strana": "domaci", "tymId": $(this).val()},
            type: 'get',
            dataType:'json'
        });
    });
});

Controller:

public function handleGetHraci($strana, $tymId)
{
    $tym = $this->tymManager->getTymRepository()->find($tymId);
    $muzstvo = $this->tymManager->getMuzstvoRepository()->findBy(["nazev" => self::HLAVNI_TYM]);
    $hraci = $this->hracManager->getHracRepository()
                ->findBy(["tym" => $tym, "muzstvo" => $muzstvo], ["prijmeni" => "ASC", "jmeno" => "ASC"]);

    if($this->isAjax()){
        $this->template->hraciDomaci = $hraci;

        $this->redrawControl('hraciDomaci');
    }
}

The form has not yet been created and processed, so the first select box I have done is temporarily and select box with the snippet is independent on the form. I use it for copying. JS calls the handler correctly, and if I dump $this->template->hraciDomaci before redrawControl the data is there, but redrawControl will not do anything. But a new line with the process is added to the page in the lower Tracy bar. I do not have a bug in my debugger, the process has status 200 but response contains only:

{"state":[],"snippets":{"snippet--hraciDomaci":"\t\t\t\t\t\t\t\t\t\t<option></option>\n"}}

I tried to use $.get instead of $.nette.ajax, wrap in snippetArea and I normally have this code in {block content}, so snippetArea should not be needed. nette.ajax.js with initialization $.nette.init(); I have too.

Thanks a lot for any advice.

Johny12369
  • 75
  • 7
  • 1
    Your ajax code does not do anything after the call. the php needs to return the html and your ajax cone has to insert it. Also .bind is deprecated for .on – mplungjan Sep 07 '18 at 08:22
  • @mplungjan no, it is working but I had a problem as comment below describes. But thanks with that .bind deprecatiation. I'll change that. – Johny12369 Sep 07 '18 at 15:21

1 Answers1

1

check that you are not rewriting hraciDomaci variable in render* method (e.g. renderDefault) in the presenter

  • I had no idea it is a problem what it's ajax request and there's no reload of the page. I was setting empty array to the variable in render* method. Fixed! Thanks a lot! – Johny12369 Sep 07 '18 at 15:13