jquery - Select2: Trim entered text and if empty - prevent sending Ajax requests -


i using select2 ajax load remote data (suggestions).

everything works fine, there 1 problem: when user enters spaces (by pressing spacebar) - ajax request being sent.

how can prevent this? also, trim terms before being sent.

$(".js-data-example-ajax").select2({   placeholder: "select... ",   minimuminputlength: 3,   ajax: {     url: "/my-site/tags",     datatype: 'json',     data: function (params) {         return {             q: params.term         };     },     processresults: function (data) {         return {             results: data         };     },     cache: true } 

edit:

i forgot mention have tried this:

        data: function (params) {             if (params.term.trim().length  < 2) {                 return;             }             return {                 q: $.trim(params.term)             };         }, 

and (ajax) request still sent.

you can use data function check length of search term (after trim), , return if length < 3:

data: function (params) {   if (params.term.trim().length  < 3) {     throw false;   }   return {     q: params.term.trim()   }; }, 

i trying value of minimuminputlength inside data function couldn't, should use have number written twice.

update

the way able stop request throw exception inside the data function. know it's not option, works.

and found open ticket regarding issue: https://github.com/select2/select2/issues/1637 guess there no such option (yet).


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -