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.

Comments

wuf31’s picture

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

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

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

-=- Jhon -=-

wuf31’s picture

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 -=-

geraldito’s picture

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.

wuf31’s picture

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.

ericinwisconsin’s picture

This works, but I also get this error:

Notice: Undefined variable: class in include() (line 1 of /var/www/website/sites/all/themes/drupalace/views-view-table.tpl.php).

kvaes’s picture

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

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

g10’s picture

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

Junro’s picture

The Views Custom Field module does that. :)

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

wuf31’s picture

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

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

Junro’s picture

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

Vasu’s picture

Add a field called 'view result counter' (under 'global' in the selection list) and move it to top position using sort.

bulat’s picture

+1

h3rj4n’s picture

+1

rohanchopra1’s picture

+1