<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hello-sebastian / hello-bootstrap-table-bundle example snippets
// src/HelloTable/UserTable.php
namespace App\HelloTable;
use App\Entity\User; // your entity class ...
use HelloSebastian\HelloBootstrapTableBundle\Columns\ColumnBuilder;
use HelloSebastian\HelloBootstrapTableBundle\Columns\TextColumn;
use HelloSebastian\HelloBootstrapTableBundle\Columns\DateTimeColumn;
use HelloSebastian\HelloBootstrapTableBundle\Columns\HiddenColumn;
use HelloSebastian\HelloBootstrapTableBundle\Columns\ActionColumn;
use HelloSebastian\HelloBootstrapTableBundle\Columns\BooleanColumn;
use HelloSebastian\HelloBootstrapTableBundle\HelloBootstrapTable;
class UserTable extends HelloBootstrapTable
{
protected function buildColumns(ColumnBuilder $builder, array $options): void
{
$builder
->add("id", HiddenColumn::class)
->add('username', TextColumn::class)
->add('email', TextColumn::class, array(
'title' => 'E-Mail',
'visible' => false
))
->add('firstName', TextColumn::class, array(
'title' => 'First name'
))
->add('lastName', TextColumn::class, array(
'title' => 'Last name'
))
->add('createdAt', DateTimeColumn::class, array(
'title' => 'Created at'
))
->add('department.name', TextColumn::class, array(
'title' => 'Department',
'emptyData' => 'No Department',
'addIf' => function() {
// In this callback it is decided if the column will be rendered.
return $this->security->isGranted('ROLE_DEPARTMENT_VIEWER');
}
))
->add("isActive", BooleanColumn::class, array(
'title' => 'is active',
'trueLabel' => 'yes',
'falseLabel' => 'no'
))
->add('department.costCentre.name', TextColumn::class, array(
'title' => 'Cost Centre',
'data' => function (User $user) {
return "#" . $user->getDepartment()->getCostCentre()->getName();
}
))
->add("actions", ActionColumn::class, array(
'title' => 'Actions',
'width' => 150,
'buttons' => array( //see ActionButton for more examples.
array(
'displayName' => 'open',
'routeName' => 'show_user',
'classNames' => 'btn btn-xs'
'additionalClassNames' => 'btn-success mr-1'
),
array(
'displayName' => 'edit',
'routeName' => 'edit_user',
'classNames' => 'btn btn-xs btn-warning',
'addIf' => function(User $user) {
// In this callback it is decided if the button will be rendered.
return $this->security->isGranted('ROLE_USER_EDITOR');
}
)
)
));
}
protected function getEntityClass(): string
{
return User::class;
}
}
// src/Controller/UserController.php
use HelloSebastian\HelloBootstrapTableBundle\HelloBootstrapTableFactory;
use App\HelloTable\UserTable;
// ...
/**
* @Route("/", name="default")
*/
public function index(Request $request, HelloBootstrapTableFactory $tableFactory): Response
{
$table = $tableFactory->create(UserTable::class);
$table->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
return $this->render('index.html.twig', array(
'table' => $table->createView()
));
}
//use statements for search and sort option
use Doctrine\ORM\Query\Expr\Composite;
use Doctrine\ORM\QueryBuilder;
->add('username', TextColumn::class, array(
'title' => 'Username',
'emptyData' => "No Username found.",
//optional overrides ...
'data' => function (User $user) { //entity from getEntityClass
//you can return what ever you want ...
return $user->getId() . " " . $user->getUsername();
},
'sort' => function (QueryBuilder $qb, $direction) { //execute if user sort this column
$qb->addOrderBy('username', $direction);
},
'search' => function (Composite $composite, QueryBuilder $qb, $search) {
//first add condition to $composite
//don't forget the ':' before the parameter for binding
$composite->add($qb->expr()->like($dql, ':username'));
//then bind search to query
$qb->setParameter("username", $search . '%');
}
))
->add("actions", ActionColumn::class, array( // key "actions" can be chosen freely but must be unique in the table
'title' => 'Actions',
'width' => 120, //optional
'buttons' => array(
array(
'displayName' => 'show',
'routeName' => 'show_user',
'additionalClassNames' => 'btn-success',
'attr' => array(
'title' => 'Show',
// any number of other attributes
)
),
array(
'displayName' => 'edit',
'routeName' => 'edit_user',
// 'classNames' => 'btn btn-xs' (see below for more information)
'additionalClassNames' => 'btn-warning',
'addIf' => function(User $user) { // you can use your entity in the function
// In this callback it is decided if the button will be rendered.
return $this->security->isGranted('ROLE_ADMIN');
}
)
)
))
use HelloSebastian\HelloBootstrapTableBundle\Filters\TextFilter;
->add('firstName', TextColumn::class, array(
'title' => 'First name',
'filter' => array(TextFilter::class, array(
'placeholder' => 'Enter first name ...'
))
))
use HelloSebastian\HelloBootstrapTableBundle\Filters\ChoiceFilter;
->add('department.name', TextColumn::class, array(
'title' => 'Department',
'filter' => array(ChoiceFilter::class, array(
'choices' => array(
'null' => 'All', //null is special key word. If 'null' is set QueryBuilder skip this column.
'IT' => 'IT',
'Sales' => 'Sales'
)
))
))
"choices" => array(
"null" => "All", // key must be "null", if you want allow to show all results
"true" => "True", // key must be "true", if you want allow true
"false" => "False" // key must be "false", if you want allow false
)
->add("isActive", BooleanColumn::class, array(
'title' => 'is active',
'filter' => array(BooleanChoiceFilter::class, array( // only if you want to override the choices
'choices' => array(
"null" => "Alle", // instead of "all"
"true" => "Ja", // instead of "yes"
"false" => "Nein" // instead of "no"
)
)),
'trueLabel' => 'yes',
'falseLabel' => 'no'
))
/**
* @Route("/", name="default")
*/
public function index(Request $request, HelloBootstrapTableFactory $tableFactory)
{
//first create a instance of your table
$table = $tableFactory->create(TestTable::class);
//then you can access the QueryBuilder from the table
$table->getQueryBuilder()
->andWhere('department.name = :departmentName')
->setParameter('departmentName', 'IT');
$table->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
return $this->render("index.html.twig", array(
"table" => $table->createView()
));
}
// inside table class
protected function buildColumns(ColumnBuilder $builder, array $options): void
{
$this->setTableDataset(array(
'sort-name' => 'firstName', // dql (or if set field name) of column
'sort-order' => 'desc' // or asc
));
$builder->add('firstName', TextColumn::class, array(
'title' => 'First name'
));
}
// outside table class
$table = $tableFactory->create(UserTable::class);
$table->setTableDataset(array(
'sort-name' => 'firstName',
'sort-order' => 'desc'
));
protected function buildColumns(ColumnBuilder $builder, array $options): void
{
//enable detail-view and set formatter
$this->setTableDataset(array(
'detail-view' => true,
'detail-formatter' => 'detailViewFormatter'
));
$builder
// other columns ...
// detailView is not a database field and can be named as you like.
// but the column should not displayed in the table (HiddenColumn)
->add('detailView', HiddenColumn::class, array(
// override data callback (as attribute you can access the entity that you specified in getEntityClass())
'data' => function (User $user) {
// now you can return everthing you want (twig render