Advertisement
NecromancerCoding

Ordenar dados alfabéticamente

Aug 19th, 2024 (edited)
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.01 KB | None | 0 0
  1. function sortSelectOptions(selectElement) {
  2. $(selectElement).each(function(){
  3. var options = $(this).find('option').toArray();
  4. options.sort(function(a, b) {
  5. if (a.value === "") return -1;
  6. return a.text.localeCompare(b.text);
  7. });
  8. $(this).empty().append(options);
  9. if ($(this).find('option[selected="selected"]').length === 0) {
  10. $(this).find('option[value=""]').attr('selected', 'selected');
  11. }
  12. });
  13. }
  14.  
  15. function sortNewSelectOptions(selectElement) {
  16. $(selectElement).each(function(){
  17. var options = $(this).find('option').toArray();
  18. options.sort(function(a, b) {
  19. if (a.value === "") return -1;
  20. return a.text.localeCompare(b.text);
  21. });
  22. $(this).empty().append(options);
  23. $(this).find('option[value=""]').attr('selected', 'selected');
  24. });
  25. }
  26.  
  27. $(function() {
  28. sortSelectOptions('select[name^="post_dice"]');
  29. });
  30.  
  31. $(document).on('click', '[href^="javascript:add_dice"]', function() {
  32. setTimeout(function() {
  33. $('select[name^="post_dice"][value=""]').each(function() {
  34. sortNewSelectOptions(this);
  35. });
  36. }, 100);
  37. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement