Table view - row numbering

alkam - August 8, 2007 - 11:00

Hi. Is any simple method table view shows row number (# 1, 2, 3, ....) in table as first column? I not found that field type or view type for View module. THX. Alek.

Well, hope that this is simple enough

wuf31 - August 21, 2007 - 06:47

There may be a simpler method, but this is what works for me.

1. Create a new custom module (I just call it custom.module)
2. Put the code below in it

<?php
function theme_views_view_table_addrownumber($view, $nodes) {
 
// Insert a No Header Column
 
array_unshift(
   
$view->table_header,
    array(
'data' => 'No', 'class' => 'view-cell-header view-field-row-number'));
       
 
// Get All Fields
 
$fields = _views_get_fields();

 
// Build Rows
 
$count = 0;
  foreach (
$nodes as $node) {   
   
$row = array();
   
// First Column is Row Numbering
   
$count += 1;
   
$cell['data']  = $count + $_GET['page'] * $view->nodes_per_page;
   
$cell['class'] = 'view-field ' . 'view-field-row-number';
   
$row[] = $cell;
   
// Loop the rest of fields
   
foreach ($view->field as $field) {
      if (
$fields[$field['id']]['visible'] !== FALSE) {
       
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
       
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
       
$row[] = $cell;
      }
    }
   
$rows[] = $row;
  }
 
  return
theme('table', $view->table_header, $rows); 
}
?>

3. For every views that u want to add row numbering, simply add this code

<?php
 
function theme_views_view_table_<YOUR_VIEW_NAME>($view, $nodes) {
    return
theme_views_view_table_addrownumber($view, $nodes);
?>

-=- Jhon -=-

For Views 2 on Drupal 6

wuf31 - September 16, 2008 - 05:24

In Views 2 this has been made so simple :P

This is a simple modification from the default views theme tpl

Just put this views-view-table.tpl.php into your theme folder.

<table class="<?php print $class; ?>">
  <?php if (!empty($title)) : ?>
    <caption><?php print $title; ?></caption>
  <?php endif; ?>
  <thead>
    <tr>
      <th>No</th>
      <?php foreach ($header as $field => $label): ?>
        <th class="views-field views-field-<?php print $fields[$field]; ?>">
          <?php print $label; ?>
        </th>
      <?php endforeach; ?>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($rows as $count => $row): ?>
      <tr class="<?php print ($count % 2 == 0) ? 'even' : 'odd';?>">
        <td><?php print $count+1+($view->pager['current_page']*$view->pager['items_per_page']); ?></td>
        <?php foreach ($row as $field => $content): ?>
          <td class="views-field views-field-<?php print $fields[$field]; ?>">
            <?php print $content; ?>
          </td>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

PS: Remember to clear drupal cache

EtradingGallery

-=- Jhon -=-

don't get it

geraldito - May 17, 2009 - 10:18

sorry, but I don't get it. put the above code for views2 into views-view-table.tpl.php substiting "print $rows" part but get "Invalid argument supplied for foreach()" for the two foreach() used in php.

Well, then try to put the

wuf31 - June 2, 2009 - 09:15

Well, then try to put the original file from views/theme/views-view.table.tpl.php into your theme folder.
And then remember to clear views cache. See if it still fail on the foreach.

If it works, then the above code should work, but then I haven't got time to check it againts recent views2.

EtradingGallery

-=- Jhon -=-

Thanks Jhon, this just

kvaes - October 24, 2007 - 11:55

Thanks Jhon, this just helped my out a great bunch!

http://www.kvaes.be - http://www.artistiku.com

nice tip… simply adding it

g10 - February 17, 2008 - 18:29

nice tip…
simply adding it to the template.php of your theme works also

The Views Custom Field module

Junro - June 2, 2009 - 09:23

The Views Custom Field module does that. :)

Really simple to use.
You can set rownumber field everywhere you need it.

Excellent, now why don't I

wuf31 - June 3, 2009 - 03:30

Excellent, now why don't I see that before ?

There's whole lots of small useful module out there hiding somewhere ;)

EtradingGallery

-=- Jhon -=-

Hi Wuf, exactly! lol there is

Junro - June 3, 2009 - 07:26

Hi Wuf, exactly! lol there is a lot of useless module too! lol

 
 

Drupal is a registered trademark of Dries Buytaert.