datalist.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $(this).click();
  38. });
  39. }
  40. const makeRequiredDatalist = (selector, required = true) => $(selector).closest('.datalist').toggleClass("required", required);
  41. //---------
  42. function setDatalistFirst(selector) {
  43. var index = 1;
  44. var elementRoot = $(selector).parents('.datalist');
  45. var num = elementRoot.find('ul li:not(.not-selectable)').length;
  46. if (index <= num) {
  47. while (elementRoot.find('ul li:nth-child(' + index + ')').hasClass("not-selectable") && index <= num) {
  48. index++;
  49. }
  50. var element = elementRoot.find('ul li:nth-child(' + index + ')');
  51. elementRoot.find('.datalist-input').text(element.html().replace(/[\t\n]+/g, ' ').trim());
  52. $(selector).val(element.data("id"));
  53. elementRoot.find("li").removeClass("selected");
  54. element.addClass("selected");
  55. }
  56. }
  57. function disableDatalist(selector, disabled = true) {
  58. var elementRoot = $(selector).parents('.datalist');
  59. if (disabled) {
  60. elementRoot.addClass("disabled");
  61. elementRoot.find('.icono').removeClass('ing-cancelar iconoAzul pointer').addClass('ing-buscar');
  62. elementRoot.find('ul').hide();
  63. } else
  64. elementRoot.removeClass("disabled");
  65. }
  66. function invalidDatalist(selector, invalid = true) {
  67. var elementRoot = $(selector).parents('.datalist');
  68. if (invalid) {
  69. elementRoot.addClass("datalist-invalid");
  70. } else
  71. elementRoot.removeClass("datalist-invalid");
  72. }
  73. //¿Se usa?
  74. function buscaDatalist(selector, valor) {
  75. selector.find('ul li').each(function () {
  76. var elem = $(this);
  77. if ($(this).parent().is('li'))
  78. elem = $(this).parent();
  79. if (!$(this).html().toUpperCase().includes(valor.toUpperCase())) {
  80. $(elem).hide();
  81. selector.find('.datalist-input').val("");
  82. selector.find("input[type=hidden]").val("");
  83. } else
  84. $(elem).show();
  85. });
  86. }
  87. function getDatalistText(selector, valor) {
  88. var elementRoot = $(selector).parents('.datalist');
  89. var text = "";
  90. $.each(elementRoot.find('ul li:not(.not-selectable)'), function () {
  91. if ($(this).data("id") == valor) {
  92. text = $(this).html();
  93. }
  94. });
  95. return text;
  96. }