This patch does two things.

First, adds an ID to the table element in the form, so that it's easier to point to using javascript.

Seconds, adds table headers to the top (A,B,C,D,...) and left side (1,2,3,4,...) and hides input field borders, so that the feel is more spreadsheet-like.

The grand purpose of this is that I need to get horizontally-resizable columns (as one project I'm coding will use TableField with moderate-length strings), and javascript libraries usually hook into thead to do their magic.

I'm aware that this patch is totally uncalled for and that it might not make it to mainline.

CommentFileSizeAuthor
tablefield-spreadsheet.patch3.04 KBptaff
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

weseze’s picture

I'd like to thank you for your patch. It was really useful for my project. You can find the patch of my project with your patch included in it at http://drupal.org/node/1094568

mauriziopinotti’s picture

A cleaner solution could be adding a custom format to tablefield, and defining it in our own module.

PROS: no modifications to tablefield module

CONS: the table header is not a "th" tag but a "td" with a bold font

HOWTO: my module is named "casaconvenienza" (so search and replace it with your module name), so I added a "Tabular View (vertical headers)" with hook_field_formatter_info():

function casaconvenienza_field_formatter_info() {
  return array(
    'casaconvenienza_tablefield_vertical_headers' => array(
      'label' => t('Tabular View (vertical headers)'),
      'field types' => array('tablefield'),
    ),
  );
} // casaconvenienza_field_formatter_info

then I defined my hook_field_formatter_view for the given formatter copying and slightly modifying the tablefield function:

function casaconvenienza_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // casaconvenienza_tablefield_vertical_headers
  if ($display['type'] == 'casaconvenienza_tablefield_vertical_headers') {
    // *** CUT && PASTE FROM TABLEFIELD MODULE ***
    $settings = $display['settings'];
    $formatter = $display['type'];

    foreach ($items as $delta => $table) {

      // Rationalize the stored data
      if (!empty($table['tablefield'])) {
        $tabledata = tablefield_rationalize_table($table['tablefield']);
      }
      elseif (!empty($table['value'])) {
        $tabledata = tablefield_rationalize_table(unserialize($table['value']));
      }

      // Run the table through input filters
      if (isset($tabledata)) {
        if (!empty($tabledata)) {
          foreach ($tabledata as $row_key => $row) {
            foreach ($row as $col_key => $cell) {
              if (!empty($table['format'])) {
                $tabledata[$row_key][$col_key] = array('data' => check_markup($cell, $table['format']), 'class' => array('row_' . $row_key, 'col_' . $col_key));
              }
              else {
                if (intval($cell) != 0) $cell .= '%';
                $tabledata[$row_key][$col_key] = array('data' => check_plain($cell), 'class' => array('row_' . $row_key, 'col_' . $col_key));
              }
            }
          }
        }

        // Pull the header for theming <-- *** THIS IS THE ONLY CUSTOMIZATION ***
        $header = array(); //array_shift($tabledata);

        // Theme the table for display
        $element[$delta]['#markup'] = theme('tablefield_view', array('header' => $header, 'rows' => $tabledata, 'delta' => $delta));
      }
    }
  } // casaconvenienza_tablefield_vertical_headers

  return $element;
} // casaconvenienza_field_formatter_view

This actually removes the table headers, then I put the "strong" font with CSS:

.tablefield .col_0 {
  font-weight: bold !important;
}
Liam Morland’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

Drupal 6 is no longer supported. If this issue still exists in later versions, please re-open and update the version.