stag.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (function ($) {
  2. STAG = {
  3. initSelect2RemoteSearch: function () {
  4. var select2SearchFields = $('[select2-search]');
  5. console.log({select2SearchFields});
  6. $.each(select2SearchFields, function (i, select) {
  7. select = $(select);
  8. var url = select.data('url');
  9. var text = select.data('text-prop');
  10. text = text.split('|');
  11. var id = select.data('id-prop');
  12. var uid = select.data('uid-prop');
  13. var placeholder = select.attr('placeholder');
  14. select.select2({
  15. placeholder: placeholder,
  16. minimumInputLength: 2,
  17. ajax: {
  18. type: "GET",
  19. url: url,
  20. dataType: 'json',
  21. processResults: function (response) {
  22. var data = response.data;
  23. return {
  24. results: $.map(data, function (item) {
  25. var textString = '';
  26. for (var i = 0; i < text.length; i++) {
  27. var prop = text[i];
  28. var value = item[prop];
  29. if (value) {
  30. textString = textString + value + ' ';
  31. }
  32. }
  33. return {
  34. text: textString,
  35. id: item[id]
  36. }
  37. })
  38. };
  39. }
  40. }
  41. });
  42. });
  43. },
  44. init: function () {
  45. this.initSelect2RemoteSearch();
  46. }
  47. };
  48. STAG.init();
  49. })(jQuery);