KoolReport's Forum

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

Select 2 - dashboard - big database #3331

Open cyberweb opened this topic on on Sep 4 - 4 comments

cyberweb commented on Sep 4

in the dashboard, how do you make a select 2 using remote data on a large database?

KoolReport commented on Sep 5

Connect select2 using datasource to large database is not different from small database. However due to the size of database the query may take time. You may use Cache for datasource to improve the speed of application.

Sebastian Morales commented on Sep 5

You can try the "minimumInputLength" option to prevent Select2 to show options all at once:

                        Select2::create(array(
                            ...
                            "options" => [
                                ...
                                "minimumInputLength" => 2,
                            ]
                        )); 

There is also an ajax property if loading all data is too slow.

cyberweb commented on Sep 5

I'm trying this one

  Row::create([
                    [
                        Html::label("Choix du signataire")->style("font-weight:bold"),
                        Select2::create('selectSignataire')
                            ->placeHolder("Choisir un signataire")
                            ->dataSource(function ($params = null) {
                                if (isset($params['search']) && !empty($params['search'])) {
                                    $keyword = $params['search'];
                                    return AdminAutoMaker::table('clients')
                                        ->select('id', 'CONCAT(prenom, " ", nom, " ", societe) as name')
                                        ->whereRaw('CONCAT(prenom, " ", nom, " ", societe) LIKE ?', ["%$keyword%"])
                                        ->limit(10)
                                        ->run();  
                                }
                                return [];
                            })
                            ->multiple(false)
                            ->fields([
                                Number::create('id'),
                                Text::create('name'),
                            ])->options([
                                'minimumInputLength' => 1,
                            ])->cssStyle("width:100%;"),
                    ],
                ]),

But no result, i'm trying to call the data but not all table, but not work

Sebastian Morales commented on Sep 6

Maybe the options property is overwritten by a default empty array value after it is created. I would suggest you create class called MySelect2 like this:

class MySelect2 extends \koolreport\dashboard\inputs\Select2
{
    protected function onInit()
    {
        $this
        ...
        ->options([
            ...
            'minimumInputLength' => 2,
        ])
        ;
    } 

    protected function dataSource()
    {
        return ...;
    }

Then use this MySelect2 class in your dashboard intead of a direct Select2 create.

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
help needed

Dashboard