问题描述
我正在尝试使用 knp 包设置分页。我想尝试每页显示一个任务,但是当我单击第二页时,它是完全相同的任务。你对问题有什么想法吗?
TacheController(检索插入过滤器(表单)中的数据)
/**
* @Route("/accueil",name="accueil")
*/
public function liste(TacheRepository $tacheRepository,CategorieRepository $categorieRepository,Request $request,PaginatorInterface $paginator): Response
{
$tache = new Tache();
$form = $this->createForm(FiltreFormType::class,$tache);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!empty($form['q']->getData())) {
$tache->setTitre($form['q']->getData());
}
if (!empty($form['categories']->getData()->getValues())) {
foreach ($form['categories']->getData() as $categorie) {
$tache->addCategory($categorie);
}
}
}
isset($_POST['datedecroissante']) ? $tri = false : isset($_POST['datecroissante']) ? $tri = true : $tri = false;
$taches = $tacheRepository->rechercherTache($tache,$tri);
$pagination = $paginator->paginate(
$taches,$request->query->getInt('page',1),1
);
return $this->render('main/accueil.html.twig',['taches' => $taches,'categories' => $categorieRepository->findAll(),'form' => $form->createView(),'pagination' => $pagination]);
}
TacheRepository
public function rechercherTache(Tache $tache,$tri)
{
$qb = $this->createqueryBuilder('t')
->select('t','c')
->join('t.categories','c');
if (!empty($tache->getTitre())) {
$qb = $qb->andWhere('t.titre LIKE :q')
->setParameter('q',"%{$tache->getTitre()}%");
}
if (!empty($tache->getCategories()->getValues())) {
$qb = $qb->andWhere('c.id IN (:categories)')
->setParameter('categories',$tache->getCategories());
}
if ($tri == false) {
$qb = $qb->orderBy('t.date_creation','DESC');
} else {
$qb = $qb->orderBy('t.date_creation','ASC');
}
return $qb->getQuery()->getResult();
}
Accueil.html.twig
{% extends ('main/base.html.twig') %}
{% block body %}
<div class="container rounded shadow">
<div class="row">
<div class="col-sm-11 col-md-9 col-lg-9 my-5 mx-auto">
<div class="card login-form">
<div class="card-body">
{% if app.user %}
<h2 class="card-title text-center mb-3">Bienvenue {{ app.user.prenom }}</h2>
{% endif %}
<h3 class="card-title text-center mb-3">Ma Todolist</h3>
<form action="{{ path('ajouter_tache') }}" method="POST">
<div class="add-items d-flex justify-content-center" style="margin-top: 30px">
<input id="titre" name="titre" type="text" class="form-control todo-list-input"
placeholder="Quelle tâche souhaitez-vous ajouter ?">
</div>
<div class="add-items d-flex mt-3">
{% if categories is defined %}
{% for categorie in categories %}
<div class="form-check-inline">
<label class="form-check-label" style="margin-right: 10px">
<input id="categorie{{ categorie.id }}" name="categories[]"
type="checkBox" value="{{ categorie.id }}"
class="form-check-input">{{ categorie.intitule }}
</label>
</div>
{% endfor %}
{% endif %}
</div>
<button type="submit" id="add"
class="mt-3 add btn btn-primary font-weight-bold todo-list-add-btn">Ajouter
</button>
</form>
<div class="js-filter-form">
{% include 'main/filtre.html.twig' with {form:form} only %}
</div>
<form action="{{ path('accueil') }}" method="POST">
<input type="submit" class="mt-3 add btn btn-primary font-weight-bold todo-list-add-btn"
name="datecroissante" value="Date croissante">
<input type="submit" class="mt-3 add btn btn-primary font-weight-bold todo-list-add-btn"
name="datedecroissante" value="Date décroissante">
</form>
<ul class=" list-group list-group-flush
" style="margin-top: 50px">
{% if taches is defined %}
{% for tache in taches %}
<li id="task{{ tache.id }}"
class="list-group-item d-flex justify-content-between align-items-center">
<div class="liste custom-control custom-checkBox">
<input type="checkBox" class="check custom-control-input"
id="check{{ tache.id }}"
{{ (tache.estTermine == 1) ? 'checked' : '' }}
data-check="{{ path('etat_tache',{id: tache.id}) }}">
<label id="intitule{{ tache.id }}"
class="custom-control-label font-weight-bold"
style="{{ (tache.estTermine == 1) ? 'text-decoration: line-through' : '' }}"
for="check{{ tache.id }}">{{ tache.titre }}</label>
<br>
{% if tache.categories is defined %}
{% for categorie in tache.categories %}
<span class="badge badge-pill badge-primary">{{ categorie.intitule }}</span>
{% endfor %}
{% endif %}
<p class="font-italic"
style="font-size: x-small;">{{ tache.dateCreation|date('d-m-Y') }}</p>
</div>
<div class="btn-toolbar">
<form action="{{ path('form_tache',{id: tache.id}) }}"
method="GET">
<button type="submit" id="edit{{ tache.id }}"
class="edit btn btn-warning"
style="width: 40px">
<span class="far fa-edit fa-1x"></span>
</button>
</form>
<form action="{{ path('supprimer_tache',{id: tache.id}) }}"
method="POST">
<button type="submit" id="delete{{ tache.id }}"
class="btn btn-danger"
style="margin-left: 10px; width: 40px">
<span class="far fa-trash-alt fa-1x"></span>
</button>
</form>
</div>
</li>
{% endfor %}
{% endif %}
</ul>
<div style="margin-top: 20px">
{{ knp_pagination_render(pagination) }}
</div>
</div>
</div>
</div>
</div>
</div>
Knp_paginator.yaml
knp_paginator:
page_range: 5 # number of links showed in the pagination menu (e.g: you have 10 pages,a page_range of 3,on the 5th page you'll see links to page 4,5,6)
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results,useful when ORM queries are using GROUP BY statements
filter_field_name: filterField # filter field query parameter name
filter_value_name: filterValue # filter value query parameter name
template:
pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig' # sliding pagination controls template
sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template
filtration: '@KnpPaginator/Pagination/filtration.html.twig' # filters template
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)