Showing subtables

edited January 2014 in Modules
Hi

Any hints for this would be appreciated.

I have a table called "computers" which lists computer models and a sub-table which lists their "components":
id
computers_id // the id of the parent in "computers"
component_type_id // the id of the component type lookup table, could be "motherboard", "graphics card" etc.
component_name // the component name, varchar 256

In the simple module for "computers", I'd like to show all the related records in the "components" table and be able to add/delete etc. I want to be able to show the component_type_id and component_name where component_type_id is also resolved to the textual name from the lookup.

Is there an easy way to do this?

Thanks

Comments

  • edited 11:28PM
    I don't have an example where you can edit the components in a modal window but the following does provide a list of components in a separate section called "Components" with links to edit them. There is a script tag that you can put your own custom javascript if you need it including something like opening up the components edit window in modal iframe window.
    function form_fields($values = array(), $related = array()) { $fields = parent::form_fields($values, $related); // ADD YOUR CUSTOM MODEL FORM CODE HERE if (!empty($values['id'])) { $CI =& get_instance(); $CI->load->model('computers_model'); $components = $CI->computers_model->options_list('id', 'name', array('computers_id' => $values['id'])); $str = ''; foreach($components as $key => $c) { $edit_uri = ($CI->fuel->admin->is_inline()) ? 'inline_edit' : 'edit'; $str .= '<a href="'.fuel_url('components/'.$edit_uri.'/'.$key).'">' . $c . '</a><br>'; } $create_uri = ($CI->fuel->admin->is_inline()) ? 'inline_create' : 'create'; $str .= '<br><br><a href="'.fuel_url('components/'.$create_uri).'" class="btn_field">Add</a><br>'; $fields['Components'] = array('type' => 'section', 'order' => 100); $fields['components'] = array('type' => 'copy', 'value' => $str, 'order' => 101, 'js' => '<script>$(function(){ // add your javascript here });</script>'); } return $fields; }
  • That's a great example, thanks. It would be nice for the add & edit buttons to popup modally like some other fields do...

    Is it possible to pass extra values via the add button? ie. my add button takes me to the components child form where the "computer_id" field is already populated from the parent form.
  • edited 11:28PM
    You can use query string parameters to pass values (e.g. ?computer_id=1)
  • groovy. works a treat, thanks.

    I'll come back to setting this up to work as a modal - will post my code when it works...
Sign In or Register to comment.