KnpPaginatorBundle
KnpPaginatorBundle copied to clipboard
Controller Parameter Names Not Changing
I originally asked in the KNP Paginator Component Github, but they sent me here.
| Bundle version | 5.9.0 | Components version | 3.6.0 | Symfony version | 6.4.2 | PHP version | 8.3.2
I have several pagination objects on a single page, so I want to change the respective parameter names for sorting and paging so they don't conflict. Changing the page parameter name works using "pageParameterName" (see code below for more details), however using the documented "sortFieldParameterName" and "SortDirectionParameterName" change nothing. The parameters are still the default "sort" and "direction", which I do not want to be changed in other parts of the application, only in this function.
I'm referring to this for documentation.
Location table function calls the initial page for the table, where Twig embeds the item table below.
* Function to display all locations in the system
*
* @author Daniel Boling
*/
#[Route('/location/list/', name:'loc_list')]
public function loc_list(Request $request): Response
{
if (isset($request->query->all()['loc_code']))
{
$loc = $this->loc_repo->find($request->query->all()['loc_code']);
} else {
$loc = new Location;
}
$loc_form = $this->createForm(LocationType::class, $loc);
$loc_thead = [
'loc_code' => 'Loc Code',
'loc_desc' => 'Loc Desc',
'loc_notes' => 'Loc Notes',
'item_total_qty' => 'Item Total Qty.',
];
$result = $this->loc_repo->findAll();
$result = $this->paginator->paginate($result, $request->query->getInt('loc_page', 1), 1, ['sortFieldParameterName' => 'loc_sort', 'sortDirectionParameterName' => 'loc_direction', 'pageParameterName' => 'loc_page']);
$normalized_locations = [];
foreach ($result->getItems() as $item)
{
$normalized_locations[] = [
'loc_code' => $item->getLocCode(),
'loc_desc' => $item->getLocDesc(),
'loc_notes' => $item->getLocNotes(),
'item_total_qty' => $item->getItemQty(),
];
}
$result->setItems($normalized_locations);
return $this->render('location/loc_list.html.twig', [
'locations' => $result,
'loc_thead' => $loc_thead,
'form' => $loc_form,
]);
}
Item Table Fragment. Is called using Twig controller embedding.
* Fetches selected location's items for template fragment
*
* @author Daniel Boling
*/
public function loc_items_list(Request $request, ?string $loc_code, ?int $item_page = 1): Response
{
$loc = $this->loc_repo->find($loc_code);
$item_thead = [
'item_code' => 'Item Code',
'item_desc' => 'Item Desc',
'item_unit' => 'Item Unit',
'item_notes' => 'Item Notes',
'item_exp_date' => 'Item Exp. Date',
'item_qty' => 'Item Total Qty.',
];
// to autofill form fields, or leave them null.
$result = $this->item_repo->findByLoc($loc_code);
$result = $this->paginator->paginate($result, $item_page, 10, ['pageParameterName' => 'item_page', 'sortParameterName' => 'item_sort', 'sortDirectionParameterName' => 'item_direction']);
$normalized_items = [];
foreach ($result->getItems() as $item)
{
$normalized_items[] = [
'item_code' => $item->getItemCode(),
'item_desc' => $item->getItemDesc(),
'item_notes' => $item->getItemNotes(),
'item_exp_date' => $item->getItemExpDate(),
'item_qty' => $item->getItemQty(),
'item_unit' => $item->getItemUnit()->getUnitCode(),
];
}
$result->setItems($normalized_items);
return $this->render('location/item_table.html.twig', [
'items' => $result,
'item_thead' => $item_thead,
]);
}
Each table page parameter should have the respective name, which is working. The sort parameters should change as well, in the same way, however, they are not. What am I missing?