Thứ Năm, 4 tháng 10, 2012

[DRUPAL 6 - VIEW] Ẩn tiêu đề và nội dung của field khi không có nội dung

Nice work, here's a slightly modified (cleaner loops?) version, with php short tags replaced by long tags.
<?php/**
* Product Table View customized so that any columns with no data are omitted from display.
*/
foreach ($rows as $count => $row):
    foreach (
$row as $field => $content):
      if (
$content) $column_has_content[ $field ] = true;
    endforeach;
endforeach;
?>



<table class="<?php print $class; ?>">
  <?php if (!empty($title)) : ?>
    <caption><?php print $title; ?></caption>
  <?php endif; ?>
  <thead>
    <tr>    
      <?php foreach ( $header as $field => $label ): ?>
        <?php if ( $column_has_content[ $field ] ): ?>
          <th class="views-field views-field-<?php print $fields[$field]; ?>">
            <?php print $label; ?>
          </th>
        <?php endif;?>
      <?php endforeach; ?>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($rows as $count => $row): ?>
      <tr class="<?php print implode(' ', $row_classes[$count]); ?>">
        <?php foreach ($row as $field => $content): ?>
          <?php if ( $column_has_content[ $field ] ): ?>
            <td class="views-field views-field-<?php print $fields[$field]; ?>">
              <?php print $content; ?>
            </td>
          <?php endif; ?>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

Use preprocessing function

Thank you all for examples. I modified them so column checking code is in preprocessing function.
Put following in template.php
<?php/**
* Removes column headers for columns that do not have content.
*/
function THEME-NAME_preprocess_views_view_table__VIEW-NAME(&$vars) {
 
$rows = $vars['rows'];
 
$column_has_content = array();
  foreach (
$rows as $count => $row) {
    foreach (
$row as $field => $content) {
      if (!empty(
$content)) {
       
$column_has_content[$field] = TRUE;
      }
    }
  }
 
$vars['column_has_content'] = $column_has_content;
}
?>
You can now use $column_has_content variable in views-view-table--VIEW-NAME.tpl.php
<table class="<?php print $class; ?>">
  <?php if (!empty($title)) : ?>
    <caption><?php print $title; ?></caption>
  <?php endif; ?>
  <thead>
    <tr>
      <?php foreach ($header as $field => $label): ?>
      <?php if (  $column_has_content[$field]  ):  ?>
        <th class="views-field views-field-<?php print $fields[$field]; ?>">
          <?php print $label; ?>
        </th>
      <?php endif; ?>
      <?php endforeach; ?>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($rows as $count => $row): ?>
      <tr class="<?php print implode(' ', $row_classes[$count]); ?>">
        <?php foreach ($row as $field => $content): ?>
        <?php if (  $column_has_content[$field]  ):  ?>
          <td class="views-field views-field-<?php print $fields[$field]; ?>">
            <?php print $content; ?>
          </td>
        <?php endif; ?>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
Don't forget to replace VIEW-NAME with name of your view and THEME-NAME with name of your theme.

Không có nhận xét nào:

Đăng nhận xét