我通常使用观察者向网格中添加一列。 See this page (german language) how to do this.
Also the full module of this tutorial is available on GitHub.
但是为了帮助你,一些源代码就可以了。您要过滤哪个自定义列?它可以有哪些值?
我是这样做的(在文件 Observer.php 中)
$grid->addColumnAfter(
'custom_column_code', // replace by your column code
array(
'header' => 'Label of your column',
'index' => 'custom_column_code', // replace by your column code
'type' => 'options',
'renderer' => 'YourCompany_YourModule_YourRenderer',
'filter_condition_callback' => array($this, '_filterHasMyCustomConditionCallback')
'options' => array( //these are your filter options
'1' => 'Yes',
'0' => 'No'
),
),
'name'
);
然后也在 Observer.php
public function _filterHasMyCustomConditionCallback($collection, $column)
{
$value = $column->getFilter()->getValue();
if ($value == '0')
{
// join custom_column LEFT (inner join fails on NULL values) and filter by NULL
$collection->addAttributeToFilter('custom_column_code', array(['null' => true]), 'left');
}
else
{
// filter all by NOT NULL
$collection->addFieldToFilter('custom_column_code', ['notnull' => true]);
}
return $this;
}