4

I'm building a fieldtype add-on, and it presents the data contained therein via a table. I'm using EE's Table class to generate the thing, and it's mostly working fine - columns are sortable, etc. However, one column is a checkbox (for selecting that row), and obviously I don't want it to be able to sort by that column.

According to the Table class documentation, I should be able to disable sorting like this:

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

When viewing the generated table, it looks like it's doing the right thing (the "no-sort" class):

<table cellspacing="0" cellpadding="0" border="0" class="mainTable" data-table_config="{removed for brevity}">
    <thead>
        <tr class="odd">
            <th data-table_column="code" class="header">Code</th>
            <th data-table_column="name" class="header">Name</th>
            <th class="no-sort header" data-table_column="_check">
                <input type="checkbox" class="toggle_all" value="true" name="select_all">
            </th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

However, the table is still sortable by the checkbox column.

With javascript disabled, the output code is:

<table cellspacing="0" cellpadding="0" border="0" class="mainTable" data-table_config="{removed for brevity}">
    <thead>
        <tr>
            <th data-table_column="code">Code</th>
            <th data-table_column="name">Currency Name</th>
            <th class="no-sort" data-table_column="_check">
                <input type="checkbox" class="toggle_all" value="true" name="select_all">
            </th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

I've looked at tables that have sorting disabled for one column (the Content -> Edit table of entries being the prime example), and it appears that the "header" class that is getting added to my table via javascript might be at fault, and the Entries table doesn't have that:

<thead>
    <tr class="even">
        <th data-table_column="entry_id">#</th>
        <th data-table_column="title">Title</th>
        <th class="no-sort" data-table_column="view">View</th>
        <th data-table_column="comment_total">Comments</th>
        <th data-table_column="screen_name">Author</th>
        <th data-table_column="entry_date" class="headerSortUp">Date</th>
        <th data-table_column="channel_name">Channel</th>
        <th data-table_column="status">Status</th>
        <th class="no-sort" data-table_column="_check">
            <input type="checkbox" class="toggle_all" value="true" name="select_all">
        </th>
    </tr>
</thead>

How do I do this properly?

EDIT: As requested, here's the full table generation code (simplified by removal of conditionals, etc):

$this->EE->table->set_columns(array(
    'code'      => array('header' => lang('code')),
    'name'      => array('header' => lang('name')),
    '_check'    => array(
        'header' => form_checkbox('select_all', 'true', FALSE, 'class="toggle_all"'),
        'sort' => FALSE
    )
));

$table_data = array();

// Iterate over the complete list of items to build each table row
foreach ($this->items as $code => $label)
{
    // Is $code found in the $data array?
    $checked = (in_array($code, $data)) ? TRUE : FALSE;

    // Prepare checkbox parameters
    $params = array(
        'name'      => $field_name.'[]',
        'id'        => $code,
        'value'     => $code,
        'checked'   => $checked,
        'class'     => 'toggle'
    );
    // Add row as array to table array
    $table_data[] = array(
        'code'      => form_label($code,$code),
        'name'      => form_label($label,$code),
        '_check'    => form_checkbox($params)
    );
} // End table rows loop

// With all rows prepared, buid complete table
$this->EE->table->set_data($table_data);
return $this->EE->table->generate();
2
  • I can't see anything immediately wrong with your code. Another good example of using the table class is in system/expressionengine/controllers/cp/members.php. How are you displaying your table in your view file? Commented Jan 14, 2013 at 17:38
  • @AdrianMacneil I've edited the OP with the requested code sample. I think. I never actually properly learned what a "view file" is. :p
    – Sandwich
    Commented Jan 14, 2013 at 22:41

1 Answer 1

1

I've done this before by removing the javascript sort function in my own addon's jquery file.

$('table.mainTable > thead th.no-sort').unbind();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.