From d63e2a21611900b837f2cbb6557538e42d04aacf Mon Sep 17 00:00:00 2001
From: Dan Chadwick <dan899@gmail.com>
Date: Sat, 10 Oct 2015 10:18:25 -0400
Subject: [PATCH] Issue #2581891 by DanChadwick: Added advanced grid (multiple
 choice, conditionals, non-homogeneous options).

---
 components/grid.inc             | 298 +++++++++++++++++++++++++++++++++-------
 components/select.inc           |   9 ++
 includes/webform.components.inc |  14 +-
 js/webform-admin.js             |  15 --
 js/webform.js                   |  18 +++
 webform.api.php                 |   4 -
 webform.module                  |   5 +
 7 files changed, 283 insertions(+), 80 deletions(-)

diff --git a/components/grid.inc b/components/grid.inc
index 3d7eb19..8404ceb 100644
--- a/components/grid.inc
+++ b/components/grid.inc
@@ -1,4 +1,4 @@
-<?php
+﻿<?php
 
 /**
  * @file
@@ -37,7 +37,6 @@ function _webform_defaults_grid() {
   );
 }
 
-
 /**
  * Implements _webform_theme_component().
  */
@@ -60,14 +59,37 @@ function _webform_theme_grid() {
 function _webform_edit_grid($component) {
   $form = array();
 
+  $form['help'] = array(
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => !empty($component['cid']),
+    '#title' => t('About options and questions&hellip;'),
+    '#description' => t('Options and questions may be configured here, in additional nested Select Options components, or even both.'),
+    '#weight' => -4,
+    'pros_and_cons' => array(
+      '#theme' => 'table',
+      '#header' => array('', t('Options and questions configured <strong>here</strong>'), t('Configured in additional <strong>nested</strong> components'), t('Both')),
+      '#rows' => array(
+        array(t('Questions'), t('Enter the questions below.'), t('Configure and save this grid, then add additional Select Options components nested (indented) below this grid.'), t('Additional questions from nested components will be displayed below any questions configured here.')),
+        array(t('Options'), t('Enter options below.'), t('May be different for each question. Initially the same as defined below.'), t('Options from additional nested components will be merged with any options configured here.')),
+        array(t('Checkboxes'), t('No. Radio buttons only.'), t('Yes. Some or all questions may be multiple choice with check boxes.'), ''),
+        array(t('Default'), t('Yes. Must be same for all questions.'), t('Yes. May all be the same or different.'), ''),
+        array(t('Pre-built option lists'), t('No.'), t('Yes.'), ''),
+        array(t('Required'), t('Yes. Must be same for all questions.'), t('Yes. May all be the same or different.'), ''),
+        array(t('Question conditionals'), t('No.'), t('Yes. Individual questions may be used in conditional rules and/or actions.'), t('The whole grid may be conditionally shown or required.')),
+        array(t('Other types of nested components'), t('No.'), t('Yes. Other component types may also be included in the grid. They will be displayed where the options would normally be.'), ''),
+      ),
+    ),
+  );
+
   if (module_exists('options_element')) {
     $form['options'] = array(
       '#type' => 'fieldset',
       '#title' => t('Options'),
       '#collapsible' => TRUE,
-      '#description' => t('Options to select across the top. Usually these are ratings such as "poor" through "excellent" or "strongly disagree" through "strongly agree".'),
       '#attributes' => array('class' => array('webform-options-element')),
       '#element_validate' => array('_webform_edit_validate_options'),
+      '#weight' => -3,
     );
     $form['options']['options'] = array(
       '#type' => 'options',
@@ -79,15 +101,16 @@ function _webform_edit_grid($component) {
       '#key_type_toggle' => t('Customize option keys (Advanced)'),
       '#key_type_toggled' => $component['extra']['custom_option_keys'],
       '#default_value_pattern' => '^%.+\[.+\]$',
+      '#description' => t('<strong>Options to select across the top</strong>, such as "Poor" through "Excellent". Indicate the default to the left of the desired item. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help'),
     );
 
     $form['questions'] = array(
       '#type' => 'fieldset',
       '#title' => t('Questions'),
       '#collapsible' => TRUE,
-      '#description' => t('Questions list down the side of the grid.'),
       '#attributes' => array('class' => array('webform-options-element')),
       '#element_validate' => array('_webform_edit_validate_options'),
+      '#weight' => -2,
     );
     $form['questions']['options'] = array(
       '#type' => 'options',
@@ -98,6 +121,7 @@ function _webform_edit_grid($component) {
       '#key_type' => 'mixed',
       '#key_type_toggle' => t('Customize question keys (Advanced)'),
       '#key_type_toggled' => $component['extra']['custom_question_keys'],
+      '#description' => t('<strong>Questions list down the side of the grid.</strong> For a heading column on the right, append "|" and the right-side heading. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help'),
     );
   }
   else {
@@ -105,11 +129,11 @@ function _webform_edit_grid($component) {
       '#type' => 'textarea',
       '#title' => t('Options'),
       '#default_value' => $component['extra']['options'],
-      '#description' => t('Options to select across the top. One option per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help'),
+      '#description' => t('Options to select across the top, such as "Poor" through "Excellent" or "Stronly Disagree" through "Strongly Agree".') .
+                          '<p>' . t('One key-value option per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys.') . '</p>' . theme('webform_token_help'),
       '#cols' => 60,
       '#rows' => 5,
       '#weight' => -3,
-      '#required' => TRUE,
       '#wysiwyg' => FALSE,
       '#element_validate' => array('_webform_edit_validate_select'),
     );
@@ -117,11 +141,11 @@ function _webform_edit_grid($component) {
       '#type' => 'textarea',
       '#title' => t('Questions'),
       '#default_value' => $component['extra']['questions'],
-      '#description' => t('Questions list down the side of the grid. One question per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable question"</strong>. For a heading column on the right, append "|" and the right-side heading. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help'),
+      '#description' => t('Questions list down the side of the grid. One question per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable question"</strong>. For a heading column on the right, append "|" and the right-side heading. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help') . ' ' .
+                        '<p>' . t('<strong>Or for more control</strong> over the appearance and configuration, create additional additional Select Options or other type components nested under this grid. They will operate as separate components, but be displayed within this grid.') . '</p>',
       '#cols' => 60,
       '#rows' => 5,
       '#weight' => -2,
-      '#required' => TRUE,
       '#wysiwyg' => FALSE,
       '#element_validate' => array('_webform_edit_validate_select'),
     );
@@ -218,51 +242,138 @@ function _webform_render_grid($component, $value = NULL, $filter = TRUE, $submis
 function webform_expand_grid($element) {
   $options = $element['#grid_options'];
   $questions = $element['#grid_questions'];
+  $weights = array();
 
-  if (!empty($element['#optrand'])) {
-    _webform_shuffle_options($options);
-  }
-
-  if (!empty($element['#qrand'])) {
-    _webform_shuffle_options($questions);
+  // Process questions and options from nested components.
+  foreach (element_children($element) as $key) {
+    $question = $element[$key];
+    // Both forms and grid displays have #webform_component.
+    if (isset($question['#webform_component']) &&
+        $question['#webform_component']['type'] == 'select' &&
+        !$question['#webform_component']['extra']['aslist'] &&
+        !$question['#webform_component']['extra']['other_option']) {
+      $options = webform_grid_merge_options($options, $question['#options']);
+      $weights[$key] = $question['#weight'];
+    }
   }
 
+  // Add the internal grid questions.
+  $weight = -1000;
+  $value = isset($element['#default_value']) ? $element['#default_value'] : array();
   foreach ($questions as $key => $question) {
     if ($question != '') {
+      $question_value = isset($value[$key]) && $value[$key] !== '' ? $value[$key] : NULL;
       $element[$key] = array(
+        '#grid_question' => TRUE,
         '#title' => $question,
         '#required' => $element['#required'],
-        '#options' => $options,
+        '#options' => $element['#grid_options'],
         '#type' => 'radios',
+        '#default_value' => $question_value,
+        '#value' => $question_value,
         '#process' => array('form_process_radios', 'webform_expand_select_ids'),
 
         // Webform handles validation manually.
         '#validated' => TRUE,
         '#webform_validated' => FALSE,
         '#translatable' => array('title'),
+        '#weight' => $weight,
       );
 
       // Add HTML5 required attribute, if needed.
       if ($element['#required']) {
         $element[$key]['#attributes']['required'] = 'required';
       }
+
+      $weights[$key] = $weight;
+      $weight++;
     }
   }
 
-  $value = isset($element['#default_value']) ? $element['#default_value'] : array();
-  foreach (element_children($element) as $key) {
-    if (isset($value[$key])) {
-      $element[$key]['#default_value'] = ($value[$key] !== '') ? $value[$key] : NULL;
-    }
-    else {
-      $element[$key]['#default_value'] = NULL;
-    }
+  if (!empty($element['#optrand'])) {
+    _webform_shuffle_options($options);
+  }
+  $element['#grid_options'] = $options;
+
+  asort($weights);
+  if (!empty($element['#qrand'])) {
+    _webform_shuffle_options($weights);
+  }
+  $weight = min($weights);
+  foreach ($weights as $key => $old_weight) {
+    $element[$key]['#options'] = webform_grid_remove_options($options, $element[$key]['#options']);
+    $element[$key]['#weight'] = $weight++;
+    $element['#grid_questions'][$key] = $element[$key]['#title'];
   }
 
   return $element;
 }
 
 /**
+ * Helper. Merge select component options in order.
+ *
+ * @param array $existing
+ *   An array of existing values into which any values from $new that aren't in
+ *   $existing are inserted.
+ * @param array $new
+ *   Values to be inserted into $existing.
+ * @return array
+ *   The merged array.
+ */
+function webform_grid_merge_options($existing, $new) {
+  $insert = NULL;
+  $queue = array();
+  foreach ($new as $key => $value) {
+    if (isset($existing[$key])) {
+      // Insert the queue before the found item.
+      $insert = array_search($key, array_keys($existing));
+      if ($queue) {
+        $existing = array_slice($existing, 0, $insert, TRUE) +
+                    $queue +
+                    array_slice($existing, $insert, NULL, TRUE);
+        $insert += count($queue);
+        $queue = array();
+      }
+      $insert++;
+    }
+    elseif (is_null($insert)) {
+      // It is not yet clear yet where to put this item. Add it to the queue.
+      $queue[$key] = $value;
+    }
+    else {
+      // PHP array_splice does not preserved the keys of the inserted array,
+      // but array_slice does (if the preserve keys parameter is TRUE).
+      $existing = array_slice($existing, 0, $insert, TRUE) +
+                  array($key => $value) +
+                  array_slice($existing, $insert, NULL, TRUE);
+      $insert++;
+    }
+  }
+  // Append any left over queued items.
+  $existing += $queue;
+  return $existing;
+}
+
+/**
+ * Helper. Replace missing options with empty values.
+ *
+ * @param array $header
+ *   An array of options to be used at the grid table header.
+ * @param array $row_options
+ *   An array of options to be used for this row.
+ * @return array
+ *   The $row_options with any missing options replaced with empty values.
+ **/
+function webform_grid_remove_options($header, $row_options) {
+  foreach ($header as $key => $value) {
+    if (!isset($row_options[$key])) {
+      $header[$key] = '';
+    }
+  }
+  return $header;
+}
+
+/**
  * Implements _webform_display_component().
  */
 function _webform_display_grid($component, $value, $format = 'html', $submission = array()) {
@@ -279,6 +390,7 @@ function _webform_display_grid($component, $value, $format = 'html', $submission
     '#format' => $format,
     '#grid_questions' => $questions,
     '#grid_options' => $options,
+    '#default_value' => $value,
     '#sticky' => $component['extra']['sticky'],
     '#theme' => 'webform_display_grid',
     '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
@@ -300,6 +412,19 @@ function _webform_display_grid($component, $value, $format = 'html', $submission
 }
 
 /**
+ * Preprocess function for displaying a grid component.
+ */
+function template_preprocess_webform_display_grid(&$variables) {
+  $element =& $variables['element'];
+  // Expand the grid, suppressing randomization. This builds the grid
+  // questions and options.
+  $element['#qrand'] = FALSE;
+  $element['#optrand'] = FALSE;
+  $element['#required'] = FALSE;
+  $element = webform_expand_grid($element);
+}
+
+/**
  * Format the text output for this component.
  */
 function theme_webform_display_grid($variables) {
@@ -315,18 +440,30 @@ function theme_webform_display_grid($variables) {
     // Set the header for the table.
     $header = _webform_grid_header($element, $right_titles);
 
-    foreach ($element['#grid_questions'] as $question_key => $question) {
+    foreach (element_children($element) as $question_key) {
+      $question_element = $element[$question_key];
       $row = array();
-      $questions = explode('|', $question, 2);
+      $questions = explode('|', $question_element['#title'], 2);
+      $values = $question_element['#value'];
+      $values = is_array($values) ? $values : array($values);
       $row[] = array('data' => webform_filter_xss($questions[0]), 'class' => array('webform-grid-question'));
-      foreach ($element['#grid_options'] as $option_value => $option_label) {
-        if (strcmp($element[$question_key]['#value'], $option_value) == 0) {
-          $row[] = array('data' => '<strong>X</strong>', 'class' => array('checkbox', 'webform-grid-option'));
-        }
-        else {
-          $row[] = array('data' => '&nbsp;', 'class' => array('checkbox', 'webform-grid-option'));
+      if (isset($element['#grid_questions'][$question_key])) {
+        foreach ($element['#grid_options'] as $option_value => $option_label) {
+          if (in_array($option_value, $values)) {
+            $row[] = array('data' => '<strong>X</strong>', 'class' => array('checkbox', 'webform-grid-option'));
+          }
+          else {
+            $row[] = array('data' => '&nbsp;', 'class' => array('checkbox', 'webform-grid-option'));
+          }
         }
       }
+      else {
+        $question_element['#title_display'] = 'none';
+        $row[] = array(
+          'data' => drupal_render($question_element),
+          'colspan' => count($element['#grid_options']),
+        );
+      }
       if ($right_titles) {
         $row[] = array('data' => isset($questions[1]) ? webform_filter_xss($questions[1]) : '', 'class' => array('webform-grid-question'));
       }
@@ -338,10 +475,26 @@ function theme_webform_display_grid($variables) {
   }
   else {
     $items = array();
-    foreach (element_children($element) as $key) {
-      $items[] = ' - ' . _webform_grid_question_header($element[$key]['#title']) . ': ' .
-                 (isset($element['#grid_options'][$element[$key]['#value']]) ? $element['#grid_options'][$element[$key]['#value']] : '');
-    }
+    foreach (element_children($element) as $question_key) {
+      $question_element = $element[$question_key];
+      if (isset($element['#grid_questions'][$question_key])) {
+        $values = $question_element['#value'];
+        $values = is_array($values) ? $values : array($values);
+        foreach ($values as $value_key => $value) {
+          if (isset($element['#grid_options'][$value])) {
+            $values[$value_key] = $element['#grid_options'][$value];
+          }
+          else {
+            unset($values[$value_key]);
+          }
+        }
+        $value = implode(', ', $values);
+      } else {
+        $element[$question_key]['#title'] = '';
+        $value = drupal_render($element[$question_key]);
+      }
+      $items[] = ' - ' . _webform_grid_question_header($question_element['#title']) . ': ' . $value;
+   }
     $output = implode("\n", $items);
   }
 
@@ -401,10 +554,13 @@ function _webform_analysis_grid($component, $sids = array(), $single = FALSE, $j
     $rows[] = $row;
   }
 
-  return array(
-    'table_header' => $header,
-    'table_rows' => $rows,
-  );
+  // Return return the table unless there are no internal questions in the grid.
+  if ($rows) {
+    return array(
+      'table_header' => $header,
+      'table_rows' => $rows,
+    );
+  }
 }
 
 /**
@@ -425,7 +581,10 @@ function _webform_table_grid($component, $value) {
     }
   }
 
-  return $output;
+  // Return output if the grid contains internal questions.
+  if (count($questions)) {
+    return $output;
+  }
 }
 
 /**
@@ -480,8 +639,13 @@ function _webform_csv_data_grid($component, $export_options, $value) {
  * A Form API element validate function to check that all choices are unique.
  */
 function _webform_edit_grid_unique_validate($element) {
-  $nr_unique = count(array_unique($element['#value']));
-  $nr_values = count($element['#value']);
+  // Grids may contain nested multiple value select components.
+  // Create a flat array of values.
+  $values = array();
+  array_walk_recursive($element['#value'], function($a) use (&$values) { $values[] = $a; });
+
+  $nr_unique = count(array_unique($values));
+  $nr_values = count($values);
   $nr_possible = count($element['#grid_options']);
   if (strlen($element['#grid_default']) && isset($element['#grid_options'][$element['#grid_default']])) {
     // A default is defined and is one of the options. Don't count default values
@@ -511,23 +675,52 @@ function theme_webform_grid($variables) {
 
   foreach (element_children($element) as $key) {
     $question_element = $element[$key];
-    $question_titles = explode('|', $question_element['#title'], 2);
+    $title_element =& $question_element;
+    if ($question_element['#type'] == 'select_or_other') {
+      $title_element =& $question_element['select'];
+    }
+    $question_titles = explode('|', $title_element['#title'], 2);
 
     // Create a row with the question title.
-    $row = array(array('data' => webform_filter_xss($question_titles[0]), 'class' => array('webform-grid-question')));
+    $required = !empty($question_element['#required']) ? theme('form_required_marker', array('element' => $question_element)) : '';
+    $row = array(array('data' => t('!title !required', array('!title' => webform_filter_xss($question_titles[0]), '!required' => $required)), 'class' => array('webform-grid-question')));
 
     // Render each radio button in the row.
-    $radios = form_process_radios($question_element);
-    foreach (element_children($radios) as $key) {
-      $radio_title = $radios[$key]['#title'];
-      $radios[$key]['#title'] = $question_element['#title'] . ' - ' . $radio_title;
-      $radios[$key]['#title_display'] = 'invisible';
-      $row[] = array('data' => drupal_render($radios[$key]), 'class' => array('checkbox', 'webform-grid-option'), 'data-label' => array($radio_title));
+    if ($question_element['#type'] == 'radios' || $question_element['#type'] == 'checkboxes') {
+      $radios = form_process_radios($question_element);
+      foreach (element_children($radios) as $key) {
+        $radio_title = $radios[$key]['#title'];
+        if (!strlen($radio_title)) {
+          $row[] = '&nbsp;';
+        }
+        else {
+          $radios[$key]['#title'] = $question_element['#title'] . ' - ' . $radio_title;
+          $radios[$key]['#title_display'] = 'invisible';
+          $row[] = array('data' => drupal_render($radios[$key]), 'class' => array('checkbox', 'webform-grid-option'), 'data-label' => array($radio_title));
+        }
+      }
+    }
+    else {
+      $title_element['#title_display'] = 'none';
+      $row[] = array(
+        'data' => drupal_render($question_element),
+        'colspan' => count($element['#grid_options']),
+      );
     }
     if ($right_titles) {
       $row[] = array('data' => isset($question_titles[1]) ? webform_filter_xss($question_titles[1]) : '', 'class' => array('webform-grid-question'));
     }
-    $rows[] = $row;
+
+    // Convert the parents array into a string, excluding the "submitted" wrapper.
+    $nested_level = $question_element['#parents'][0] == 'submitted' ? 1 : 0;
+    $parents = str_replace('_', '-', implode('--', array_slice($question_element['#parents'], $nested_level)));
+
+    $rows[] = array(
+      'data' => $row,
+      'class' => empty($question_element['#grid_question'])
+                    ? array('webform-component', 'webform-component-' . str_replace('_', '-', $question_element['#type']), 'webform-component--' . $parents)
+                    : array(),
+    );
   }
 
   $option_count = count($header) - 1;
@@ -544,7 +737,7 @@ function _webform_grid_header($element, $right_titles) {
     $header[] = array('data' => webform_filter_xss($option), 'class' => array('checkbox', 'webform-grid-option'));
   }
   if ($right_titles) {
-    $header[] = array('data' => _webform_grid_header_title($element, $titles[1]), 'class' => array('webform-grid-question'));
+    $header[] = array('data' => _webform_grid_header_title($element, isset($titles[1]) ? $titles[1] : ''), 'class' => array('webform-grid-question'));
   }
   return $header;
 }
@@ -584,4 +777,3 @@ function _webform_grid_right_titles($element) {
 function _webform_grid_question_header($text) {
   return implode('/', array_filter(explode('|', $text)));
 }
-
diff --git a/components/select.inc b/components/select.inc
index 942c1d4..defd8f5 100644
--- a/components/select.inc
+++ b/components/select.inc
@@ -60,6 +60,15 @@ function _webform_edit_select($component) {
     ),
   );
 
+  // Default component if nested under a grid.
+  if (!isset($component['cid']) && $component['pid'] &&
+      ($node = node_load($component['nid'])) && ($parent = $node->webform['components'][$component['pid']]) &&
+      $parent['type'] == 'grid') {
+    $component['value'] = $parent['value'];
+    $component['extra']['items'] = $parent['extra']['options'];
+    $component['required'] = $parent['required'];
+  }
+
   $other = array();
   if ($info = _webform_select_options_info()) {
     $options = array('' => t('None'));
diff --git a/includes/webform.components.inc b/includes/webform.components.inc
index fbe4fb6..6330343 100644
--- a/includes/webform.components.inc
+++ b/includes/webform.components.inc
@@ -448,17 +448,16 @@ function webform_component_edit_form($form, $form_state, $node, $component, $clo
     '#weight' => 8,
   );
   if (webform_component_feature($component['type'], 'title_display')) {
-    $inline_option = webform_component_feature($component['type'], 'title_inline') ? array('inline' => t('Inline')) : array();
-    $internal_option = webform_component_feature($component['type'], 'title_internal') ? array('internal' => t('Inside the component')) : array();
-    if ($inline_option || $internal_option) {
+    if (webform_component_feature($component['type'], 'title_inline')) {
       $form['display']['title_display'] = array(
         '#type' => 'select',
         '#title' => t('Label display'),
         '#default_value' => !empty($component['extra']['title_display']) ? $component['extra']['title_display'] : 'before',
-        '#options' => array('before' => t('Above')) +
-                      $inline_option +
-                      $internal_option +
-                      array('none' => t('None')),
+        '#options' => array(
+          'before' => t('Above'),
+          'inline' => t('Inline'),
+          'none' => t('None'),
+        ),
         '#description' => t('Determines the placement of the component\'s label.'),
       );
     }
@@ -967,7 +966,6 @@ function webform_component_feature($type, $feature) {
     'title' => TRUE,
     'title_display' => TRUE,
     'title_inline' => TRUE,
-    'title_internal' => FALSE,
     'conditional' => TRUE,
     'conditional_action_set' => FALSE,
     'spam_analysis' => FALSE,
diff --git a/js/webform-admin.js b/js/webform-admin.js
index 9b9f2af..cd70e5c 100644
--- a/js/webform-admin.js
+++ b/js/webform-admin.js
@@ -311,21 +311,6 @@
   };
 
   /**
-   * Given a table's DOM element, restripe the odd/even classes.
-   */
-  Drupal.webform.restripeTable = function (table) {
-    // :even and :odd are reversed because jQuery counts from 0 and
-    // we count from 1, so we're out of sync.
-    // Match immediate children of the parent element to allow nesting.
-    $('> tbody > tr, > tr', table)
-      .filter(':odd').filter('.odd')
-        .removeClass('odd').addClass('even')
-      .end().end()
-      .filter(':even').filter('.even')
-        .removeClass('even').addClass('odd');
-  };
-
-  /**
    * Triggers a change event when a label receives a click.
    *
    * When the browser automatically selects a radio button when it's label is
diff --git a/js/webform.js b/js/webform.js
index 240c1c5..d2c6675 100644
--- a/js/webform.js
+++ b/js/webform.js
@@ -175,6 +175,9 @@
                 // Record that the target was hidden.
                 targetLocked[action['target']] = 'hide';
               }
+              if ($target.is('tr')) {
+                Drupal.webform.restripeTable($target.closest('table').first());
+              }
             }
             break;
           case 'require':
@@ -629,4 +632,19 @@
     return this;
   };
 
+  /**
+   * Given a table's DOM element, restripe the odd/even classes.
+   */
+  Drupal.webform.restripeTable = function (table) {
+    // :even and :odd are reversed because jQuery counts from 0 and
+    // we count from 1, so we're out of sync.
+    // Match immediate children of the parent element to allow nesting.
+    $('> tbody > tr, > tr', table)
+      .filter(':visible:odd').filter('.odd')
+        .removeClass('odd').addClass('even')
+      .end().end()
+      .filter(':visible:even').filter('.even')
+        .removeClass('even').addClass('odd');
+  };
+
 })(jQuery);
diff --git a/webform.api.php b/webform.api.php
index 0d00b55..131f4ed 100644
--- a/webform.api.php
+++ b/webform.api.php
@@ -520,10 +520,6 @@ function hook_webform_component_info() {
       // This component has a title that can be displayed inline.
       'title_inline' => TRUE,
 
-      // This component has a title that can be displayed within the form
-      // element itself, such as a grid.
-      'title_internal' => TRUE,
-
       // If this component can be used as a conditional SOURCE. All components
       // may always be displayed conditionally, regardless of this setting.
       // Defaults to TRUE.
diff --git a/webform.module b/webform.module
index abbcc01..56f4b17 100644
--- a/webform.module
+++ b/webform.module
@@ -1050,6 +1050,7 @@ function webform_webform_component_info() {
         'title_internal' => TRUE,
         'css_classes' => FALSE,
         'conditional' => FALSE,
+        'group' => TRUE,
       ),
       'file' => 'components/grid.inc',
     ),
@@ -3433,6 +3434,10 @@ function _webform_client_form_submit_flatten($node, $fieldset, $parent = 0) {
           $values[$cid] = $value;
         }
       }
+      else {
+        // This $form_key must belong to the parent. For example, a grid.
+        $values[$parent][$form_key] = $value;
+      }
     }
   }
 
-- 
2.6.1.windows.1

