Advertisement
gallopelado

Configuracion del autocomplete 1

Mar 11th, 2021
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #addMedicamentoAutoComplete(targetId, medicamentoList) {
  2.         let campo = null;
  3.         const t=this, dom = document;
  4.         if(document.querySelector(`#${targetId}`) === null) {throw 'could not get the selector of the nombre_medicamento column'};
  5.         document.querySelector(`#${targetId}`).addEventListener("autoComplete", event => {
  6.             campo = event.target;
  7.         });
  8.         new autoComplete({
  9.             data: {
  10.                 src: medicamentoList,
  11.                 key: ['codigo_medicamento'],
  12.                 cache: false
  13.             },
  14.             noResults: () => {
  15.                 Notiflix.Report.Failure('Error', 'No existe Medicamento', 'Cerrar este aviso', function() {
  16.                     campo.value = '';
  17.                 });
  18.             },
  19.             onSelection: feedback => {
  20.                 let selectedItemList = [];
  21.                 const selection = feedback.selection.value.codigo_medicamento.split(' | ');
  22.                 $('input[name=codigo_medicamento]').each(function(k,v){
  23.                     if($(v).val()){
  24.                         selectedItemList.push($(v).val());
  25.                     }
  26.                 });
  27.                 if(selectedItemList.includes(selection[0])){
  28.                     Notiflix.Report.Failure( 'Medicamento repetido', 'Este medicamento ya existe', 'Cerrar', function(){
  29.                         campo.value="";
  30.                         campo.focus();
  31.                     });
  32.                     return;
  33.                 }
  34.                 const tbody = $('#tabla_prescripcion tbody');
  35.                 // put the values to the corresponded input values
  36.                 campo.parentElement.parentElement.children[0].children[0].value = selection[0]; // codigo_medicamento
  37.                 campo.value = selection[1]; // nombre_medicamento
  38.                 campo.parentElement.parentElement.children[2].children[0].value = selection[2]; // forma_farmaceutica
  39.                 campo.parentElement.parentElement.children[4].children[0].value = selection[3]; // concentracion
  40.                 // disable the current field and unset id
  41.                 campo.disabled = true;
  42.                 campo.id="";
  43.                 //lista automatica del autocomplete
  44.                 campo.parentElement.children[1].innerHTML="";
  45.                 campo.parentElement.children[1].id="";
  46.                 // Revisar despues, esta seccion clona los nodos del tbody sin los eventos(autocomplete, selected)
  47.                 // Pero se debe recuperar los eventos del select node
  48.                 //const clon = tbody.children().clone();
  49.                 //tbody.html(clon);
  50.                 tbody.append(empty_row_medicacion_pre_anestesica(t.#via_list));
  51.                 t.#addMedicamentoAutoComplete("crear_receta_nombre_medicamento", t.#medicamentoList);
  52.             },
  53.             maxResults: 20,
  54.             selector: `#${targetId}`,
  55.             threshold: 1,
  56.             debounce: 300,
  57.             searchEngine: "strict",
  58.             highlight: true,
  59.             resultsList: {
  60.                 render: true,
  61.                 container: source => {
  62.                     source.setAttribute("id", "autoComplete_list");
  63.                 },
  64.                 destination: document.querySelector(`#${targetId}`),
  65.                 position: "afterend",
  66.                 element: "ul"
  67.             },
  68.             resultItem: {
  69.                 content: (data, source) => {
  70.                     source.innerHTML = data.match;
  71.                 },
  72.                 element: "li"
  73.             }
  74.         });
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement