Skip to content

Commit b505601

Browse files
ErmishErmish
authored andcommitted
Added Ajax Error Handling
Added an error message when an ajax error occurs. This can be overridden through the select2 option “formatAjaxError”.
1 parent fdad112 commit b505601

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

select2.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ html[dir="rtl"] .select2-results {
422422
color: #000;
423423
}
424424

425-
426425
.select2-results .select2-no-results,
427426
.select2-results .select2-searching,
427+
.select2-results .select2-ajax-error,
428428
.select2-results .select2-selection-limit {
429429
background: #f4f4f4;
430430
display: list-item;
@@ -454,6 +454,10 @@ disabled look for disabled choices in the results dropdown
454454
background: #f4f4f4 url('select2-spinner.gif') no-repeat 100%;
455455
}
456456

457+
.select2-results .select2-ajax-error {
458+
background: rgba(255, 50, 50, .2);
459+
}
460+
457461
.select2-more-results {
458462
background: #f4f4f4;
459463
display: list-item;

select2.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@ the specific language governing permissions and limitations under the Apache Lic
443443
// TODO - replace query.page with query so users have access to term, page, etc.
444444
// added query as third paramter to keep backwards compatibility
445445
var results = options.results(data, query.page, query);
446+
query.callback(results);
447+
},
448+
error: function(jqXHR, textStatus, errorThrown){
449+
var results = {
450+
hasError: true,
451+
jqXHR: jqXHR,
452+
textStatus: textStatus,
453+
errorThrown: errorThrown,
454+
};
455+
446456
query.callback(results);
447457
}
448458
});
@@ -1766,6 +1776,12 @@ the specific language governing permissions and limitations under the Apache Lic
17661776
return;
17671777
}
17681778

1779+
// handle ajax error
1780+
if(data.hasError !== undefined && checkFormatter(opts.formatAjaxError, "formatAjaxError")) {
1781+
render("<li class='select2-ajax-error'>" + evaluate(opts.formatAjaxError, opts.element, data.jqXHR, data.textStatus, data.errorThrown) + "</li>");
1782+
return;
1783+
}
1784+
17691785
// save context, if any
17701786
this.context = (data.context===undefined) ? null : data.context;
17711787
// create a default choice and prepend it to the list
@@ -3408,6 +3424,7 @@ the specific language governing permissions and limitations under the Apache Lic
34083424
formatSelectionCssClass: function(data, container) {return undefined;},
34093425
formatMatches: function (matches) { if (matches === 1) { return "One result is available, press enter to select it."; } return matches + " results are available, use up and down arrow keys to navigate."; },
34103426
formatNoMatches: function () { return "No matches found"; },
3427+
formatAjaxError: function (jqXHR, textStatus, errorThrown) { return "Loading failed"; },
34113428
formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " or more character" + (n == 1? "" : "s"); },
34123429
formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1? "" : "s"); },
34133430
formatSelectionTooBig: function (limit) { return "You can only select " + limit + " item" + (limit == 1 ? "" : "s"); },

0 commit comments

Comments
 (0)