我目前正在开发一个自定义模块加载项,我希望能够在我的控制面板管理员的表格上使用排序和过滤。我正在使用EE表类并形成帮助器。我正在尝试按documentation here进行设置,但是当我打电话试图在我的课程中调用'_datasource'方法时我得到了这个错误
Fatal error: Call to undefined method Content_publish::_datasource() in /home/public_html/system/expressionengine/libraries/EE_Table.php on line 162
我觉得这是一个范围问题,但在表类'$ this-> EE-> table-> datasource()'方法中,您应该只传递一个字符串值,其名称为您的数据源功能就是我正在做的事情。
我似乎不是唯一一个有这个问题的人。 this EE Discussion forum thread
上有更多详细信息和代码示例文档不是很清楚。我也试着看看EE自己的评论模块,看看我是否可以搞清楚,但没有运气。有人有这方面的经验吗?
这是我打电话的方法:
$data = $this->EE->table->datasource('_datasource');
这是我班上的功能:
function _datasource()
{
// ....
// $query comes from DB result set code above.
// I have omitted it here for brevity
$datarows = array();
foreach ($query->result_array() as $key => $row)
{
$datarows[] = array(
'entry_id' => $row['entry_id'],
'date' => date('Y-m-d',$row['entry_date']),
'author' => $row['screen_name'],
'payment' => $payment_amount,
'status' => $status,
'title' => $edit_href.$row['title']."</a>"
);
}
return $datarows;
}
答案 0 :(得分:2)
你的datasource
回调函数必须在Module_mcp
类上(查看你试图在插件上使用它的论坛帖子,这可以解释错误)。
如果要将数据源方法放在不同的类上,那么只需在调用datasource()
之前添加此行,以便使用正确的类来欺骗表库:
// ensure table callbacks use this class rather than our MCP file
$this->EE->_mcp_reference =& $this;
$data = $this->EE->table->datasource('_datasource');
table
和form_validation
库是使用特殊_mcp_reference
变量的唯一两个库,因此我无法看到更改它的任何副作用,并且已成功完成此操作至少有两个模块。
另外,如果您想了解如何使用内置tablesorter的一个很好的示例,请查看system/expressionengine/controllers/cp/members.php
。文档非常糟糕,但源代码总是说实话:)
答案 1 :(得分:2)
我一直遇到问题,并且有一个generate()和datasource工作的混合解决方案。在这里:
在我的mcp文件中:
public function index()
{
$this->EE->cp->set_variable('cp_page_title', lang('my_module_name'));
$data = $this->EE->table->datasource('_datasource');
return $this->EE->load->view('index', $data, TRUE);
}
public function _datasource()
{
$headers = array(
'name' => array('header' => 'Name'),
'color' => array('header' => 'Color'),
'size' => array('header' => 'Size')
);
$rows = array(
array('name' => 'Fred', 'color' => 'Blue', 'size' => 'Small'),
array('name' => 'Mary', 'color' => 'Red', 'size' => 'Large'),
array('name' => 'John', 'color' => 'Green', 'size' => 'Medium'),
);
return array(
'rows' => $rows,
'headers' => $headers
);
}
在我的索引视图文件中:
$this->table->set_columns($headers);
$this->table->set_data($rows);
echo $this->table->generate();
此刻似乎正在工作,我还没有尝试分页,但排序工作。