We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Need help with sortable tables and datasource()

Development and Programming

Michael C. (ProImage)'s avatar
Michael C. (ProImage)
102 posts
13 years ago
Michael C. (ProImage)'s avatar Michael C. (ProImage)

EE 2.5.3

I’m developing a fieldtype that will display a table of options to choose from, using a checkbox for each row. The table is pretty long, so I’d like to allow sorting on the table columns, as well as pagination, filtering, etc. I’m using the Table class stuff as documented here.

I’ve got sorting working (too) well; it’s sorting via all 3 columns, despite my having specified not to allow sorting via one of them (the last one):

$this->EE->table->set_columns(array(
    'code' => array('header' => 'Code'),
    'name'  => array('header' => 'Name'),
    '_check'  => array('header' => form_checkbox(), 'sort' => false)
));

That’s problem one.

Problem two is that I can’t get the whole datasource thing working at all. It seems like no matter what I try, I get the following PHP error message:

Fatal error: Call to undefined method Content_publish::_datasource() in /home/public_html/system/expressionengine/libraries/EE_Table.php on line 162

My fieldtype’s code looks basically like this:

class Fieldtype_name_here extends EE_Fieldtype {

    /*
    Various functions...
    */
    
    function display_field($data)
    {
        $this->EE->load->library('table');
        
        $this->EE->table->set_columns(array(
            'code' => array('header' => 'Code'),
            'name'  => array('header' => 'Name'),
            '_check'  => array('header' => form_checkbox(), 'sort' => false)
        ));
        $table_stuff = $this->EE->table->datasource('mc_source');
        return $table_stuff['table_html'];
    }

    function _datasource($state)
    {
        $rows = array(
            array('name' => 'Fred', 'code' => 'Blue',  '_check' => '[]'),
            array('name' => 'Mary', 'code' => 'Red',   '_check' => '[]'),
            array('name' => 'John', 'code' => 'Green', '_check' => '[]')
        );

        return array(
            'rows' => $rows
        );
    }
    
    /*
    Even more functions...
    */
    
}

I’ve searched all over the place, but to no avail. Any takers?

       
Michael C. (ProImage)'s avatar
Michael C. (ProImage)
102 posts
13 years ago
Michael C. (ProImage)'s avatar Michael C. (ProImage)

bump

       
Adam Christianson's avatar
Adam Christianson
115 posts
13 years ago
Adam Christianson's avatar Adam Christianson

Did you ever get this figured out? I’ve having a similar issue with a module. I don’t get an error, but I can’t get my data to display once I’ve set-up the columns and data according to the ‘Named Columns’ section of the User Guide. Here is the code I’m using in my view:

$this->table->set_template($cp_table_template);
    $this->table->set_columns(
        array(
            'id'                => array('header' => lang('id')),
            'date'              => array('header' => lang('date')),
            'author_name'       => array('header' => lang('author_name')),
            'payment_amount'    => array('header' => lang('payment_amount')),
            'status'            => array('header' => lang('status')),
            'title'             => array('header' => lang('title'))
        )
    );

    $dataset = array();
    foreach($results as $result)
    {
        $dataset[] = 
            array(
                'id' => $result['entry_id'],
                'date' => $result['date'],
                'author_name' => $result['author'],
                'payment_amount' => $result['payment'],
                'status' => $result['status'],
                'title' => $result['title']
            );
    }
    $this->table->set_data(array($dataset));

     echo $this->table->generate();

In my case I do get column headers that are sortable, but the dataset never displays. According to the documentation it claims that “If you only need single page sorting, this function lets you set the named column data directly”, so in my case I believe calling

$this->table->set_data(array($dataset));

should be enough and I shouldn’t have to set up a _dataset method in my Module class, right?

       
Adam Christianson's avatar
Adam Christianson
115 posts
13 years ago
Adam Christianson's avatar Adam Christianson

I also wanted to mention that I’m currently on EE 2.5.2 and I did run into this bug since I’m not using the asynchronous data retrieval via a datasource. It says the bug is “fixed”, so I assume that’s in EE 5.2.3?

Undefined property: EE_Table::$raw_data
       
Adam Christianson's avatar
Adam Christianson
115 posts
13 years ago
Adam Christianson's avatar Adam Christianson

OK. I figured out my issue. When I was calling

$this->table->set_data()

I was passing my ‘$dataset’ as an array

$this->table->set_data(array($dataset))

because set_data expects the data as an array or arrays. The thing was that $dataset was already defined earlier in my code as an array. I simply had too much array nesting.

       
Adam Christianson's avatar
Adam Christianson
115 posts
13 years ago
Adam Christianson's avatar Adam Christianson
EE 2.5.3 Problem two is that I can’t get the whole datasource thing working at all. It seems like no matter what I try, I get the following PHP error message:
Fatal error: Call to undefined method Content_publish::_datasource() in /home/public_html/system/expressionengine/libraries/EE_Table.php on line 162

to help you out with this, I think the trouble is the method name you’re passing to

$this->EE->table->datasource()

.

Your code is:

$table_stuff = $this->EE->table->datasource('mc_source');

So the callback is looking for a datasource function called ‘mc_source’, but in the code example you posted your datasource function is called ‘_datasource’.

function _datasource($state)
    {
        $rows = array(
            array('name' => 'Fred', 'code' => 'Blue',  '_check' => '[]'),
            array('name' => 'Mary', 'code' => 'Red',   '_check' => '[]'),
            array('name' => 'John', 'code' => 'Green', '_check' => '[]')
        );

        return array(
            'rows' => $rows
        );
    }

That’s why the error is stating ‘Call to undefined method’. If you change the function name to ‘mc_source’ it should work I think.

function mc_source($state)
    {
        $rows = array(
            array('name' => 'Fred', 'code' => 'Blue',  '_check' => '[]'),
            array('name' => 'Mary', 'code' => 'Red',   '_check' => '[]'),
            array('name' => 'John', 'code' => 'Green', '_check' => '[]')
        );

        return array(
            'rows' => $rows
        );
    }
       
Adam Christianson's avatar
Adam Christianson
115 posts
13 years ago
Adam Christianson's avatar Adam Christianson

Ok, my answer above may or may not work. I just tried implementing it myself for my project and now I get a similar error even when I do have the correct function defined in my class.

Fatal error: Call to undefined method Addons_modules::_datasource() in /home/public_html/system/expressionengine/libraries/EE_Table.php on line 162

I’m calling the method like this:

$data = $this->EE->table->datasource('_datasource');

and have this method in my class:

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;
}
       
Michael C. (ProImage)'s avatar
Michael C. (ProImage)
102 posts
13 years ago
Michael C. (ProImage)'s avatar Michael C. (ProImage)

Would you believe this is still kicking my rear? I’ve just about given up on getting the _datasource() thing to work, but disabling the sorting is something I have to have.

       
gelviis's avatar
gelviis
55 posts
12 years ago
gelviis's avatar gelviis

Had similar issues. Posted a solution here: http://stackoverflow.com/questions/13406966/expression-engine-module-tables-and-datasort/15475177#15475177

Might help!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.