function theme_table_header_sortable

7.x theme.inc theme_table_header_sortable($headers = array(), $element_id = "default")
6.x theme.inc theme_table_header_sortable($headers = array(), $element_id = "default")

Given an array of table headers (in the format listed below), returns back the HTML to draw it to the screen. This makes them clickable, to make the table header sortable. This is meant to be used with queries, by adding in an "ORDER BY" clause.

$headers should look like this: 0 = array('label' => 'First Name', 'field' => 'field__first_name');

If the label should NOT be clickable, then "field" should be blank or absent.

7 calls to theme_table_header_sortable()
admin_display_watchdog in modules/admin/admin.module
alerts_advisees_alerts_form in modules/alerts/alerts.module
Displays alerts for our various advisees.
alerts_display_advisee_activities_page in modules/alerts/alerts.module
Display all advisee activities since the beginning of time, thanks to pager query.
content_display_content_admin_list in modules/content/content.module
Display a list of content for the administrator
content_public_files_form in modules/content/content.module
This screen lets the user upload/manage/delete "public files" stored at custom/files/content_uploads/public_uploads/

... See full list

File

includes/theme.inc, line 25

Code

function theme_table_header_sortable($headers = array(), $element_id = "default") {

  // Is there a "current student id" being specified in the URL? If so, let's keep it in our links.
  $csid = trim(@$_REQUEST ['current_student_id']);
  if ($csid) {
    $csid = "&current_student_id=$csid";
  }

  $rtn = "";
  $rtn .= "<tr>";

  $filter = trim(@$_GET ['filter']); // if there is an existing filter we need to preserve.
  $filter_line = "";
  if ($filter) {
    $filter_line = "&filter=" . $filter;
  }

  foreach ($headers as $header) {
    $th_class = fp_get_machine_readable(strtolower($header ['label']));
    $rtn .= "<th class='header-sortable-$th_class'>";

    $label = $header ['label'];
    $field = @$header ['field'];
    $init_dir = @$hader ['init_dir'];

    if (!$field) {
      $rtn .= $label;
    }
    else {
      // Convert label to a link.  Also, show it as ASC or DESC based on if it is currently selected.

      // get the current header sort field and dir....
      $current_fsort = @$_GET ['fsort'];
      $current_fsortdir = @$_GET ['fsortdir'];

      $opposite_fsortdir = 'ASC';
      if ($current_fsortdir == 'ASC') {
        $opposite_fsortdir = 'DESC';
      }

      if ($field == $current_fsort) {
        if ($current_fsortdir == 'ASC') {
          $label .= " <i class='fa fa-chevron-up'></i>";
        }
        else {
          $label .= " <i class='fa fa-chevron-down'></i>";
        }
      }


      $new_fsortdir = 'ASC';

      //if ($default_sort) $new_fsortdir = $default_sort;



      // Scenario #1:
      // We have never clicked this item before.  The link should be for THIS field, and for its "new_fsortdir"
      if ($current_fsort != $field) {
        $link = l($label, $_GET ['q'], "fsort=$field&fsortdir=$new_fsortdir" . $filter_line . $csid);
      }

      // Scenario #2:
      // We have just recently clcked this item.  We want to REVERSE the sort direction
      if ($current_fsort == $field) {
        $link = l($label, $_GET ['q'], "fsort=$field&fsortdir=$opposite_fsortdir" . $filter_line . $csid);
      }


      $rtn .= $link;


    } // else



    $rtn .= "</th>";
  } // foreach header





  $rtn .= "</tr>";
  return $rtn;

}