自定义Silverstripe报告过滤器不会被应用,直到手动页面刷新

时间:2016-09-22 16:52:40

标签: filter report silverstripe

我在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);
    );
}

1 个答案:

答案 0 :(得分:1)

所以我想出来了。如果其他人遇到类似问题,问题源于使用标准数组,并使用该数组创建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
}

不需要更改其他功能。