2

我目前正在开发一个自定义模块插件,我希望能够在我的控制面板管理员的表格上使用排序和过滤。我正在使用 EE 表类和表单助手。我正在尝试按照此处的文档进行设置,但是当我调用时尝试在我的类中调用“_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()' 方法中,您应该只传递一个带有数据源函数名称的字符串值,这就是我正在做。

我似乎不是唯一一个遇到这个问题的人。此 EE 讨论论坛线程上有更多详细信息和代码示例

文档不是很清楚。我还尝试查看 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;
} 
4

2 回答 2

2

I've been having issues too and have a mixed solution of generate() and datasource working. Here it is here:

In my mcp file:

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

In my index view file:

$this->table->set_columns($headers);
$this->table->set_data($rows);
echo $this->table->generate();

Seems to be working at the moment and I've not tried pagination yet, but sorting works.

于 2013-03-18T10:58:36.320 回答
2

Your datasource callback function must be on your Module_mcp class (looking at your forum thread you are trying to use it on a plugin which would explain the error).

If you want to put the datasource method on a different class, then just add this line right before you call datasource() to trick the table library into using the correct class:

// ensure table callbacks use this class rather than our MCP file
$this->EE->_mcp_reference =& $this;
$data = $this->EE->table->datasource('_datasource');

The table and form_validation libraries are the only two which use the special _mcp_reference variable, so I can't see any side effects to changing it, and have successfully done this in at least two modules.

On a side note, if you want a good example of how to use the built in tablesorter, take a look at system/expressionengine/controllers/cp/members.php. The documentation is pretty bad, but the source code always tells the truth :)

于 2012-11-17T02:59:39.567 回答