question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Merge options under <optgroup> when possible

See original GitHub issue

I have a grouped select2 list that uses pagination to load 30 list items at a time. Each <optgroup> contains one or more <option> list items.

Feature request: an option group can cross the boundary of 30 items. When this happens, groups with the same labels at the page boundaries should be merged.

Example

optgroup group 1 contains 31 options, AJAX page size is 30.

AJAX Page 1

{
  "results" : [
    { 
      "text" : "group 1"
      "children" : [
        {
          "id" : "item1"
          "text" : "first item"
        },
        {
          "id" : "item2"
          "text" : "second item"
        },
        // items 3 to 29 omitted for brevity
        {
          "id" : "item30"
          "text" : "thirtied item"
        }
      ]
    }
  ]
  "pagination" : {
    "more" : true
  }
}

AJAX Page 2

{
  "results" : [
    { 
      "text" : "group 1"
      "children" : [
        {
          "id" : "item31"
          "text" : "thirty-first item"
        }
      ]
    }
  ]
  "pagination" : {
    "more" : false
  }
}

Expected result

The code should recognize that page 2 should be merged with page 1 instead of appended, because the group header is the same.

<optgroup label="group 1">
    <option value="item1">first item</option>
    <option value="item2">second item</option>
    <!--  items 3 to 29 omitted for brevity -->
    <option value="item30">thirtied item</option>
    <option value="item31">thirty-first item</option>
</optgroup>

Actual result

<optgroup label="group 1">
    <option value="item1">first item</option>
    <option value="item2">second item</option>
    <!--  items 3 to 29 omitted for brevity -->
    <option value="item30">thirtied item</option>
</optgroup>
<optgroup label="group 1">
    <option value="item31">thirty-first item</option>
</optgroup>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
SandovalDanilocommented, Mar 4, 2017

Any chance to update the main project to implement this feature?

0reactions
Discartypticscommented, Mar 18, 2021

This is a feature I’d love to have, since I deal with a lot of data and sometimes that data is grouped.

For anybody else finding this thread via Google, here’s a small example of how I was able to achieve a similar feature. This code manipulates the returned server data, replacing “duplicate” groups with their children. (Alternatively, you could also send the “latestGroup” variable to the server and check the group on the server-level.)

Feel free to use however you want, no credit needed.

let latestGroup;
$(dropdown).select2({
    ...
    ajax: {
        ...
        processResults: (data,params) => {
            const oldResults = data["results"];
            let newResults;
            if (oldResults.length > 0) {
                if (latestGroup !== undefined) {
                    newResults = [];
                    Array.from(oldResults).forEach(result => {
                        if (result["children"] !== undefined && result["text"].trim().toUpperCase() === latestGroup.trim().toUpperCase()) {
                            Array.from(result["children"]).forEach(child => {
                                newResults.push(child);
                            });
                        } else {
                            newResults.push(result);
                        }
                    });
                } else {
                    newResults = oldResults;
                }
                Array.from(oldResults).forEach(result => {
                    if (result["children"] !== undefined) {
                        latestGroup = result["text"];
                    } 
                });
            } else {
                newResults = oldResults;
            }
            return {
                results: newResults,
                ...
            };    
        }
    }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add optgroup and options dynamically one by one
1 Answer 1 ... You can try to watch checkbox change event and reiterate over all checkboxes to collect questions. Then, generate HTML...
Read more >
Mixing grouped and non-grouped OPTIONs inside a SELECT?
Simply spoken, if some options are not grouped, you don't use an option group to combine them. For example if you have a...
Read more >
H85: Using optgroup to group option elements inside a select
It is possible for a select element to contain both single option elements and optgroup groups, though authors should consider if this is...
Read more >
change select based on optgroup filter - CodePen
Ever have a need to fine tune a select box based on external triggers but don't want/need to update the select contents via...
Read more >
grouped_options_for_select (ActionView::Helpers - APIdock
Note: It is possible for this value to match multiple options as you might have the same option in multiple groups. Each will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found