Solved

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

Posted on 2014-12-24
21
1,627 Views
Last Modified: 2015-01-24
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
0
Comment
Question by:andieje
  • 10
  • 9
21 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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...
0
 

Author Comment

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

thaks
0
 

Author Comment

by:andieje
Comment Utility
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
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
<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...
0
 

Author Comment

by:andieje
Comment Utility
i use xampp. I can post the appropriate htdocs folder and and sql dump

thanks
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
and I am guessing that select2 is here
0
 

Author Comment

by:andieje
Comment Utility
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
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
there should be some errors like, cross domain security issues or something like that... check console for any errors...
0
 

Author Comment

by:andieje
Comment Utility
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
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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...
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
or instead of using alert(), maybe you should show an error message on the page (show a hidden div for example)
0
 

Author Comment

by:andieje
Comment Utility
we are on 3.5.2 now?
0
 

Author Comment

by:andieje
Comment Utility
re comment: ID: 40517892

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

thasnk
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
they say it will be fixed in 2.4... maybe this may also work (from developers)

:) it should be Select2 V4.0
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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...
0
 

Author Comment

by:andieje
Comment Utility
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.
0
 

Author Comment

by:andieje
Comment Utility
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 :(
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
Comment Utility
Here is a demo: http://jsbin.com/nubeti/1/edit?html,js,output

essentially you just needed to wrap your javascript on the jquery initializer function

$(function() {
...
});
0
 

Author Closing Comment

by:andieje
Comment Utility
wickedly ace
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now