Skip to content

Commit

Permalink
Merge pull request #1374 from suraj-webkul/datagrid-dropdown-searchab…
Browse files Browse the repository at this point in the history
…le-filter

datagrid searchable dropdown.
  • Loading branch information
jitendra-webkul authored Aug 20, 2024
2 parents 7f50f9c + 307a4a4 commit f712c28
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 39 deletions.
44 changes: 31 additions & 13 deletions packages/Webkul/Admin/src/DataGrids/Activity/ActivityDataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Support\Facades\DB;
use Webkul\Admin\Traits\ProvideDropdownOptions;
use Webkul\DataGrid\DataGrid;
use Webkul\Lead\Repositories\LeadRepository;
use Webkul\User\Repositories\UserRepository;

class ActivityDataGrid extends DataGrid
{
Expand Down Expand Up @@ -42,7 +44,7 @@ public function prepareQueryBuilder(): Builder
$this->addFilter('title', 'activities.title');
$this->addFilter('schedule_from', 'activities.schedule_from');
$this->addFilter('created_by', 'users.name');
$this->addFilter('created_by_id', 'activities.user_id');
$this->addFilter('created_by_id', 'users.name');
$this->addFilter('created_at', 'activities.created_at');
$this->addFilter('lead_title', 'leads.title');

Expand Down Expand Up @@ -82,12 +84,20 @@ public function prepareColumns(): void
]);

$this->addColumn([
'index' => 'created_by_id',
'label' => trans('admin::app.activities.index.datagrid.created_by'),
'type' => 'string',
'searchable' => false,
'sortable' => true,
'filterable' => true,
'index' => 'created_by_id',
'label' => trans('admin::app.activities.index.datagrid.created_by'),
'type' => 'string',
'searchable' => false,
'sortable' => true,
'filterable' => true,
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => UserRepository::class,
'column' => [
'label' => 'name',
'value' => 'name',
],
],
'closure' => function ($row) {
$route = urldecode(route('admin.settings.users.index', ['id[eq]' => $row->created_by_id]));

Expand All @@ -102,12 +112,20 @@ public function prepareColumns(): void
]);

$this->addColumn([
'index' => 'lead_title',
'label' => trans('admin::app.activities.index.datagrid.lead'),
'type' => 'string',
'searchable' => true,
'filterable' => true,
'sortable' => true,
'index' => 'lead_title',
'label' => trans('admin::app.activities.index.datagrid.lead'),
'type' => 'string',
'searchable' => true,
'sortable' => true,
'filterable' => true,
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => LeadRepository::class,
'column' => [
'label' => 'title',
'value' => 'title',
],
],
'closure' => function ($row) {
if ($row->lead_title == null) {
return "<span class='text-gray-800 dark:text-gray-300'>N/A</span>";
Expand Down
10 changes: 8 additions & 2 deletions packages/Webkul/Admin/src/DataGrids/Contact/PersonDataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ public function prepareColumns(): void
'searchable' => true,
'filterable' => true,
'sortable' => true,
'filterable_type' => 'dropdown',
'filterable_options' => $this->organizationRepository->all(['name as label', 'name as value'])->toArray(),
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => OrganizationRepository::class,
'column' => [
'label' => 'name',
'value' => 'name',
],
],
]);
}

Expand Down
44 changes: 32 additions & 12 deletions packages/Webkul/Admin/src/DataGrids/Lead/LeadDataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public function prepareQueryBuilder(): Builder

$this->addFilter('id', 'leads.id');
$this->addFilter('user', 'leads.user_id');
$this->addFilter('sales_person', 'leads.user_id');
$this->addFilter('lead_source_name', 'lead_sources.id');
$this->addFilter('sales_person', 'users.name');
$this->addFilter('lead_source_name', 'lead_sources.name');
$this->addFilter('person_name', 'persons.name');
$this->addFilter('type', 'lead_pipeline_stages.code');
$this->addFilter('stage', 'lead_pipeline_stages.id');
Expand Down Expand Up @@ -118,8 +118,14 @@ public function prepareColumns(): void
'searchable' => false,
'sortable' => true,
'filterable' => true,
'filterable_type' => 'dropdown',
'filterable_options' => $this->userRepository->all(['name as label', 'id as value'])->toArray(),
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => \Webkul\Contact\Repositories\PersonRepository::class,
'column' => [
'label' => 'name',
'value' => 'name',
],
],
]);

$this->addColumn([
Expand All @@ -137,8 +143,14 @@ public function prepareColumns(): void
'searchable' => false,
'sortable' => true,
'filterable' => true,
'filterable_type' => 'dropdown',
'filterable_options' => $this->sourceRepository->all(['name as label', 'id as value'])->toArray(),
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => SourceRepository::class,
'column' => [
'label' => 'name',
'value' => 'name',
],
],
]);

$this->addColumn([
Expand All @@ -150,12 +162,20 @@ public function prepareColumns(): void
]);

$this->addColumn([
'index' => 'person_name',
'label' => trans('admin::app.leads.index.datagrid.contact-person'),
'type' => 'string',
'searchable' => false,
'sortable' => true,
'filterable' => true,
'index' => 'person_name',
'label' => trans('admin::app.leads.index.datagrid.contact-person'),
'type' => 'string',
'searchable' => false,
'sortable' => true,
'filterable' => true,
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => \Webkul\Contact\Repositories\PersonRepository::class,
'column' => [
'label' => 'name',
'value' => 'name',
],
],
]);

$this->addColumn([
Expand Down
40 changes: 28 additions & 12 deletions packages/Webkul/Admin/src/DataGrids/Quote/QuoteDataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function prepareQueryBuilder(): Builder

$this->addFilter('id', 'quotes.id');
$this->addFilter('user', 'quotes.user_id');
$this->addFilter('sales_person', 'quotes.user_id');
$this->addFilter('sales_person', 'users.name');
$this->addFilter('person_name', 'persons.name');
$this->addFilter('expired_at', 'quotes.expired_at');
$this->addFilter('created_at', 'quotes.created_at');
Expand Down Expand Up @@ -67,11 +67,19 @@ public function prepareColumns(): void
]);

$this->addColumn([
'index' => 'sales_person',
'label' => trans('admin::app.quotes.index.datagrid.sales-person'),
'type' => 'string',
'sortable' => true,
'filterable' => true,
'index' => 'sales_person',
'label' => trans('admin::app.quotes.index.datagrid.sales-person'),
'type' => 'string',
'sortable' => true,
'filterable' => true,
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => \Webkul\User\Repositories\UserRepository::class,
'column' => [
'label' => 'name',
'value' => 'name',
],
],
'closure' => function ($row) {
$route = urldecode(route('admin.settings.users.index', ['id[eq]' => $row->user_id]));

Expand All @@ -82,17 +90,25 @@ public function prepareColumns(): void
$this->addColumn([
'index' => 'expired_quotes',
'label' => trans('admin::app.quotes.index.datagrid.expired-quotes'),
'type' => 'string',
'type' => 'date',
'searchable' => false,
'filterable' => true,
]);

$this->addColumn([
'index' => 'person_name',
'label' => trans('admin::app.quotes.index.datagrid.person'),
'type' => 'string',
'sortable' => true,
'filterable' => true,
'index' => 'person_name',
'label' => trans('admin::app.quotes.index.datagrid.person'),
'type' => 'string',
'sortable' => true,
'filterable' => true,
'filterable_type' => 'searchable_dropdown',
'filterable_options' => [
'repository' => \Webkul\Contact\Repositories\PersonRepository::class,
'column' => [
'label' => 'name',
'value' => 'name',
],
],
'closure' => function ($row) {
$route = urldecode(route('admin.contacts.persons.index', ['id[eq]' => $row->person_id]));

Expand Down
42 changes: 42 additions & 0 deletions packages/Webkul/Admin/src/Http/Controllers/DataGridController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Webkul\Admin\Http\Controllers;

use Illuminate\Support\Facades\Crypt;

class DataGridController extends Controller
{
/**
* Look up.
*/
public function lookUp()
{
/**
* Validation for parameters.
*/
$params = $this->validate(request(), [
'datagrid_id' => ['required'],
'column' => ['required'],
'search' => ['required', 'min:2'],
]);

/**
* Preparing the datagrid instance and only columns.
*/
$datagrid = app(Crypt::decryptString($params['datagrid_id']));
$datagrid->prepareColumns();

/**
* Finding the first column from the collection.
*/
$column = collect($datagrid->getColumns())->map(fn ($column) => $column->toArray())->where('index', $params['column'])->firstOrFail();

/**
* Fetching on the basis of column options.
*/
return app($column['filterable_options']['repository'])
->select([$column['filterable_options']['column']['label'].' as label', $column['filterable_options']['column']['value'].' as value'])
->where($column['filterable_options']['column']['label'], 'LIKE', '%'.$params['search'].'%')
->get();
}
}
Loading

0 comments on commit f712c28

Please sign in to comment.