KoolReport's Forum

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

Dashboard Table: I'd like to know how to code the search input to work for both uppercase and lowercase letters? #3473

Open Pablo Tavares opened this topic on on Feb 26 - 5 comments

Pablo Tavares commented on Feb 26

I'd like to know how to code the search input to work for both uppercase and lowercase letters?

It doesn't work when I type in lowercase, and I find that very annoying.

<?php

namespace App\Dashboard\Produtos;

use \koolreport\dashboard\widgets\Table;
use Database\AutoMaker;
use \koolreport\dashboard\fields\Text;
use koolreport\dashboard\admin\screens\ListScreen;
use \koolreport\dashboard\fields\Currency;
use koolreport\dashboard\admin\relations\HasMany;
use koolreport\dashboard\admin\Resource;
use koolreport\dashboard\fields\ID;
use koolreport\dashboard\inputs\Select;
use koolreport\dashboard\validators\NumericValidator;
use koolreport\dashboard\validators\RequiredFieldValidator;

class ProductTable extends Table
{
    protected function onInit()
    {
        $this->pageSize(5);
        $this->searchable(true);
        $this->showSearchBox(true); 
        $this->searchAlign("left");
    }

    protected function dataSource()
    {
        $selecionada = $this->sibling("ProductByLine")->selectedAssociada();
        $compradores = $this->sibling("CompradoresSelect")->value();              
        $periodoRange = $this->sibling("PeriodoRanger")->value();

        if ($selecionada === null) {
            return []; 
        }

        if (!empty($search)) {
            $query->where(function($q) use ($search) {
                $q->whereRaw("LOWER(prod.codigo) LIKE LOWER(?)", ["%{$search}%"])
                ->orWhereRaw("LOWER(prod.descricao) LIKE LOWER(?)", ["%{$search}%"])
                ->orWhereRaw("LOWER(prod.complemento_descricao) LIKE LOWER(?)", ["%{$search}%"]);
            });
        }

        return AutoMaker::table("tb_pedidos ped")
            ->join("tb_rel_itens_pedidos rip", "rip.fk_pedido", "=", "ped.id_pedido")
            ->join("tb_produtos prod", "prod.id_produto", "=", "rip.fk_produto")
            ->leftJoin("tb_clientes_associados clia", "clia.id_cliente_associado", "=", "ped.fk_cliente_associado")
            ->where("ped.fk_cliente", $this->app()->user()->others()['id_cliente'])
            ->whereIn("ped.status", ['C', 'P'])
            ->where("ped.tipo", 'N')
            ->whereRaw("rip.desaprovado IS FALSE")
            ->whereBetween('ped.data_resposta', $periodoRange)
            ->when($compradores, function($query) use ($compradores) {
                return $query->whereIn('ped.fk_usuario_cliente', $compradores); 
            })
            ->whereRaw("COALESCE(clia.nome_abreviado, 'Matriz') = ?", [$selecionada])
            ->groupBy("prod.id_produto", "prod.descricao", "prod.codigo", "prod.complemento_descricao")
            ->orderBy("total", "desc")
            ->select("prod.descricao", "prod.codigo", "prod.complemento_descricao")
            ->selectRaw("SUM((rip.dados_cotacao->>'preco')::decimal(12,5) * rip.quantidade) AS total");
    }

    protected function fields()
    {
        return [
            Text::create("codigo")
                ->label("Cód.")
                ->searchable(true),

            Text::create("descricao")
                ->label("Descrição")
                ->searchable(true),

            Text::create("complemento_descricao")
                ->label("Complemento")
                ->searchable(true),

            Currency::create("total")
                ->label("Total R$")
                ->BRL()
                ->symbol()
        ];
    }
}
Sebastian Morales commented on Mar 3

That meant your database data is case sensitive. Let us know your database type (MySQL, SQL Server, PostgreSQL, etc) for us to see if there is a way to make its search case insensitive for you.

Pablo Tavares commented on Mar 3

my database is Mysql

Sebastian Morales commented on Mar 4

If you use your MySQL admin interfaces such as command line or phpMyAdmin, does your database sql query search case sensitively or insensitively?

Pablo Tavares commented on Mar 4

I'm sorry, I answered incorrectly our database uses PostgreSQL.

Sebastian Morales commented on Mar 5

So PosgreSQL is case sensitive by default. I guess you can either make your PostgreSQL database case insensitive (pretty significant change) or try the following quick workaround:

  1. Open the file {path to}/koolreport/dashboard/data/SQLHandler.php and replace this line:
    $query->{$whereMethod}($field ,"LIKE","%".$search['text']."%");

with this one:

    $query->{$whereMethod}("lower(" . $field . ")" ,"LIKE","%".strtolower($search['text'])."%");

In the meantime we will find a nicer option to make Dashboard Table search work case insensitively.

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

Inputs