Datagrid add column before and after event added

This commit is contained in:
Prashant Singh 2019-09-11 01:32:35 +05:30
parent 6c03f824ec
commit 7d7cda1f73
4 changed files with 153 additions and 15 deletions

View File

@ -28,6 +28,8 @@ class AttributeDataGrid extends DataGrid
public function addColumns() public function addColumns()
{ {
$this->setInvoker($this);
$this->addColumn([ $this->addColumn([
'index' => 'id', 'index' => 'id',
'label' => trans('admin::app.datagrid.id'), 'label' => trans('admin::app.datagrid.id'),
@ -64,19 +66,19 @@ class AttributeDataGrid extends DataGrid
'filterable' => true 'filterable' => true
]); ]);
$this->addColumn([ // $this->addColumn([
'index' => 'is_required', // 'index' => 'is_required',
'label' => trans('admin::app.datagrid.required'), // 'label' => trans('admin::app.datagrid.required'),
'type' => 'boolean', // 'type' => 'boolean',
'sortable' => true, // 'sortable' => true,
'searchable' => false, // 'searchable' => false,
'wrapper' => function($value) { // 'wrapper' => function($value) {
if ($value->is_required == 1) // if ($value->is_required == 1)
return 'True'; // return 'True';
else // else
return 'False'; // return 'False';
} // }
]); // ]);
$this->addColumn([ $this->addColumn([
'index' => 'is_unique', 'index' => 'is_unique',

View File

@ -0,0 +1,28 @@
<?php
namespace Webkul\Product\Commands;
use Webkul\Product\Repositories\ProductRepository as Product;
class DemoProducts
{
/**
* Holds ProductRepository instance
*/
protected $product;
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* To call the seeder and provide the demo data generators parameters
*/
public function callGenerator()
{
$result = $this->productSeeder();
}
}

View File

@ -2,7 +2,7 @@
namespace Webkul\Ui\DataGrid; namespace Webkul\Ui\DataGrid;
use Illuminate\Http\Request; use Event;
/** /**
* DataGrid class * DataGrid class
@ -12,22 +12,87 @@ use Illuminate\Http\Request;
*/ */
abstract class DataGrid abstract class DataGrid
{ {
/**
* set index columns, ex: id.
*/
protected $index = null; protected $index = null;
/**
* To know the class of datagrid calling parent methods, to be set by the extending class.
*/
protected $invoker = null;
/**
* Default sort order of datagrid
*/
protected $sortOrder = 'asc'; protected $sortOrder = 'asc';
/**
* Situation handling property when working with custom columns in datagrid, helps abstaining
* aliases on custom column.
*/
protected $enableFilterMap = false; protected $enableFilterMap = false;
/**
* This is array where aliases and custom column's name are passed
*/
protected $filterMap = []; protected $filterMap = [];
/**
* array to hold all the columns which will be displayed on frontend.
*/
protected $columns = []; protected $columns = [];
protected $completeColumnDetails = []; protected $completeColumnDetails = [];
/**
* Hold query builder instance of the query prepared by executing datagrid
* class method setQueryBuilder
*/
protected $queryBuilder = []; protected $queryBuilder = [];
/**
* Final result of the datagrid program that is collection object.
*/
protected $collection = []; protected $collection = [];
/**
* Set of handly click tools which you could be using for various operations.
* ex: dyanmic and static redirects, deleting, etc.
*/
protected $actions = []; protected $actions = [];
/**
* Works on selection of values index column as comma separated list as response
* to your endpoint set as route.
*/
protected $massActions = []; protected $massActions = [];
protected $request;
/**
* Parsed value of the url parameters
*/
protected $parse; protected $parse;
/**
* To show mass action or not.
*/
protected $enableMassAction = false; protected $enableMassAction = false;
/**
* To enable actions or not.
*/
protected $enableAction = false; protected $enableAction = false;
/**
* paginate the collection or not
*/
protected $paginate = true; protected $paginate = true;
/**
* If paginated then value of pagination.
*/
protected $itemsPerPage = 15; protected $itemsPerPage = 15;
protected $operators = [ protected $operators = [
'eq' => "=", 'eq' => "=",
'lt' => "<", 'lt' => "<",
@ -105,9 +170,13 @@ abstract class DataGrid
public function addColumn($column) public function addColumn($column)
{ {
$this->fireEvent('before.add.column.'.$column['index']);
array_push($this->columns, $column); array_push($this->columns, $column);
$this->setCompleteColumnDetails($column); $this->setCompleteColumnDetails($column);
$this->fireEvent('after.add.column.'.$column['index']);
} }
public function setCompleteColumnDetails($column) public function setCompleteColumnDetails($column)
@ -306,12 +375,34 @@ abstract class DataGrid
return $collection; return $collection;
} }
protected function fireEvent($name)
{
$className = get_class($this->invoker);
$className = last(explode("\\", $className));
$className = strtolower($className);
$eventName = $className.'.'.$name;
Event::fire($eventName, $this->invoker);
}
public function prepareMassActions() { public function prepareMassActions() {
} }
public function prepareActions() { public function prepareActions() {
} }
public function setInvoker($class)
{
$this->invoker = $class;
// $this->invoker = last(explode("\\", $class));
// $this->invoker = strtolower($this->invoker);
}
public function render() public function render()
{ {
$this->addColumns(); $this->addColumns();

View File

@ -0,0 +1,17 @@
Event::listen('attributedatagrid.before.add.column.id', function($dataGridInstance) {
$data = [
'index' => 'is_required',
'label' => 'trans',
'type' => 'boolean',
'sortable' => true,
'searchable' => false,
'wrapper' => function ($value) {
if ($value->is_required == 1)
return 'True';
else
return 'False';
}
];
$dataGridInstance->addColumn($data);
});