Select2 with multiple nested groups

后端 未结 4 1547
广开言路
广开言路 2020-12-31 18:17

I\'m having trouble using the Select2 with various groups, only the latter appears.


                        
    
提交评论

  • 2020-12-31 18:33

    I used like this. Working Fine.

    $(document).on("ready", function() {
      function formatResult(node) {
        var level = 0;
        if(node.element !== undefined){
          level = (node.element.className);
          if(level.trim() !== ''){
            level = (parseInt(level.match(/\d+/)[0]));
          }
        }
        var $result = $('<span style="padding-left:' + (20 * level) + 'px;">' + node.text + '</span>');
        return $result;
      };
    
      $("#select2").select2({
        placeholder: 'Select an option',
        width: "300px",
        templateResult: formatResult,
      });
    });
    <!DOCTYPE html>
    
    <html>
    
    <head runat="server">
      <title></title>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    </head>
    
    <body>
      <select id="select2" data-placeholder="Select an option" tabindex="-1" aria-hidden="true">
      <option></option>
      <optgroup label="Base">
        <option class="level_0" value="0">Base Parent</option>
      </optgroup>
      <option class="level_1" value="11">A</option>
      <option class="level_2" value="12">Ant</option>
      <option class="level_3" value="15">Fire Ant</option>
      <option class="level_2" value="14">Apple</option>
      <option class="level_1" value="13">B</option>
    </select>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
    </body>
    </html>

    0 讨论(0)
  • 2020-12-31 18:36

    Take a look at this GitHub issue. What matters is

    HTML itself forbids nesting <optgroup>s, so your markup is invalid before it even reaches Select2. However, you can arbitrarily nest choices via children when you use JS objects to represent the choices.

    This means that you can use children to get multiple nested options. The following solution and jsfiddle are based on fperie's solution.

    <input name="txtConta" id="txtConta" data-placeholder="Selecione a conta" />     
    
    <script>
    var data = [
        {id: "2", name: "2 - Gastos", children: [
            {id: "2.1", name: "2.1 - DESPESA OPERACIONAL FIXA", children: [
                {id: "2.1.1", name: "2.1.1 - PESSOAL", children: [
                    {id: "2.1.1", name: "2.1.1 - PESSOAL"}, 
                    {id: "2.1.1.1", name: "2.1.1.1 - GERENCIA/ADMINSTRATIVO", children: [
                        {id: "2.1.1.1.1", name: "2.1.1.1.1 - SALÁRIOS"},
                        {id: "2.1.1.1.2", name: "2.1.1.1.2 - DIVIDENDOS / COMISSÕES /BONUS"},
                        {id: "2.1.1.1.3", name: "2.1.1.1.3 - INSS"},
                        {id: "2.1.1.1.4", name: "2.1.1.1.4 - FGTS"}
                    ]}
                ]}
            ]}
        ]}
        ];
    
    $('#txtConta').select2({
        allowClear: true,
        width: 'resolve',
        dropdownAutoWidth: true,
        width: '400px',
        data: {results: data, text: "name"}, 
        formatSelection: function(item) { 
            return item.name 
        }, 
        formatResult: function(item) { 
            return item.name 
        }
    });
    </script>
    

    With this solution the leafs are still selectable. If you don't want to select the leafs you should remove the id attribute from the leafs.

    See this JSfiddle that demonstrates both configurations. Take note that I've only used a portion of the data you provided.

    0 讨论(0)
  • 2020-12-31 18:50

    I tried adding this as a comment to Saravanan's post above, but the length of the comment was too long, so consider this as an expansion to his post, and credit goes to him for giving me the idea.

    This is a bit of a necro post from me, but just wanted to expand on how I implemented the above solution with the newer jquery format of $document.ready instead of $document.on. Slightly modified as well, since I have mine nested in a pageLoad, and thus the function is outside the pageLoad, and I used an attribute rather than a class. Important part though, is that I had to put both templateResult and templateSelection for it to work, as without the latter, nothing happened:

    function pageLoad() {
        $(document).ready(function () {
            $(".multiple-group").select2({
                allowClear: true,
                closeOnSelect: false,
                templateResult: formatResult,
                templateSelection: formatResult
            });
        });    
    }
    
    function formatResult(node) {
        var level = 0;
        if (node.element !== undefined) {
            level = node.element.getAttribute("hierarchy-level");
            if (level.trim() !== '') {
                level = parseInt(level) - 1;
            }
        }
    
        var $result = $('<span style="padding-left:' + (15 * level) + 'px;">' + node.text + '</span>');
        return $result;
    }
    
    0 讨论(0)
  • 提交回复
    热议问题