在手动页面刷新之前,自定义 Silverstripe 报告过滤器未被应用

Custom Silverstripe Report Filter isn't being applied till manual page refresh

我正在 SilverStripe 中处理此自定义报告,当我筛选报告时,SilverStripe 会重新加载页面,但不会应用筛选器。但是,如果我点击刷新按钮或按 f5 并自行重新加载页面,则过滤器会成功应用。任何帮助都将不胜感激。

这是我所拥有版本的简化版本:

public function sourceRecords($params){
    $data = Data::get();
    $records = [];
    $filterParam = 0;

    if (isset($params["filterParam"])
        $filterParam = $params["filterParam"];

    foreach ($data as $item) {
        if ($item->value == $filterParam) {
            $records[] = $item;
        }
    }
    return new ArrayList($records, "Data");
}

public function columns() {
    return array ("value" => "Value");
}

public function parameterFields() {
    return new FieldList(
        new TextField("FilterParam", _t("MyCustomReport.FilterParam", "Filter Param"), 0);
    );
}

所以我想通了。如果其他人遇到类似问题,则问题源于使用标准数组,并使用该数组创建 ArrayList。当我从一开始就切换到只使用 ArrayList 时,过滤器工作正常。

更正后的代码:

public function sourceRecords($params){
    $data = Data::get();
    $records = new ArrayList(); // This is now new ArrayList instead of []
    $filterParam = 0;

    if (isset($params["filterParam"])
        $filterParam = $params["filterParam"];

    foreach ($data as $item) {
        if ($item->value == $filterParam) {
            $records->push($item); // changed to use the correct syntax for ArrayList
        }
    }
    return $records; // No longer making new ArrayList here
}

无需更改其他功能。