How to show the Primary Key ID in a simple Fuel Module view in the Admin

edited March 2012 in Modules
Is there a way to have the ID field show up in the record listing of a simple module in the Fuel admin? Say I have a module called "sample" that has an ID field that is set to auto-increment and is the primary key. I would like this column to show up in the Fuel admin area so that the column can be sorted upon and referenced to each record. I have a module configuration setup as follows:

$config['modules']['sample'] = array( 'module_name' => 'Sample', 'module_uri' => 'sample', 'model_name' => 'sample_model', 'model_location' => 'sample', 'table_headers' => array( 'id', 'field1', 'field2', 'field3' ) );

Under the table_headers option, including id basically does nothing. I am wondering if there is a hack to get that ID column to show up but not show up on the Edit form for the module record form to prevent people from entering in a value for ID field to prevent the obvious, which is to input a number that is already taken, which would generate a database error.

Thanks,
Erik

Comments

  • edited March 2012
    Sure thing:

    Add a field 'display_id' in your config.

    In your model:

    function list_items($limit = NULL, $offset = NULL, $col = 'name', $order = 'asc') { $this->db->select('*, id AS display_id'); $data = parent::list_items($limit, $offset, $col, $order); return $data; }

    Display_id wont show up in an edit form.

    Change the $col to a field that suits your schema.
  • edited March 2012
    Thanks, I got the id to show up in the list view, which is good (check). However, I need it to show up and be sortable, so here is my modified code that successfully displays the id column, but now none of the columns are sortable:

    function list_items() { $this->db->select('*, id AS "Invoice Number"', FALSE); $data = parent::list_items(); return $data; }

    To give you more background on this, the table in question holds invoices, so the ID field is the main way to track each record (there is no "name" field equivalent).

    I basically want to show the id column (which is the primary key) all the way on the left side of the list view of the module, allow it to be sortable and not break the sortability of each of the other columns and to sort descending by default, but so far I have not been able to accomplish that despite multiple tweaks to the list_items function.

    Here is the code I have so far that displays either a SQL error or won't show any records at all:

    function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'desc') { $this->db->select('id, id AS "Invoice Number", invoice_date, status, corporate_partner_id, amount_due, transaction_id, payment_date, amount_paid'); $data = parent::list_items($limit, $offset, $col, $order); return $data; }

    Any hints?
  • edited March 2012
    Yep, it'll be sortable by display_id with what I gave you.

    In you're first example, all the fields from the method signature have gone. That'll break all the filters.

    If you use mine but change the default $col to display_id it'll work. That $col value is the default it'll use if there's nothing sent to it. Other suitable fields are usually things like date_added or published (maybe you're invoice_date or status fields would work).

    With your second example, whats the SQL error? It's likely because you've lost the false from the end of the select. If you're getting all fields from that row, you can use * instead of specifying them individually.

    You sure you want to use the primary key for your invoice identifiers? Usually a better idea is to either combine those with a date stamp of some sort or I commonly use a short 6 character hash.
  • edited 12:47PM
    Awesome, thanks for all the tips. I got it to work successfully based on all the pointers you gave. Once again, thanks to the excellent Fuel team for your support!
  • edited September 2019

    Hi Erik,

    This is old post but it's worth of trying. I have same requirement as you, to show ID in the list. Everything works fine, except I lost "edit | delete" button at the end. Click on the list item is gone as well, I can not open and edit the item on the list. Do you know how to fix that?

Sign In or Register to comment.