KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Input multiple Select2 for 3 digits #3471

Open Pablo Tavares opened this topic on on Feb 20 - 3 comments

Pablo Tavares commented on Feb 20

I'm trying to create an input multiple so that it only loads data when I have typed 3 characters, but it's not working, can anyone help me quickly?

Select2

class ProdutosSelect extends Select2
{
    protected function onInit()
    {
        $this->multiple(true);

        $this->options([
            // "language" => [
            //     "inputTooShort" => "Digite pelo menos 3 caracteres...",
            //     "searching" => "Buscando produtos...",
            //     "noResults" => "Nenhum produto encontrado"
            // ],
                "minimumInputLength" => 3,
            "ajax" => [
                "delay" => 400,
            ],
            
        ]);
        
    }   

    protected function dataSource()
    {

    if (!$this->value()) {
        return [];
    }

        return AutoMaker::rawSQL("
            SELECT 
                codigo as value, 
                CONCAT(codigo, ' - ', descricao) AS label
            FROM tb_produtos 
            WHERE fk_cliente = :idCliente
            AND ativo IS TRUE
            AND (
                codigo ILIKE :search
                OR descricao ILIKE :search
            )
            LIMIT 50
        ")
        ->params([
            ":idCliente" => $this->app()->user()->others()['id_cliente'],
            ":search" => "%{$search}%"
        ]);
    }

    protected function fields()
    {
        return [
            Text::create("label"),
            Text::create("value"),
        ];
    }

    protected function actionChange($request, $response)
    {
        // $this->sibling("QuantidadeRenegociacao")->update();
    }
}
Sebastian Morales commented on Feb 26

Pls try removing the ajax option in onInit function this:

            // "ajax" => [
            //     "delay" => 400,
            // ],

and see if it solves the problem. Let us know the result. Rgds,

Pablo Tavares commented on Feb 26

the result is that it did not solve my problem, what would you do to solve this problem

<?php

namespace App\Dashboard\Categorias;

use \koolreport\dashboard\inputs\Select2;
use \koolreport\dashboard\fields\Text;
use \Database\AutoMaker;

class ProdutosSelect extends Select2
{
    protected function onInit()
    {
        $this->multiple(true);

        $this->options([
            "minimumInputLength" => 3,
            "delay" => 400,
            "language" => [
                "inputTooShort" => "Digite pelo menos 3 caracteres...",
                "searching" => "Buscando produtos...",
                "noResults" => "Nenhum produto encontrado"
            ]
        ]);
    }

    protected function dataSource()
    {
        $search = $this->params()['search'] ?? '';

        if (strlen($search) < 3) {
            return [];
        }

        return AutoMaker::rawSQL("
            SELECT 
                CONCAT(codigo, ' - ', descricao) AS label,
                id_produto AS value
            FROM tb_produtos 
            WHERE fk_cliente = :idCliente
            AND ativo IS TRUE
            AND (
                codigo ILIKE :search
                OR descricao ILIKE :search
            )
            ORDER BY descricao
            LIMIT 50
        ")
        ->params([
            ":idCliente" => $this->app()->user()->others()['id_cliente'],
            ":search" => "%{$search}%"
        ]);
    }

    protected function fields()
    {
        return [
            Text::create("label"),
            Text::create("value"),
        ];
    }
}

Sebastian Morales commented on Feb 27

Pls use js function string to return custom language text like this:

                "language" => [
                    "inputTooShort" => "function() {
                        return 'Digite pelo menos 3 caracteres...';
                    }",
                    ...

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
bug
help needed

Inputs