Editors Select2

Editors Select2

ZakInterDevZakInterDev Posts: 51Questions: 16Answers: 0

Does DataTables/Editor support the grouping option for select2? For example when sending data back using options (see code below) or updating the list after the dependent() method has been called? I'm trying to achieve a header in my select2 list of a branch name, then all the cylinders that belong to that branch. Any ideas?

->options(function() {
        $out = array();
        foreach ($cylinders as $cylinder) {
                $out[] = array("value" => $cylinder->id, "label" => $cylinder->barcode);
        }
        return $out;
})

Thanks in advance

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    Not using the built in update method of the field type, which supports only a simple list, but I found this SO discussion which shows the data structure that Select2 uses for opt groups and you could set that against the instance manually rather than having Editor do it.

    Regards,
    Allan

  • ZakInterDevZakInterDev Posts: 51Questions: 16Answers: 0

    So just for anyone coming across this thread. This is the solution, in terms of the link Allan posted.

    editor.on('open', function (e) {
            var select2_ary = [];
    
            select2_ary.push({
                id: "one",
                text: "one"
            }, {
                text: 'group',
                children: [{
                        id: "sub-one",
                        text: "sub-one"
                    }, {
                        id: "sub-two",
                        text: "sub-two"
                    }]
            }, {
                id: 'three',
                text: 'three'
            });
            
            editor.field('cylinders[].id').inst().select2({
                data: select2_ary
            });
        });
    

    Just need to figure out an easy way to get the same format sent back from an aJax request.

    Thanks once again Allan. Loving DataTables & Editor..using them on a new website we're developing.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Thanks for posting back with your solution. And great to hear it is going well!

    Allan

This discussion has been closed.