KnpLabs / knp-components

Various component pack, includes paginator
MIT License
751 stars 141 forks source link

Controller Parameter Names Not Changing #327

Closed dboling19 closed 9 months ago

dboling19 commented 9 months ago

| 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 respectve parameters names for sorting and paging so they don't conflict. Paging works by using "pageParameterName" (see code below for more details), however using the documented "sortFieldParameterName" and "SortDirectionParameterName" change nothing. The parameters are still the default, which I do not want changed in other parts of the application, only in this function.

Location table function, which calls the inital 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,
    ]);
  }

In my mind, each table page parameter should have the respective name, which is working. The sort parameters should change as well, in the exact same way, however, they are not. What am I missing?

garak commented 9 months ago

Could you clarify "parameters are still the default"?

dboling19 commented 9 months ago

When selecting the links to sort or change page, the parameter names in the URL are whatever is set in knp_paginator.yaml, hence the "default". I would expect these parameters to change to whatever I defined in the controller. This works fine for "pageParameterName", but none of the others which are defined in the documentation.

garak commented 9 months ago

I'm sorry but I'm not able to reproduce the problem. Moreover, this is the repository for the library: if you can provide a reproducer, please open an issue on the bundle repository. Thanks

dboling19 commented 9 months ago

I'm not sure how to "provide a reproducer". I provided my code above, it's not working as the documentation claims.
I'm using the component's documentation, as this is not covered in the bundle's documentation. I figured it's here and not there due to a granularity thing. https://github.com/KnpLabs/knp-components/blob/master/docs/pager/config.md 'pageParamterName' is working fine on all uses. Only the sortable seems broken.

garak commented 9 months ago

A reproducer is a minimal running project where the issue is programmatically reprosuceable. Minimal assumptions about the environment (so, ideally, a project running inside a docker instance)