Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select2 issue in which selected value is not showing properly when the Select is re-opened #6292

Closed
ManoMahe opened this issue Jan 6, 2024 · 1 comment

Comments

@ManoMahe
Copy link

ManoMahe commented Jan 6, 2024

I have integrated Select2 plugin into my project which is working as good as I wanted. But I'm still facing small issues like whenever I scroll down and selected the last value on the select2 options list got selected and closed correctly.

My default initial Select2 input design below:

enter image description here

  1. If I wanted to re-open it the selected value(Group 3) is not automatically shown at top/scrolled to that selected option, so manually again I need to scroll down and see the option making the user uncomfortable isn't it? Is there any solution to make this working properly as like that html select select input works?

    enter image description here

    If I scrolled to down its showing as selected, but not automatically its happening

    enter image description here

  2. Another issue am facing is, whenever the keyboard down key presses to select the option, the highlighted option also not automatically scrolled down and shown within the option list without scrolling it by mouse

    enter image description here

My script Select2 initialization code for better understanding below:

<script>
    $('.selectsearch').select2({
        // tags: 'true',
        theme: 'bootstrap5',
        dropdownParent: $('.filter-form'),
        placeholder: 'Select an option',
        templateResult: function(item) {
            if (item.loading) {
                return item.text;
            }
            var term = query.term || '';
            var $result = markMatch(item.text, term);
            return $result;
        },
        language: {
            searching: function(params) {
                // Intercept the query as it is happening
                query = params;
                // Change this to be appropriate for your application
                return 'Searching...';
            }
        },
        cache: true,
    });

    function markMatch(text, term)
    {
        if ($.trim(term) === '')
        {
            text = '<b class="fw-bold">' + text.substring(0, text.indexOf('(')) + '</b>' + text.substring(text.indexOf('('));
        }
        var startString = '<b class="fw-bold">';
        var endString = text.replace(startString, '');
        var match = endString.toUpperCase().indexOf(term.toUpperCase());
        var $result = $('<span></span>');
        // If there are no search terms, return all of the data
        if ($.trim(term) === '') {
            return $result.append(text);
        }
        if (match < 0) {
            return $result.text(text);
            // return $result.append(text);
        }
        var elementToReplace = endString.substr(match, term.length);
        var $match = '<span class="select2-rendered__match">' + endString.substring(match, match + term.length) + '</span>';
        text = startString + endString.replace(elementToReplace, $match);
        $result.append(text);
        return $result;
    }
</script>
@ManoMahe
Copy link
Author

ManoMahe commented Feb 2, 2024

After several manual trial and error methods, I found an easy way to fix this issue by just commenting out the theme:'bootstrap5' attribute and using the default theme of select2.

After that, I made some changes to the CSS to fit it into my project. Now my select2 script looks like below:

<script>
    $('.selectsearch').select2({
        // theme: 'bootstrap5',
        dropdownParent: $('.filter-form'),
        placeholder: 'Select an option',
        templateResult: function (item) {
            if (item.loading) {
                return item.text;
            }
            var term = query.term || '';
            var $result = markMatch(item.text, term);
            return $result;
        },
        language: {
            searching: function (params) {
                query = params;
                return 'Searching...';
            }
        },
        cache: true,
    });
</script>

Anyhow I didn't get why, the setting theme:'bootstrap5' did not throw me any exception or error if it does not exist, then why its not taking default theme settings automatically.

@ManoMahe ManoMahe closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant