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 4.0.0-beta3] Display a link to add a new item if no results are found #3008

Closed
ianlamb77 opened this issue Feb 5, 2015 · 11 comments
Milestone

Comments

@ianlamb77
Copy link

In previous versions, the formatNoMatches option could be used to provide a link to add a new item if no matches were found:

formatNoMatches: function(term) {
  return "<a href='add_item?term="+term+"' class='new_item'>Add New Item</a>";
 }

However when trying to replicate this using 4.0-beta3's new languages.noResults function:

language: {
  noResults: function() {
    return "<a href='some_URL' class='new_item'>Add New Item</a>";
 }
}

the raw unformatted string appears in the dropdown, rather than a link. Manually creating a DOM object in the function and returning that also doesn't work, though I wouldn't really expect it to. Is there another way to achieve this functionality in 4.0? Thanks.

@ianlamb77 ianlamb77 changed the title Display a link to add a new item if no results are found [select2 4.0.0-beta3] Display a link to add a new item if no results are found Feb 5, 2015
@kevin-brown kevin-brown added this to the 4.0 milestone Feb 5, 2015
@kevin-brown
Copy link
Member

I've updated the code to allow for HTML to be passed back from the language options, but keep in mind that it is passed through escapeMarkup.

You can get around this by building it out as a jQuery object and passing that back, or by overriding escapeMarkup to be a no-op.

@ianlamb77
Copy link
Author

Awesome, thanks for quick response!

@JayatiTehri
Copy link

Hey ,
Can you pls explain how can I customise ' No results found' to a link like ' Add Item'?

@KarlKl
Copy link

KarlKl commented May 5, 2015

@JayatiTehri use language->noResults and escapeMarkup options to customice the no result text. ;-)

$('select').select2({
     language: {
             noResults: function() {
                 return "<a href='http://google.com'>Add</a>";
            }
     },
    escapeMarkup: function (markup) {
        return markup;
    }
});

@elbouillon
Copy link

Where can I found documentation about those functions? I didn't find anything in the main website.

@KarlKl
Copy link

KarlKl commented Aug 17, 2015

The general docu for Select2 can be found here.
The language keyword: Internationalization

The code I provided is just a "workaround". It is a combination of a customized "No results" text and the overridden escapeMarkup function to use HTML in the language strings.

@JNajera
Copy link

JNajera commented Oct 21, 2015

Its any workaround to provide a link to add a new item on the top of the results? I need a fixed option so its always available to the user, but I couldn't figure out how to do it. Im using a custom dataAdapter to set some records when the user open the select. The workaround proposed here its a nice option, but it not work at all for me.

@hassanrehman
Copy link

still not working .. can't get the term entered to use in the formatting of the text

@icrm-amusienko
Copy link

language: { noResults: function (term) { return '<div>' + term + '</dev>' } }
term is undefined always.
Please fix it ASAP.

@KarlKl
Copy link

KarlKl commented Apr 6, 2016

@icrm-amusienko : Please open a new issue. This issue has been closed a year ago...

@wpangestu
Copy link

wpangestu commented Jun 16, 2020

@icrm-amusienko this is how i showing term

language: { 
  noResults: function () {
    var term = event.target.value;
    return '<a href="#">add item ' + term + '</a>' 
  },
  escapeMarkup: function (markup) {
        return markup;
  }
}

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

9 participants