Change select2 dinamicaly

Change select2 dinamicaly

itarodrigoitarodrigo Posts: 15Questions: 6Answers: 0
edited February 2021 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: 15,112Questions: 1Answers: 2,583
    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: 15Questions: 6Answers: 0

    Can you post a example please?

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

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

    Kevin

This discussion has been closed.