Fill a select via jquery in cakephp

Asked

Viewed 63 times

1

I made it a little easier, but it’s not working yet. Instead of assembling everything in javascript, in my action I assemble all html and in javascript I only pass the mounted html to my select.

function MostraPrograma(){
 var _valorSelect = document.getElementById("CursoEscolaId").value;     
 $.post('/novas/mostra_programa/'+_valorSelect),$("#CursoNewAddForm").serialize(), function(data){                          
    $("#rowIdChata").html(data);

 }

}

and this is my view:

<?php
 if($info_campus == 0){
echo 'Não existem tipos de curso para esta escola';   
}else{
foreach($info_campus as $info){?>
<option value="<?=$info['CursoCampus']['id']?>"><?=$info['CursoCampus']['nome']?></option><?  php }} ?>

1 answer

0


Your application will need to perform two operations:

1) Parse the string in JSON format for javascript, there are several forms on the internet, either with pure Javascript or with libraries, but I do so:

Obs. I switched the $.post for $.get I think that’s the method you need.

     $.get('/novas/mostra_programa/'+_a, function(data){
         var opcoes = (new Function("return " + data +";"))();
     });

2) Loop and assemble options

   $.get('/novas/mostra_programa/'+_a, function(data){
      var opcoes = (new Function("return " + data +";"))();

      var sel = document.getElementById('{ID DO SELECT}');
      var fragment = document.createDocumentFragment();

      for(var i = 0; i < opcoes.length; i++){
        var opt = document.createElement('option');
        opt.innerHTML = opcoes[i].CursoCampus.nome;
        opt.value = opcoes[i].CursoCampus.id;
        fragment.appendChild(opt);
     }

      sel.appendChild(fragment.cloneNode(true));
  });

  • edited, made it a little easier, but still doesn’t work, it seems to me that when he gives post not back to function, and nothing that is inside my post runs.

  • applies step 1, and uses the.log console to identify whether it is occurring as predicted. Then apply step 2 and use the.log console to see if you are also proceeding as desired

  • I made a small modification in step 2 too, in the excerpt -> Fragment.cloneNode(true)

  • the problem was that I was running the function $post wrong, I was doing $post(url),function and the right thing is $post(url,function{});

  • Wow, you didn’t even realize this mistake. But it’s a good thing you found it yourself. You can be sure you learned a lot. Good luck there

  • Thank you very much, we took our time, but we made it.

  • That’s it, good luck!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.