skip to Main Content

I have a problem with my code.

I am currently loading my list of accounts through a database and generating a table with Codeigniters library, table.

$this->load->library('table');

$this->db->select('firstname, lastname, accountname, registerdate, birthdate, email');
$this->db->from('accounts');

$query = $this->db->get();

$template = array('table_open' => '<table class="table table-striped table-hover">');
$headings = array( 'Fornavn', 'Efternavn', 'Brugernavn', 'Registeringsdato', 'Fødselsdag', 'Email');
$this->table->set_template($template);
$this->table->set_heading($headings);

return $this->table->generate($query);

I would like a modal to open when clicking on the table row. My only headtwister is how I pass any data, like userid, to the modal opened? I can change the table template to open a modal but I cannot figure out how to pass the data. Since the data should be an id which changes from row to row, if you catch my drift.

I hope theres some smart brain out there who can help me!

Best regards
OrKarstoft

2

Answers


  1. Initialize some id for 'table-open'. Then use jQuery on view side, when table is ready to insert attribute. Try this:

    //controller

    $template = array('table_open' => '<table class="table table-striped table-hover" id="myTable">');
    

    //view

    $(document).ready(function(){
        $("#myTable tr").each(function(index){
            $(this).attr('id', 'row_' + index);
        });
    });
    
    Login or Signup to reply.
  2. You will need to do this in the view with javascript!

    Basic concept:

    • Attach a click listener to the table rows.
    • Use data- tags on the <tr>‘s or DOM traversal to collect the row’s data that you want to send to the modal.
    • Write the data to your modal template
    • Show the modal.

    I put together a jsFiddle for you to illustrate all the parts. Working Example

    To add the extra tags into your rows using the codeigniter table library, you are going to have to change the way you generate the table a little bit.

    foreach($query as $row)
    {
        $this->table->add_row('see codeigniter docs');
    }
    $this->table->generate();
    

    Codeigniter add_row() Documentation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search