Laravel Filament表过滤器中的搜索按钮

zbdgwd5y  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(166)

你好一切都好吗?我需要帮助。我搜索了很多,到目前为止还没有找到。我在一个使用MongoDB的大型项目中使用filament,我遇到了一个问题。我有一个文本索引,如果我立即发送我想要的信息,它可以很快给我带来信息。例如,我有一个“cod”字段,如果我发送我想要的信息,它会立即返回,但是如果我键入它,每一次灯丝试图根据输入的字符进行搜索,对服务器的要求很高。鉴于此,任何人都可以帮助我一种方法,这样我就可以在过滤器中插入一个按钮,只有在点击时才能过滤表?
我的过滤器代码:

Filter::make('filter')
                ->form([
                    Grid::make(4)
                        ->schema([
                            Select::make('DocumentType')
                                ->options([
                                    'CONTRATO' => 'CONTRATO',
                                    'DOCUMENTO' => 'DOCUMENTO',
                                ])->label('Tipo de Digitalização')
                                ->reactive()
                                ->hidden(function (callable $get){
                                    return $get('FullName') || $get('Rg') || $get('Cpf') || $get('Codigo') || $get('Sequencial') || $get('date_init') || $get('date_end');
                                }),
                            
                            Forms\Components\TextInput::make('FullName')
                                ->label('Nome')
                                ->reactive()
                                ->hidden(function (callable $get){
                                    return $get('Cpf') || $get('Rg') || $get('DocumentType') || $get('Codigo') || $get('Sequencial') || $get('date_init') || $get('date_end');
                                }),

                            Forms\Components\DateTimePicker::make('date_init')
                                ->label('Digitalizado a partir de')
                                ->reactive()
                                ->hidden(function (callable $get){
                                    return $get('FullName') || $get('Rg') || $get('DocumentType') || $get('Codigo') || $get('Sequencial') || $get('Cpf');
                                }),

                            Forms\Components\DateTimePicker::make('date_end')
                                ->label('Digitalizado até')
                                ->reactive()
                                ->hidden(function (callable $get){
                                    return $get('FullName') || $get('Rg') || $get('DocumentType') || $get('Codigo') || $get('Sequencial') || $get('Cpf');
                                }),
                    ]),
                    Grid::make(4)
                        ->schema([                           
                            Forms\Components\TextInput::make('Cpf')
                                ->label('CPF')
                                ->reactive()
                                ->hidden(function (callable $get){
                                    return $get('FullName') || $get('Rg') || $get('DocumentType') || $get('Codigo') || $get('Sequencial') || $get('date_init') || $get('date_end');
                                }),

                            Forms\Components\TextInput::make('Rg')
                                ->label('RG')
                                ->reactive()
                                ->hidden(function (callable $get){
                                    return $get('FullName') || $get('Cpf') || $get('DocumentType') || $get('Codigo') || $get('Sequencial') || $get('date_init') || $get('date_end');
                                }),

                            Forms\Components\TextInput::make('Codigo')
                                ->label('Código')
                                ->reactive()
                                ->hidden(function (callable $get){
                                    return $get('FullName') || $get('Rg') || $get('DocumentType') || $get('Cpf') || $get('Sequencial') || $get('date_init') || $get('date_end');
                                }),

                            Forms\Components\TextInput::make('Sequencial')
                                ->label('Sequencial')
                                ->reactive()
                                ->hintColor('primary')
                                ->hidden(function (callable $get){
                                    return $get('FullName') || $get('Rg') || $get('DocumentType') || $get('Codigo') || $get('Cpf') || $get('date_init') || $get('date_end');
                                }),
                        ]),
                ])->query(function (Builder $query, array $data): Builder {
                    return $query
                        ->when(
                            $data['DocumentType'],
                            fn (Builder $query, $value): Builder => $query->where('DocumentType', '=', $value),
                        )->when(
                            $data['FullName'],
                            fn (Builder $query, $value): Builder => $query->whereRaw(array('$text'=>array('$search'=> "\"" . $value . "\"")))
                        )->when(
                            $data['Codigo'],
                            fn (Builder $query, $value): Builder => $query->whereRaw(array('$text'=>array('$search'=> "\"" . $value . "\"")))
                        )->when(
                            $data['Rg'],
                            fn (Builder $query, $value): Builder => $query->whereRaw(array('$text'=>array('$search'=> "\"" . $value . "\"")))
                        )->when(
                            $data['Sequencial'],
                            fn (Builder $query, $value): Builder => $query->whereRaw(array('$text'=>array('$search'=> "\"" . $value . "\"")))
                        )->when(
                            $data['Cpf'],
                            fn (Builder $query, $value): Builder => $query->whereRaw(array('$text'=>array('$search'=> "\"" . $value . "\"")))
                        )
                        ->when(
                            $data['date_init'],
                            fn (Builder $query, $value): Builder => $query->where('created_at', '>=',new DateTime($value))
                        )->when(
                            $data['date_end'],
                            fn (Builder $query, $value): Builder => $query->where('created_at', '<=',new DateTime($value))
                        );
                })

字符串

yk9xbfzb

yk9xbfzb1#

如果您不希望表单输入在更改时立即触发过滤器,请从表单输入中删除->reactive()。您需要添加一个自定义按钮,用户将单击该按钮以应用过滤器:

Filter::make('filter')
    ->form([
        // ... 

        Button::make('Apply Filters')
            ->action(function (array $data) {
                // at here apply the filter to your query.
                // and you can store the $data in the session or in some other temporary state.
            }),
    ])
    // ...

字符串

相关问题