Guest User

Untitled

a guest
Jan 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. $("#selIUT").select2({
  2. cacheDataSource: [],
  3. placeholder: "Please enter the name",
  4. query: function(query) {
  5. self = this;
  6. var key = query.term;
  7. var cachedData = self.cacheDataSource[key];
  8.  
  9. if(cachedData) {
  10. query.callback({results: cachedData.result});
  11. return;
  12. } else {
  13. $.ajax({
  14. url: '/ajax/suggest/',
  15. data: { q : query.term },
  16. dataType: 'json',
  17. type: 'GET',
  18. success: function(data) {
  19. self.cacheDataSource[key] = data;
  20. query.callback({results: data.result});
  21. }
  22. })
  23. }
  24. },
  25. width: '250px',
  26. formatResult: formatResult,
  27. formatSelection: formatSelection,
  28. dropdownCssClass: "bigdrop",
  29. escapeMarkup: function (m) { return m; }
  30. });
  31.  
  32. [{"id": 0, "name": "Foo"}, {"id": 1, "name": "Bar"}]
  33.  
  34. fetchFromAPI = function() {
  35. console.log("fetchFromAPI called");
  36. var jqxhr = $.ajax(
  37. {
  38. dataType:'json',
  39. type: 'GET',
  40. url: "/services",
  41. success: function(data, textStatus, jqXHR) {
  42. services_raw = data;
  43. console.log("rosetta.fn.fetchServicesFromAPI SUCCESS");
  44. rosetta.fn.refreshServicesSelect();
  45. },
  46. error: function(jqXHR, textStatus, errorThrown) {
  47. console.log("Error inside rosetta.fn.fetchServicesFromAPI", errorThrown, textStatus, jqXHR);
  48. setTimeout(rosetta.fn.fetchServicesFromAPI(), 3000); // retry in 3 seconds
  49. }
  50. }
  51. )
  52. .done(function () {
  53. console.log("success");
  54. console.log(jqxhr);
  55. })
  56. .fail(function () {
  57. console.log("error");
  58. })
  59. .always(function () {
  60. console.log("complete");
  61. });
  62.  
  63. // Perform other work here ...
  64.  
  65. // Set another completion function for the request above
  66. jqxhr.always(function () {
  67. console.log("second complete");
  68. });
  69. };
  70.  
  71. refreshServicesSelect = function () {
  72. // ref: http://jsfiddle.net/RVnfn/2/
  73. // ref2: http://jsfiddle.net/RVnfn/101/ # mine
  74. // ref3: http://jsfiddle.net/RVnfn/102/ # also mine
  75.  
  76. console.log('refreshServicesSelect called');
  77. $("#add-service-select-service").select2({
  78. // allowClear: true
  79. data: function() {
  80. var arr = []; // container for the results we're returning to Select2 for display
  81.  
  82. for (var idx in services_raw) {
  83. var item = services_raw[idx];
  84. arr.push({
  85. id: item.id,
  86. text: item.name,
  87. _raw: item // for convenience
  88. });
  89. }
  90. return {results: arr};
  91. }
  92. });
  93. };
  94.  
  95. <input id="add-service-select-service" type="hidden" style="width:100%">
  96.  
  97. window.fetchFromAPI();
  98. window.refreshServicesSelect();
  99.  
  100. // I used an onClick event to fire the AJAX, but this can be attached to any event.
  101. // Ensure ajax call is done *ONCE* with the "one" method.
  102. $('#mySelect').one('click', function(e) {
  103. // Text to let user know data is being loaded for long requests.
  104. $('#mySelect option:eq(0)').text('Data is being loaded...');
  105. $.ajax({
  106. type: 'POST',
  107. url: '/RetrieveDropdownOptions',
  108. data: {}, // Any data that is needed to pass to the controller
  109. dataType: 'json',
  110. success: function(returnedData) {
  111. // Clear the notification text of the option.
  112. $('#mySelect option:eq(0)').text('');
  113. // Initialize the Select2 with the data returned from the AJAX.
  114. $('#mySelect').select2({ data: returnedData });
  115. // Open the Select2.
  116. $('#mySelect').select2('open');
  117. }
  118. });
  119. // Blur the select to register the text change of the option.
  120. $(this).blur();
  121. });
Add Comment
Please, Sign In to add comment