Commit df7e0b44 authored by Alfonso Rafael Solis Rangel's avatar Alfonso Rafael Solis Rangel
Browse files

Funciones para buscar sin necesidad de poner acentos

parent 368e8ed8
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -85,7 +85,18 @@ public function find(Request $request)
    {
        $query = $request->input('q');

        $contactos = Contacto::where('nombre', 'like', '%' . $query . '%')->get(['id', 'nombre', 'ap_paterno', 'ap_materno']);
        $keywords = preg_split('/\s+/', $query);

        $contactos = Contacto::where(function($q) use ($keywords){
            foreach($keywords as $word){
                $q->where(function($subQuery) use ($word){
                    $subQuery->where('nombre', 'LIKE', "%$word%")
                        ->orWhere('ap_paterno', 'LIKE', "%$word%")
                        ->orWhere('ap_materno', 'LIKE', "%$word%");
                });
            }
            
        })->get(['id', 'nombre', 'ap_paterno', 'ap_materno']);
        return response()->json($contactos);
    }

+32 −0
Original line number Diff line number Diff line
@@ -75,6 +75,32 @@
    </style>

    <script>
        function aplicarBusquedaNormalizada(niceSelectEl){
            const input = niceSelectEl.querySelector('.nice-select-search');

            if(input){
                function normalizarTexto(texto){
                    return texto.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
                }

                input.addEventListener('keydown', function (e) {
                    if (e.key === ' ') {
                        e.stopPropagation();
                    }
                });

                input.addEventListener('input', function () {
                    const search = normalizarTexto(this.value);
                    const options = niceSelectEl.querySelectorAll('.option');

                    options.forEach(option => {
                        const optionText = normalizarTexto(option.textContent);
                        option.style.display = optionText.includes(search) ? '' : 'none';
                    });
                });
            }
        }

        document.addEventListener("DOMContentLoaded", function(e) {
            // default
            var els = document.querySelectorAll(".selectize");
@@ -89,6 +115,12 @@
            NiceSelect.bind(document.getElementById("grupo_id"), options);
            NiceSelect.bind(document.getElementById("subgrupo_id"), options);

            setTimeout(()=> {
                document.querySelectorAll('.nice-select').forEach(select => {
                    aplicarBusquedaNormalizada(select);
                });
            }, 100);

            document.querySelectorAll('.nice-select .current').forEach(el => {
                if (el.textContent.trim() === 'Select an option') {
                    el.textContent = 'Selecciona una opción';
+32 −0
Original line number Diff line number Diff line
@@ -75,6 +75,32 @@
    </style>

    <script>
        function aplicarBusquedaNormalizada(niceSelectEl){
            const input = niceSelectEl.querySelector('.nice-select-search');

            if(input){
                function normalizarTexto(texto){
                    return texto.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
                }

                input.addEventListener('keydown', function (e) {
                    if (e.key === ' ') {
                        e.stopPropagation();
                    }
                });

                input.addEventListener('input', function () {
                    const search = normalizarTexto(this.value);
                    const options = niceSelectEl.querySelectorAll('.option');

                    options.forEach(option => {
                        const optionText = normalizarTexto(option.textContent);
                        option.style.display = optionText.includes(search) ? '' : 'none';
                    });
                });
            }
        }

        document.addEventListener("DOMContentLoaded", function(e) {
            var els = document.querySelectorAll(".selectize");
            els.forEach(function(select) {
@@ -87,6 +113,12 @@
            NiceSelect.bind(document.getElementById("grupo_id"), options);
            NiceSelect.bind(document.getElementById("subgrupo_id"), options);

            setTimeout(()=> {
                document.querySelectorAll('.nice-select').forEach(select => {
                    aplicarBusquedaNormalizada(select);
                });
            }, 100);

            document.querySelectorAll('.nice-select .current').forEach(el => {
                if (el.textContent.trim() === 'Select an option') {
                    el.textContent = 'Selecciona una opción';
+57 −8
Original line number Diff line number Diff line
@@ -440,6 +440,37 @@
    <script src="/assets/js/jquery.toast.js"></script>

    <script>
        function aplicarBusquedaNormalizada(niceSelectEl){
            const input = niceSelectEl.querySelector('.nice-select-search');

            if(input){
                function normalizarTexto(texto){
                    return texto.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
                }

                input.addEventListener('keydown', function (e) {
                    if (e.key === ' ') {
                        e.stopPropagation();
                    }
                });

                input.addEventListener('input', function () {
                    const search = normalizarTexto(this.value);
                    const options = niceSelectEl.querySelectorAll('.option');

                    options.forEach(option => {
                        const optionText = normalizarTexto(option.textContent);
                        option.style.display = optionText.includes(search) ? '' : 'none';
                    });
                });
            }
        }

        // seachable
        var options = {
            searchable: true
        };

        document.addEventListener("DOMContentLoaded", function(e) {
            // default
            var els = document.querySelectorAll(".selectize");
@@ -447,14 +478,16 @@
                NiceSelect.bind(select);
            });

            // seachable
            var options = {
                searchable: true
            };
            NiceSelect.bind(document.getElementById("conyuge_id"), options);
            NiceSelect.bind(document.getElementById("profesion_id"), options);
            NiceSelect.bind(document.getElementById("cargo_id"), options);

            setTimeout(()=> {
                document.querySelectorAll('.nice-select').forEach(select => {
                    aplicarBusquedaNormalizada(select);
                });
            }, 100);

            document.querySelectorAll('.nice-select .current').forEach(el => {
                if (el.textContent.trim() === 'Select an option') {
                    el.textContent = 'Selecciona una opción';
@@ -616,10 +649,11 @@

        agregarBtnSubgrupo.addEventListener('click', function(){
            const newFormGroup = document.createElement('div');
            const newSelectSubgrupo = `subgrupo_id_${Date.now()}`;
            newFormGroup.innerHTML = `
                    <div class="mb-5">
                        <label for="subgrupo_id">Subgrupo</label>
                        <select id="subgrupo_id" name="subgrupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                        <label for="${newSelectSubgrupo}">Subgrupo</label>
                        <select id="${newSelectSubgrupo}" name="subgrupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                            <option value="null" selected>Selecciona una opción</option>
                            @foreach($subgrupos as $subgrupo)
                                <option value="{{ $subgrupo->id }}">{{ $subgrupo->nombre }}</option>
@@ -628,14 +662,22 @@
                    </div>
            `;
            dynamicFormContainerSubgrupos.appendChild(newFormGroup);
            NiceSelect.bind(document.getElementById(newSelectSubgrupo), options);
            setTimeout(()=>{
                const nuevoNiceSelect = document.querySelector(`#${newSelectSubgrupo}`).nextElementSibling;
                if(nuevoNiceSelect && nuevoNiceSelect.classList.contains('nice-select')){
                    aplicarBusquedaNormalizada(nuevoNiceSelect);
                }
            }, 50);
        });

        agregarBtnGrupo.addEventListener('click', function(){
            const newFormGroup = document.createElement('div');
            const newSelectGrupo = `grupo_id_${Date.now()}`;
            newFormGroup.innerHTML = `
                <div class="mb-5">
                    <label for="grupo_id">Grupo</label>
                    <select id="grupo_id" name="grupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                    <label for="${newSelectGrupo}">Grupo</label>
                    <select id="${newSelectGrupo}" name="grupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                        <option value="null" selected>Selecciona una opción</option>
                        @foreach($grupos as $grupo)
                            <option value="{{ $grupo->id }}">{{ $grupo->nombre }}</option>
@@ -644,6 +686,13 @@
                </div>
            `;
            dynamicFormContainerGrupos.appendChild(newFormGroup);
            NiceSelect.bind(document.getElementById(newSelectGrupo), options);
            setTimeout(()=>{
                const nuevoNiceSelect = document.querySelector(`#${newSelectGrupo}`).nextElementSibling;
                if(nuevoNiceSelect && nuevoNiceSelect.classList.contains('nice-select')){
                    aplicarBusquedaNormalizada(nuevoNiceSelect);
                }
            }, 50);
        });

        // Añadir evento de clic para eliminar a los grupos iniciales
+57 −8
Original line number Diff line number Diff line
@@ -498,6 +498,37 @@
    <script src="/assets/js/jquery.toast.js"></script>

    <script>
        function aplicarBusquedaNormalizada(niceSelectEl){
            const input = niceSelectEl.querySelector('.nice-select-search');

            if(input){
                function normalizarTexto(texto){
                    return texto.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
                }

                input.addEventListener('keydown', function (e) {
                    if (e.key === ' ') {
                        e.stopPropagation();
                    }
                });

                input.addEventListener('input', function () {
                    const search = normalizarTexto(this.value);
                    const options = niceSelectEl.querySelectorAll('.option');

                    options.forEach(option => {
                        const optionText = normalizarTexto(option.textContent);
                        option.style.display = optionText.includes(search) ? '' : 'none';
                    });
                });
            }
        }

        // seachable
        var options = {
            searchable: true
        };

        document.addEventListener("DOMContentLoaded", function(e) {
            // default
            var els = document.querySelectorAll(".selectize");
@@ -505,14 +536,16 @@
                NiceSelect.bind(select);
            });

            // seachable
            var options = {
                searchable: true
            };
            
            NiceSelect.bind(document.getElementById("conyuge_id"), options);
            NiceSelect.bind(document.getElementById("profesion_id"), options);
            NiceSelect.bind(document.getElementById("cargo_id"), options);
            NiceSelect.bind(document.getElementById("subgrupo_id"), options);
            setTimeout(()=> {
                document.querySelectorAll('.nice-select').forEach(select => {
                    aplicarBusquedaNormalizada(select);
                });
            }, 100);

            document.querySelectorAll('.nice-select .current').forEach(el => {
                if (el.textContent.trim() === 'Select an option') {
@@ -675,10 +708,11 @@

        agregarBtnSubgrupo.addEventListener('click', function(){
            const newFormGroup = document.createElement('div');
            const newSelectSubgrupo = `subgrupo_id_${Date.now()}`;
            newFormGroup.innerHTML = `
                    <div class="mb-5">
                        <label for="subgrupo_id">Subgrupo</label>
                        <select id="subgrupo_id" name="subgrupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                        <label for="${newSelectSubgrupo}">Subgrupo</label>
                        <select id="${newSelectSubgrupo}" name="subgrupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                            <option value="null" selected>Selecciona una opción</option>
                            @foreach($subgrupos as $subgrupo)
                                <option value="{{ $subgrupo->id }}">{{ $subgrupo->nombre }}</option>
@@ -687,14 +721,22 @@
                    </div>
            `;
            dynamicFormContainerSubgrupos.appendChild(newFormGroup);
            NiceSelect.bind(document.getElementById(newSelectSubgrupo), options);
            setTimeout(()=>{
                const nuevoNiceSelect = document.querySelector(`#${newSelectSubgrupo}`).nextElementSibling;
                if(nuevoNiceSelect && nuevoNiceSelect.classList.contains('nice-select')){
                    aplicarBusquedaNormalizada(nuevoNiceSelect);
                }
            }, 50);
        });

        agregarBtnGrupo.addEventListener('click', function(){
            const newFormGroup = document.createElement('div');
            const newSelectGrupo = `grupo_id_${Date.now()}`;
            newFormGroup.innerHTML = `
                <div class="mb-5">
                    <label for="grupo_id">Grupo</label>
                    <select id="grupo_id" name="grupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                    <label for="${newSelectGrupo}">Grupo</label>
                    <select id="${newSelectGrupo}" name="grupo_id[]" class="block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                        <option value="null" selected>Selecciona una opción</option>
                        @foreach($grupos as $grupo)
                            <option value="{{ $grupo->id }}">{{ $grupo->nombre }}</option>
@@ -703,6 +745,13 @@
                </div>
            `;
            dynamicFormContainerGrupos.appendChild(newFormGroup);
            NiceSelect.bind(document.getElementById(newSelectGrupo), options);
            setTimeout(()=>{
                const nuevoNiceSelect = document.querySelector(`#${newSelectGrupo}`).nextElementSibling;
                if(nuevoNiceSelect && nuevoNiceSelect.classList.contains('nice-select')){
                    aplicarBusquedaNormalizada(nuevoNiceSelect);
                }
            }, 50);
        });

        // Añadir evento de clic para eliminar a los grupos iniciales
Loading