我正在使用paginator和fosJsRouting制作带过滤器的表单..
我尝试制作一个过滤器,根据所选月份仅显示结果。所以在主页上,我们选择在控制器中花费的月份,然后我想在动态请求中传递它。 我无法定义如果变量为null,则选择all。 谢谢你的帮助
这是我的Controller索引:
public function indexAction( Request $request, $page, $limit, $month ) {
$em = $this->getDoctrine()->getManager();
if(isset($_GET['month']))
$month = $_GET['month'];
else
$month = "NULL";
$debits = $em->getRepository( 'BudgetBundle:Debit' )->getAmounts($this->getUser(), $month);
// Paramerage de la limite d'affichage par page
if(isset($_GET['limit'])) $limit = $_GET['limit'];
##################################################################################### START_DEBIT
if ( $page == '' ) // Evite l'affichage de la page numero 2 sur l'index
{
$page = 1;
}
$paginator = $this->get( 'knp_paginator' );
$pagination = $paginator->paginate(
$debits, /* query NOT result */
$request->query->getInt( 'page', $page )/*page number*/,
$limit/*limit per page*/
);
// Calcul du Total de dépense par page
$total = [];
foreach ( $pagination->getItems() as $item ) {
$total[] = $item->getAmount();
}
$totalAmount = [];
for ( $i = 0; $i < count( $debits->getQuery()->getResult() ); $i ++ ) {
$totalAmount[] = $debits->getQuery()->getResult()[ $i ]->getAmount();
}
##################################################################################### END_DEBIT
return $this->render( 'BudgetBundle:Views:index.html.twig', [
'pagination' => $pagination,
'total' => array_sum( $total ),
'montantTotal' => array_sum( $totalAmount )
] );
}
查询构建器:
public function getAmounts($user, $month){
$qb = $this->createQueryBuilder('d')
->leftJoin('d.company', 'company')
->addSelect('company')
->where('d.user = :user')
->setParameter('user', $user)
// - &gt; andWhere($ month == NULL?'d.date = IS NOT NULL':'d.date =:month') // - &gt; setParameter('month',$ month) - &gt; orderBy('d.date','DESC');
var_dump($month);
var_dump($qb->getQuery()->getDQL());
return $qb;
}
我的观点:
<label for="month">Choisissez le mois</label>
<select title="month" class="form-control" style="display: inline;width: 100px; height: 30px;" name="month" id="month">
{% if app.request.query.get("month") %}
<option style="display: none;" value="{{ app.request.query.get("month") }}" selected>{{ app.request.query.get("month") }}</option>
{% endif %}
<option value="01">Janvier</option>
<option value="02">Fevrier</option>
</select>
Js:
$('#month').change(function () {
var url = Routing.generate('budget_homepage', {'month':$('#month').find(":selected").text()});
window.location.href = url;
});
路线:
budget_homepage:
path: /{page}
defaults: { _controller: BudgetBundle:Budget:index, page: 1, limit: 5, month: null }
requirements:
page: \d*
limit: \d*
options:
expose: true
答案 0 :(得分:0)