uxf / datagrid
3.56.3
2024-12-18 10:59 UTC
Requires
- php: ^8.3
- ext-json: *
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/serializer: ^6.4 || ^7.0
- uxf/core: 3.56.3
Suggests
- mk-j/php_xlsxwriter: Support xlsx export
- dev-main
- 3.56.3
- 3.56.2
- 3.56.1
- 3.56.0
- 3.55.9
- 3.55.8
- 3.55.7
- 3.55.6
- 3.55.5
- 3.55.4
- 3.55.3
- 3.55.0
- 3.54.5
- 3.54.3
- 3.54.2
- 3.54.0
- 3.53.7
- 3.53.3
- 3.53.2
- 3.53.1
- 3.53.0
- 3.52.0
- 3.51.2
- 3.51.0
- 3.50.6
- 3.50.5
- 3.50.3
- 3.50.2
- 3.50.1
- 3.50.0
- 3.49.2
- 3.49.1
- 3.49.0
- 3.48.2
- 3.48.0
- 3.47.2
- 3.47.0
- 3.46.12
- 3.46.11
- 3.46.10
- 3.46.9
- 3.46.8
- 3.46.7
- 3.46.6
- 3.46.5
- 3.46.4
- 3.46.3
- 3.46.1
- 3.46.0
- 3.44.6
- 3.44.5
- 3.44.4
- 3.44.3
- 3.44.2
- 3.44.1
- 3.44.0
- 3.43.3
- 3.43.2
- 3.43.1
- 3.43.0
- 3.42.1
- 3.42.0
- 3.41.2
- 3.41.1
- 3.41.0
- 3.40.7
- 3.40.4
- 3.40.3
- 3.40.2
- 3.40.1
- 3.40.0
- 3.39.5
- 3.39.4
- 3.39.3
- 3.39.2
- 3.39.1
- 3.38.0
- 3.37.1
- 3.37.0
- 3.36.3
- 3.36.2
- 3.36.0
- 3.35.5
- 3.35.4
- 3.35.2
- 3.35.1
- 3.35.0
- 3.34.3
- 3.34.0
- 3.33.1
- 3.33.0
- 3.32.4
- 3.32.3
- 3.32.0
- 3.30.1
- 3.30.0
- 3.29.1
- 3.29.0
- 3.28.0
- 3.27.9
- 3.27.8
- 3.27.7
- 3.27.3
- 3.27.2
- 3.27.0
- 3.26.0
- 3.25.3
- 3.24.7
- 3.24.4
- 3.24.2
- 3.24.1
- 3.24.0
- 3.23.3
- 3.23.1
- 3.23.0
- 3.22.0
- 3.21.4
- 3.21.3
- 3.21.0
- 3.20.3
- 3.20.0
- 3.19.4
- 3.19.2
- 3.19.1
- 3.19.0
- 3.18.0
- 3.17.4
- 3.17.3
- 3.17.1
- 3.17.0
- 3.15.6
- 3.15.5
- 3.13.2
- 3.13.0
- 3.11.3
- 3.11.0
- 3.10.1
- 3.10.0
- 3.9.2
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.3
- 3.7.1
- 3.7.0
- 3.6.3
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2
- v0.1
- dev-master
- dev-filter-inputs
- dev-export-stream
- dev-trim-string-filter
- dev-fix/default-settings
- dev-psr-12
This package is auto-updated.
Last update: 2024-12-18 10:03:47 UTC
README
Symfony DataGrid bundle
Install
JS
https://www.npmjs.com/package/@uxf/data-grid
PHP
composer require uxf/datagrid
Config
Route
// config/routes/uxf.php
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
return static function (RoutingConfigurator $config): void {
$config->import('@UXFDataGridBundle/config/routes.php');
};
Service
// config/packages/uxf.php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $config): void {
$config->extension('uxf_data_grid', [
'gen' => [
'schema_with_hidden_columns' => true, // default false
'areas' => [
'admin' => [
'allowed' => '/^(admin|test)/', // optional
'disabled' => '/^(private|hello)/', // optional
'destination' => __DIR__ . '/../../../backoffice/src/generated/data-grid-schema',
],
],
],
]);
};
Usage
Generator
bin/console uxf:grid-gen
Request/Response
# schema
GET /api/cms/datagrid/schema/{name}
{
"columns": [
{
"name": "name",
"label": "Name",
"sort": true,
"type": "toMany" // optional - default "string"
}
],
"filters": [
{
"name": "name",
"label": "Name",
"type": "checkbox", // optional - default "string"
"options": [{"label": "X": "id": 1}] // optional
}
],
"s": {
"sort": "id",
"dir": "asc",
},
"perPage": 10
}
# data
GET /api/cms/datagrid/{name}?f[0][name]=id&f[0][value]=666&sort=id&dir=asc&page=1&perPage=20
{
"result": [
{
"id": 1,
"name": "Superman",
}
],
"count": 15, // filtered count
"totalCount": 300
}
# autocomplete
GET /api/cms/datagrid/autocomplete/{name}/{filterName}
# export
GET /api/cms/datagrid/export/{name}
Interface UXF\DataGrid\DataGridType
Bundle find in application container every class implements interface UXF\DataGrid\DataGridType
and create for each public service.
Example:
/**
* @template-implements DataGridType<Superman>
*/
class OwnGridType implements \UXF\DataGrid\DataGridType
{
/** @var EntityManagerInterface */
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function buildGrid(DataGridBuilder $builder, array $options = []): void
{
$builder
->setDefaultPerPage(100)
->setDefaultSort('name', DataGridSortDir::DESC);
$builder->addBasicColumn('active', 'Active')
->addBooleanSelectFilter();
$builder->addToOneColumn('category', 'Category')
->setSort()
->addEntitySelectFilter(Tag::class, 'name');
$builder->addFilter(new CheckboxFilter('extraFilter', 'Extra filter'))
->setQbCallback(static function (QueryBuilder $qb, $value) {
if ((int) $value !== 1) {
return;
}
$qb->andWhere('s.setting.ignored = 1 AND s.tagM2O IS NULL');
});
// example: full text
$builder->setFullTextFilterCallback(function (QueryBuilder $qb, string $value): void {
DataGridMagicHelper::trySafelyApplyJoin($qb, 'u.role');
$qb->andWhere('u.name LIKE :fullText OR role.name LIKE :fullText')
->setParameter('fullText', "%$value%");
});
// example: row callback
$builder->setRowContentCallback(fn (Superman $item) => [
'one' => $item->getName(),
'two' => $item->getAge(),
]);
// example: row export callback
$builder->setRowExportCallback(fn (Superman $item) => [
'one' => $item->getName(),
'two' => $item->getAge(),
]);
// tabs
$builder->addTab(
'one',
'One',
'icon-1',
static fn (QueryBuilder $qb) => $qb->andWhere('s.id = 1'),
new DataGridSort('id', DataGridSortDir::DESC),
);
$builder->addTab('two', 'Two', 'icon-2', static function (QueryBuilder $qb): void {
$qb->andWhere('s.id = 2');
});
}
/**
* @param mixed[] $options
*/
public function getDataSource(array $options = []): DataSource;
{
$qb = $this->entityManager->createQueryBuilder()
->select('s')
->from(Superman::class, 's')
->where('s.tagO2O IS NULL');
return new DoctrineDataSource($qb);
}
public static function getName(): string
{
return 'test-own';
}
}
Filter by deep nested entity
For example, I have following classes structure Order -> TourOperatingTime -> Season -> Tour
- Class
Tour
istour
property in classSeason
. - Class
Season
isseason
property in classTourOperatingTime
. - Class
TourOperatingTime
istourOperatingTime
property in classOrder
.
And I want to filter orders by tour
/**
* @template-implements DataGridType<Superman>
*/
class OrderGridType implements \UXF\DataGrid\DataGridType
{
// If you not using this with our uxf/cms , you have to init builder
public function buildGrid(DataGridBuilder $builder, array $options = []): void
{
// Create new entity alias filter called tour
$tourFilter = new EntitySelectFilter(
'tour',
'Tour',
Tour::class,
'tour.id'
);
// Filter Qb callback - this will do the magic and returns desired data
$tourFilter->setQbCallback(
static function (QueryBuilder $qb, $tourId) {
if ($tourId) {
// I can create helper query to get Order ids I want, so the main query is not too big
// (filter only on ids, instead of whole table rows)
$qb2 = $qb->getEntityManager()->createQueryBuilder();
$qb2->select('o.id')
->from(Order::class, 'o')
->join('o.tourOperatingTime', 'time')
->join('time.season', 'season')
->andWhere('season.tour = :tourId'); // Parameter must be set in last query builder!
// Now I put my helper query which returns me filtered order ids, to return complete rows
$qb->andWhere("e.id IN ({$qb2})") // e is default identifier in uxf/cms package
->setParameter('tourId', $tourId);
}
}
);
// You can adjust order for FE
$tourFilter->setOrder(10001);
// Finally add filter to your grid
$builder->addFilter($tourFilter);
}
public static function getName(): string
{
return 'order-alias';
}
}
DoctrineDataSource
$dataSource = new DoctrineDataSource(
queryBuilder: $qb,
withoutPaginator: true,
withoutDistinct: true,
rootAlias: 'x',
);
Doc
Columns
- isSort/setSort = set sortable
- getOrder/setOrder = set column position
- isHidden/setHidden = hidden column is sent in response but not visible
- setCustomContentCallback(fn (mixed $item) => data) - returned value will by in response
Column
Doctrine Column + Embeddable
Filters
StringFilter
Like %% query
CheckboxFilter
Three state select (Yes/No/None)
DateRangeFilter
Filter date from/to
DateTimeRangeFilter
Filter date-time from/to
EntitySelectFilter
Specify className and field (e.g. name) and you can use Autocomplete.
IntervalFilter
Interval from/to
SelectFilter
Simple select with default options array