PEAR Manual
Prev |  | Next

# Column Formatter

Column Formatter -- What can I do with the column formatter?

## Description

The column formatter method can be a very powerful solution to a very common need. The need is to customize the output for a cell in the grid such as a link for a form element. This can be easily done by specifying a "callback" function. This function will then return the string that is needed to be printed.

Example 55-1. Using the column formatter
    <?php
          require_once 'Structures/DataGrid.php';

          $dg =& new Structures_DataGrid();
          $dg->bind('http://pear.php.net/feeds/pkg_structures_datagrid.rss', null, 'RSS');

          $dg->addColumn(new Structures_DataGrid_Column('Release', 'title', 'title', null, null, 'printLink()'));
          $dg->addColumn(new Structures_DataGrid_Column('Description', 'description', 'description', null, null, 'printDesc($length=150)'));
          $dg->addColumn(new Structures_DataGrid_Column('Date', 'dc:date', 'dc:date'));
          $dg->render();

          function printLink($params)
          {
              extract($params);
              return '<a href="' . $record['link'] . '">' . $record['title'] . '</a>';
          }

          function printDesc($params)
          {
              extract ($params);
              if (strlen($record['description']) > $length) {
                  return nl2br(substr($record['description'], 0, $length)) . '...';
              } else {
                  return nl2br($record['description']);
              }
          }
          ?>

Prev | Home | Next
Rendering the output | Up | Example - Quick