Change select2 dinamicaly

Change select2 dinamicaly

itarodrigoitarodrigo Posts: 10Questions: 5Answers: 0
edited February 20 in Editor

I have an code:

{ label: "Escola:", name: "rl_usuario_escola_turma_disciplina.escola_id", type: "select2", opts: {
    placeholder: "Selecione a escola",
    initialValue: true,
    ajax: {
        url: 'assets/dados/data_escola.php',
        dataType: 'json',
        delay: 250,
        processResults: function(data){
            return{
                results: data
            };
        },
        cache: true
    }
}},
{ label: "Turma:", name: "rl_usuario_escola_turma_disciplina.turma_id", type: "select2", opts: {
    placeholder: "Selecione a turma",
    initialValue: true,
    ajax: {
        url: 'assets/dados/data_turma.php',
        dataType: 'json',
        delay: 250,
        processResults: function(data){
            return{
                results: data
            };
        },
        cache: true
    }
}},

I need change the select2 opts of the field rl_usuario_escola_turma_disciplina.turma_id when I change the field rl_usuario_escola_turma_disciplina.escola_id.

I try this:

editor.field('rl_usuario_escola_turma_disciplina.escola_id').input().on('change', function(){
    escola_id = editor.field('rl_usuario_escola_turma_disciplina.escola_id').val();
    editor.field('rl_usuario_escola_turma_disciplina.turma_id').input().opts({
    placeholder: "Selecione a turma",
    initialValue: true,
    ajax: {
        url: 'assets/dados/new.php',
        dataType: 'json',
        delay: 250,
        processResults: function(data){
            return{
                results: data
            };
        },
        cache: true
    });
});

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 12,441Questions: 0Answers: 2,107
    Answer ✓

    You would use dependent() to then get the different values for that other Select2 element - the change to the first would trigger the update to the second.

    Colin

  • itarodrigoitarodrigo Posts: 10Questions: 5Answers: 0

    Can you post a example please?

  • kthorngrenkthorngren Posts: 13,581Questions: 25Answers: 3,194

    You likely will find some examples on the forum. Here is one thread that might give you a start.

    Kevin

Sign In or Register to comment.