Link to home
Start Free TrialLog in
Avatar of andieje
andieje

asked on

' format no matches' works with json but not ajax data for select2

Hi

If my select2 is populated with json and no matches are found works with format no matches. However if I my data is populated by ajax I am not notified in any way that no matches are found. There is nothing I can hook into to take appropriate action

<!DOCTYPE html>
<html>
    <head>
        <link href="js/select2.css" rel="stylesheet"/>
        <meta charset="UTF-8">
        <title>Select2 tests</title>
    </head>
    <body>
        <script src="js/jquery-1.11.1.min.js"></script>
        <script src="js/select2.js"></script>
        <label>Select2 with prepopulated data  (e2)  </label>  
        <input type="hidden" name="optionvalue" id="e2" class="input-xlarge" data-placeholder="Choose An Option.." />  
        <br/>
        <label>Select2 with Ajax data  (e3)   </label>  
        <input type="hidden" name="optionvalue" id="e3" class="input-xlarge" data-placeholder="Choose An Option.." />  
        <br/>
 
        <script>
                   var a = [{"id": "15", "text": "arc"}, {"id": "18", "text": "cat"}, {"id": "12", "text": "con"}];
            $('#e2').select2({
                data: a,
                formatNoMatches: function (term) {
                    alert("No Matches in E2 box");
                },
            });
            $('#e3').select2({
                // minimumInputLength: 2,
                // tags:true,
                // multiple: true,
                formatNoMatches: function (term) {
                    alert("No Matches E3 box");
                },
                ajax: {
                    url: 'functions.php',
                    dataType: 'json',
                    data: function (term, page) {
                        return {
                            q: term
                        };
                    },
                    results: function (data, page) {
                        return {results: data};
                    }
                },
            });
           
        </script>
    </body>
</html>

Open in new window


In the e3 select2 the alert box is never shown indicating the function is never called so I cannot respond when there are no matches. The alert for e2 is shown.

Both controls reduce the number of items in the select as you type but in e3 the format no matches is never triggered. The text that you type (that does not exist) stays displayed in the select2. A message appears saying 'no results found' but for some reason this isnt triggered so i cant respond.

 formatNoMatches: function (term) {
                    alert("No Matches E3 box");
                },

Many thanks
Avatar of HainKurt
HainKurt
Flag of Canada image

select2.js ??? can you attach it?

i guess you need to post a test link since it uses functions.php and without seeing what is happening, i cannot say anything...
Avatar of andieje
andieje

ASKER

please could you be clearer in your request as i hav posted the full page exept for populing select2 with ajax

thaks
Avatar of andieje

ASKER

i have posted the full page - the only thing missing is populating the page with ajax but ive just had anew router so i need to check the security
<script src="js/select2.js"></script>

what is this? did you post the code for this? or a link where we can find it?
also, code for functions.php ???

did you post anything related to javascript errors?

when you run in browser, and press F12, and open the console you should be seeing some errors...

so, what I am saying is, with so many unknons, we cannot help you much, unless you post a link to a demo/working site...
Avatar of andieje

ASKER

i use xampp. I can post the appropriate htdocs folder and and sql dump

thanks
and I am guessing that select2 is here
Avatar of andieje

ASKER

yes but the code is in js/select2.js

when i post and look for errors with f12 i have to close the browser if an error is found (not the end of the world) and run the page again

select2 is definitely loading as can be seen in google resources F12

select 2 works on other pages with other functionality but not this
there should be some errors like, cross domain security issues or something like that... check console for any errors...
Avatar of andieje

ASKER

There are no errors in the console or developer tools. Is there anywhere else i can look for errors/.
I will post these examples on line the day after boxig day if you are prepared to look

Much obliged
i found a page says, there is a bug in select2.js

if you comment out this line

"this.dropdown.on("click", killEvent);"

it may work...
check this forum
they say it will be fixed in 2.4... maybe this may also work (from developers)

var options = {
    formatNoMatches: function (term) {
        return 'No user matches "' + term + '".<br/><span class="fake-link" data-click="more-users">Click&nbsp;here</span> for more...';
    }      
};
$input.select2(options)
    .select2('data', myDataArray)
    .select2('open');

// HACK: add click events in the "no results page" template in formatNoMatches function
// This works because there is only one select2 dropdown at a time and we know it is already open from the previous line
$('#select2-drop').click(function (e) {
    if ($(e.target).data('click') === 'more-users') {
        // DO SOMETHING NOW THAT THE LINK WAS CLICKED
    }
});

Open in new window

or instead of using alert(), maybe you should show an error message on the page (show a hidden div for example)
Avatar of andieje

ASKER

we are on 3.5.2 now?
Avatar of andieje

ASKER

re comment: ID: 40517892

Please can you explain what that will achieve or what new informaton it may reveal

thasnk
they say it will be fixed in 2.4... maybe this may also work (from developers)

:) it should be Select2 V4.0
maybe alert is killed somehow, but instead displaying a hidden div on the page may work...
just an idea... since i cannot test it locally...
Avatar of andieje

ASKER

How would you use a hidden div to fix it?

 Im a it confued already about the 2 types of select2 and one with a hidden inout and one not as they have inconsistent behaviour.
Avatar of andieje

ASKER

also

i found a page says, there is a bug in select2.js

if you comment out this line

"this.dropdown.on("click", killEvent);"

it may work...

didnt work :(
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andieje

ASKER

wickedly ace