Sonata Admin 自定义模板
Posted
技术标签:
【中文标题】Sonata Admin 自定义模板【英文标题】:Sonata Admin custom template 【发布时间】:2013-09-06 08:05:21 【问题描述】:我正在尝试在 Sonata 中创建一个自定义页面,在该页面中我主要阅读当前月份创建的特定表的记录(行情)。每个报价都涉及金钱,因此在表格末尾我为当月的总计添加了额外的一行。
现在,由于这是一个自定义模板,我希望能够将奏鸣曲的“显示”操作按钮放在每条引语上,但我找不到方法……有什么想法吗?谢谢!
我没有使用 Admin 类,因为我正在覆盖自定义 CRUDController 中的 listAction 方法,如您在此处看到的...
public function listAction()
if (false === $this->admin->isGranted('LIST'))
throw new AccessDeniedException();
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
$data = array();
$em = $this->getDoctrine()->getManager();
$request = $this->getRequest();
$form = $this->createFormBuilder(null, array('label' => 'Month: '))
->add('month', 'choice', array(
'choices' => $this->getMonths(),
'label' => false
))
->add('search', 'submit', array(
'attr' => array('class' => 'btn-small btn-info'),
))
->getForm();
$form->handleRequest($request);
if($request->isMethod('POST'))
$startDate = new \DateTime($form->get('month')->getData());
$endDate = new \DateTime($form->get('month')->getData());
$endDate->modify('last day of this month');
else
$endDate = new \DateTime('now');
$startDate = new \DateTime('now');
$startDate->modify('first day of this month');
$dql = 'SELECT q FROM AcmeQuoteBundle:Quote q WHERE q.date BETWEEN \' '
. $startDate->format('Y-m-d') . '\' AND \'' . $endDate->format('Y-m-d')
. '\' ORDER BY q.date DESC';
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
50/*limit per page*/
);
$data['logrecords'] = $pagination;
return $this->render('AcmeQuoteBundle:Admin:quoteReport.html.twig',
array('data' => $data,
'action' => 'list',
'form' => $form->createView(),
'datagrid' => $datagrid
));
这是我正在使用的树枝:
% block list_table %
% set logrecords = data.logrecords %
# total items count #
<div class="count">
<h4>Total number of records founded: logrecords.getTotalItemCount </h4>
</div>
<table class="table table-striped table-hover table-bordered ">
<thead class="info">
<tr >
<th>#</th>
<th> knp_pagination_sortable(logrecords, 'Created at', 'q.date') </th>
<th> knp_pagination_sortable(logrecords, 'Quote token', 'q.viewToken') </th>
<th>Business name</th>
<th>$AUD</th>
<th>Action</th>
</tr>
</thead>
<tbody>
% set grandTotal = 0.0 %
% for quote in logrecords %
% set total = 0.0 %
% for item in quote.items %
% set total = (item.unit * item.rate) %
% endfor %
% set grandTotal = grandTotal + total %
<tr>
<td> loop.index </td>
<td> quote.date | date('Y-m-d') </td>
<td> quote.viewToken </td>
<td> quote.request.business.name </td>
<td> total </td>
<td>
<!-- THIS IS WHERE I WANT TO SHOW THE SHOW BUTTON -->
<a href="" role="button"><i class="icon-search"></i> SHOW</a>
</td>
</tr>
% endfor %
<tr>
<td colspan="3"></td>
<td>TOTAL (AUD): </td>
<td> grandTotal </td>
<td></td>
</tr>
</tbody>
</table>
# display navigation #
<div class="navigation">
knp_pagination_render(logrecords)
</div>
% endblock list_table %
【问题讨论】:
【参考方案1】:如果我理解正确,您想将带有自定义操作和模板的新路由添加到您的列表视图中。
您首先要做的是修改您的管理员,如下所示:
// Add a new route to your admin
protected function configureRoutes(RouteCollection $collection)
$collection->add('custom_show_action', $this->getRouterIdParameter().'/customshow');
// Final Step is to add your custom route and a template to the listMapper
protected function configureListFields(ListMapper $listMapper)
// Optional you can change the list layout with the following function
// and to there some calculation on the visible entries.
$this->setTemplate('list', '::custom_list_layout.html.twig');
// ...
$listMapper
// ...
->add('_action', 'actions', array(
'actions' => array(
'view' => array(),
'edit' => array(),
'custom_show_action' => array('template' => '::your_template.html.twig'),
)
))
;
// ...
我建议在控制器中计算值。就我而言,我已通过以下方式为发票包完成此操作:
// Acme/DemoBundle/Controller/AdminController
public function listAction()
if (false === $this->admin->isGranted('LIST'))
throw new AccessDeniedException();
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
$money_open = 0;
$money_payed = 0;
$money_referal = 0;
foreach($datagrid->getResults() as $key => $object)
if ($object->getPayedAt())
$money_payed += $object->getPrice();
if ($object->getReferal() && $object->getReferalPrice() > 0)
$money_referal += $object->getReferalPrice();
else
$money_open += $object->getPrice();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
return $this->render($this->admin->getTemplate('list'), array(
'action' => 'list',
'form' => $formView,
'datagrid' => $datagrid,
'money_open' => $money_open,
'money_payed' => $money_payed,
'money_referal' => $money_referal,
));
如您所见,我的资金计算是在控制器中完成的,我只需将其设置到模板中。
【讨论】:
干杯伙伴,我想我明白了你的回答:)以上是关于Sonata Admin 自定义模板的主要内容,如果未能解决你的问题,请参考以下文章
Sonata Admin Bundle - 表单类型:sonata_type_collection - 自定义模板?
Symfony Sonata Admin Bundle - 将带有树枝模板的自定义字段导出到 XLS
如何在 Sonata Admin(Symfony 3.3、PHP 7)中使用自定义 javascript 为模态窗口扩展模板?