Welcome Guest, Not a member yet? Register   Sign In
Setting tr id's per table row
#1

[eluser]sandwormusmc[/eluser]
Is there any good way to have the HTML Table class assign a different tr id for every row, based on other data provided by function calls and available data?

For example I set a template for the row in a table, then need to reference that table using JavaScript with a specific ID. I haven't found anything that will allow me to dynamically set a specific row's ID to whatever I want.

For now I'm going to hack the HTML Table add_row function to accept a second variable, which will set an ID for that row, but I hate hacking CI's internal code just for the sake of staying consistent with CI's release code.

If that makes sense, does anyone have ideas how I can accomplish that otherwise?

Thanks!
#2

[eluser]spheroid[/eluser]
Will the ID's be numerical values in incremental order (1,2,3...)? If not, what type of list of possible values will they be?
#3

[eluser]sandwormusmc[/eluser]
The IDs will be based off of a database field (host_id in this case). For this specific implementation, I'm basically listing a set of hosts, each of which have an autoincremented field assigned as an identifier.

So unfortunately I can't predict what the ID will be by easily incrementing a variable within PHP, it will have to be based on a set of results.
#4

[eluser]spheroid[/eluser]
Here's how I'd tackle it:

Controller:

Code:
function view()
{
   $this->load->model('Host_model', '', TRUE);
   $data['query'] = $this->Host_model->findAllHosts();

   $data['head_title'] = 'View Hosts';
   $this->template->load('mainTemplate', 'hosts/view_all_hosts', $data);
}

Model:

Code:
function findAllHosts()
{
   $this->db->select('*');
   $this->db->from('hostname_table');
        
   $query = $this->db->get();
   return $query;        
}

In your View:

Code:
<table>
  <tr>
   <th width="70">HOST NAME</th>
   <th width="100">IP ADDRESS</th>
  </tr>

  &lt;?php foreach($query->result() as $row): ?&gt;
  <tr id="&lt;?php echo $row->host_id; ?&gt;">
   <td>&lt;?php echo $row->host_name; ?&gt;</td>
   <td>&lt;?php echo $row->ipaddress; ?&gt;</td>
  </tr>
  &lt;?php endforeach; ?&gt;

</table>
#5

[eluser]sandwormusmc[/eluser]
Yeah, guess that would work. I was hoping to stick to the CI HTML Table Class to keep the template and alternate rows intact, but that may not be an option, as I've found hacking the Table Class is harder than it sounds regarding the effort involved.

I'm trying to get through it using JavaScript DOM traversal, but no luck there yet, either. If I figure it out I'll post my results, but more comments are definitely welcome in the mean time.

Thanks, spheroid.
#6

[eluser]Michael Wales[/eluser]
You can still use the alternator within a foreach loop.
#7

[eluser]sandwormusmc[/eluser]
[quote author="walesmd" date="1196124614"]You can still use the alternator within a foreach loop.[/quote]

Interesting. What's the syntax? I've only used the alternate through setting a template array ...
#8

[eluser]Michael Wales[/eluser]
Code:
foreach ($rows as $row) {
  echo '<tr id="' . alternator('odd', 'even') . '">
            <td>' . $row->name . '</td>
            <td>' . $row->created_on . '</td>
        </tr>';
}
#9

[eluser]xwero[/eluser]
jquery to rescue. Id's aren't allowed because they have to be unique on the page
Code:
$("tr:nth-child(odd)").addClass("odd");
#10

[eluser]sandwormusmc[/eluser]
Hmm. This unfortunately isn't just an alternating color problem. It would have been _nice_ to keep that to keep my code consistent, using the tmpl array on each view page. The real problem here (and I probably didn't explain it well) is I wanted to do an refresh-less edit/list page for all of my results.

So, I have a column with the list of available actions and their icons all the way to the left. The ID of this element is set within a div in the format of cellname_id. So for actions, the div id is actions_104, for example. The next cell has content with another div, called hostid_104, for example. When I'm doing my AJAX calls, I split on the underscore to determine the action, and the identifier of the row to work on.

So, I was hoping to have an icon to click that would change the content (innerHTML, for lack of better wording) of the entire row of that result from the static HTML to form elements. This is why I was really hoping to be able to simply set the IDs of each individual row of results (each TR). Doesn't look like CI worked for that, but I'm playing with MOOdalBox now to bring up a box with the edit form within it, instead of trying to replace the contents of each cell in that row.

Now I just have to figure out how to pass the id of the result I need to edit. Should be a bit easier than trying to coerce CI to dynamically assign ids to each result using the Table Class.

Also, walesmd, I'm not sure I understand your reply. First, IDs have to be unique on a page, as xwero pointed out. Second, I'm used to seeing function calls within views as $this->CI->whatever()... Last time I tried to put a function in a view I got nothing but error messages. Is "alternator()" something you meant I should declare within the controller, or a CI built-in I missed somewhere?




Theme © iAndrew 2016 - Forum software by © MyBB