You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 11中如何实现cart_products商品列表的升降序排序

TYPO3 Extbase实现商品列表升序/降序排序

1. 扩展Repository的查询方法

首先需要修改ProductRepository.php中的findDemanded方法,让它支持接收排序参数,并根据参数设置查询排序规则:

public function findDemanded(\Vendor\YourExt\Domain\Model\Demand $demand, string $sorting = null)
{
    $query = $this->createQuery();
    // 保留原有构建查询条件的代码

    // 处理排序逻辑
    if ($sorting !== null) {
        // 根据参数选择排序方向
        $orderDirection = $sorting === 'up' 
            ? \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING 
            : \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING;
        
        // 指定排序字段,可根据业务需求修改(比如按title、price等字段)
        $query->setOrderings([
            'title' => $orderDirection
        ]);
    }

    return $query->execute();
}

2. 在控制器中传递排序参数

更新ProductController.php的listAction,将前端传入的排序参数传递给Repository方法:

public function listAction()
{
    $demand = $this->createDemandObject(); // 保留原有创建查询条件的代码
    // 获取前端传入的排序参数,无参数时设为null
    $sorting = $this->request->hasArgument('sorting') ? $this->request->getArgument('sorting') : null;

    // 带排序参数查询商品列表
    $products = $this->productRepository->findDemanded($demand, $sorting);
    
    $this->view->assign('products', $products);
}

3. 完善视图中的排序链接

在Grid.html中补充降序排序的链接,让用户可以切换排序方向:

<f:link.action action="list" arguments="{sorting:'up'}">升序排序</f:link.action>
<f:link.action action="list" arguments="{sorting:'down'}">降序排序</f:link.action>

补充说明

  • 如果需要多字段组合排序,只需在setOrderings数组中添加更多字段规则即可,示例:
    $query->setOrderings([
        'organization.name' => $orderDirection,
        'title' => $orderDirection
    ]);
    
  • 排序字段需对应实体类中定义的属性名,比如价格price、创建时间crdate等,可根据业务需求灵活调整。

内容的提问来源于stack exchange,提问作者Urs Bo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:54:26