Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2043
  • Last Modified:

' 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
0
andieje
Asked:
andieje
  • 10
  • 9
1 Solution
 
HainKurtSr. System AnalystCommented:
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
 
andiejeAuthor Commented:
please could you be clearer in your request as i hav posted the full page exept for populing select2 with ajax

thaks
0
 
andiejeAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
HainKurtSr. System AnalystCommented:
<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
 
andiejeAuthor Commented:
i use xampp. I can post the appropriate htdocs folder and and sql dump

thanks
0
 
HainKurtSr. System AnalystCommented:
and I am guessing that select2 is here
0
 
andiejeAuthor Commented:
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
 
HainKurtSr. System AnalystCommented:
there should be some errors like, cross domain security issues or something like that... check console for any errors...
0
 
andiejeAuthor Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
or instead of using alert(), maybe you should show an error message on the page (show a hidden div for example)
0
 
andiejeAuthor Commented:
we are on 3.5.2 now?
0
 
andiejeAuthor Commented:
re comment: ID: 40517892

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

thasnk
0
 
HainKurtSr. System AnalystCommented:
they say it will be fixed in 2.4... maybe this may also work (from developers)

:) it should be Select2 V4.0
0
 
HainKurtSr. System AnalystCommented:
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
 
andiejeAuthor Commented:
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
 
andiejeAuthor Commented:
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
 
RobOwner (Aidellio)Commented:
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
 
andiejeAuthor Commented:
wickedly ace
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now