datalist.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. $(function () {
  2. const toggleIcon = (el, fromClass, toClass) => $(el).removeClass(fromClass).addClass(toClass);
  3. const ocultaTodos = () => {
  4. toggleIcon('.datalist .icono', 'ing-cancelar iconoAzul pointer', 'ing-buscar');
  5. $('.datalist ul').hide();
  6. };
  7. $(document)
  8. .on('click', '.datalist-input,.icono', function () {
  9. const parent = $(this).parent();
  10. $(".datalist ul:visible").not(parent.find('ul')).siblings('.datalist-input').trigger('click');
  11. if (parent.find('ul').is(':visible') || parent.hasClass("disabled")) return ocultaTodos();
  12. parent.find('ul, .datalist-input').show();
  13. toggleIcon(parent.find('.icono'), 'ing-buscar', 'ing-cancelar iconoAzul pointer');
  14. })
  15. .on('click', '.datalist-select > ul li:not(.not-selectable)', function () {
  16. const parent = $(this).closest('.datalist');
  17. parent.find('.datalist-input').text($(this).text().trim());
  18. parent.find("input[type=hidden]").val($(this).data('id'));
  19. $('.datalist li').removeClass("selected");
  20. $(this).addClass("selected");
  21. parent.removeClass("datalist-invalid");
  22. ocultaTodos();
  23. })
  24. .on('click', (e) => {
  25. if (!$(e.target).closest('.datalist').length) ocultaTodos();
  26. });
  27. $('.modal').on('hide.bs.modal', ocultaTodos);
  28. });
  29. const setDatalist = (selector, value = -1) => {
  30. const parent = $(selector).closest('.datalist');
  31. parent.find('ul li:not(.not-selectable)').each(function () {
  32. if ($(this).data("id") !== value) return;
  33. parent.find('.datalist-input').text($(this).text().trim());
  34. $(selector).val(value);
  35. $('.datalist li').removeClass("selected");
  36. $(this).addClass("selected");
  37. });
  38. }
  39. const makeRequiredDatalist = (selector, required = true) => $(selector).closest('.datalist').toggleClass("required", required);
  40. //---------
  41. function setDatalistFirst(selector) {
  42. var index = 1;
  43. var elementRoot = $(selector).parents('.datalist');
  44. var num = elementRoot.find('ul li:not(.not-selectable)').length;
  45. if (index <= num) {
  46. while (elementRoot.find('ul li:nth-child(' + index + ')').hasClass("not-selectable") && index <= num) {
  47. index++;
  48. }
  49. var element = elementRoot.find('ul li:nth-child(' + index + ')');
  50. elementRoot.find('.datalist-input').text(element.html().replace(/[\t\n]+/g, ' ').trim());
  51. $(selector).val(element.data("id"));
  52. elementRoot.find("li").removeClass("selected");
  53. element.addClass("selected");
  54. }
  55. }
  56. function disableDatalist(selector, disabled = true) {
  57. var elementRoot = $(selector).parents('.datalist');
  58. if (disabled) {
  59. elementRoot.addClass("disabled");
  60. elementRoot.find('.icono').removeClass('ing-cancelar iconoAzul pointer').addClass('ing-buscar');
  61. elementRoot.find('ul').hide();
  62. } else
  63. elementRoot.removeClass("disabled");
  64. }
  65. function invalidDatalist(selector, invalid = true) {
  66. var elementRoot = $(selector).parents('.datalist');
  67. if (invalid) {
  68. elementRoot.addClass("datalist-invalid");
  69. } else
  70. elementRoot.removeClass("datalist-invalid");
  71. }
  72. function buscaDatalist(selector, valor) {
  73. selector.find('ul li').each(function () {
  74. var elem = $(this);
  75. if ($(this).parent().is('li'))
  76. elem = $(this).parent();
  77. if (!$(this).html().toUpperCase().includes(valor.toUpperCase())) {
  78. $(elem).hide();
  79. selector.find('.datalist-input').val("");
  80. selector.find("input[type=hidden]").val("");
  81. } else
  82. $(elem).show();
  83. });
  84. }
  85. function getDatalistText(selector, valor) {
  86. var elementRoot = $(selector).parents('.datalist');
  87. var text = "";
  88. $.each(elementRoot.find('ul li:not(.not-selectable)'), function () {
  89. if ($(this).data("id") == valor) {
  90. text = $(this).html();
  91. }
  92. });
  93. return text;
  94. }