JHtmlGrid is utility class for creating HTML Grids.

1. sort()

This method is used to sort a column in a grid.

  • string $title - The link title
  • string $order - The order field for the column
  • string $direction - The current direction
  • string $selected - The selected ordering
  • string $task - An optional task override
  • string $new_direction - An optional direction for the new column
  • string $tip - An optional text shown as tooltip title instead of $title
  • string $form - An optional form selector
public static function sort($title, $order, $direction = 'asc', $selected = '', $task = null, $new_direction = 'asc', $tip = '', $form = null)
{
JHtml::_('behavior.core');
JHtml::_('bootstrap.popover');

$direction = strtolower($direction);
$icon = array('arrow-up-3', 'arrow-down-3');
$index = (int) ($direction === 'desc');

if ($order != $selected)
{
$direction = $new_direction;
}
else
{
$direction = $direction === 'desc' ? 'asc' : 'desc';
}

if ($form)
{
$form = ', document.getElementById(\'' . $form . '\')';
}

$html = '<a href="#" onclick="Joomla.tableOrdering(\'' . $order . '\',\'' . $direction . '\',\'' . $task . '\'' . $form . ');return false;"'
. ' class="hasPopover" title="' . htmlspecialchars(JText::_($tip ?: $title)) . '"'
. ' data-content="' . htmlspecialchars(JText::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN')) . '" data-placement="top">';

if (isset($title['0']) && $title['0'] === '<')
{
$html .= $title;
}
else
{
$html .= JText::_($title);
}

if ($order == $selected)
{
$html .= '<span class="icon-' . $icon[$index] . '"></span>';
}

$html .= '</a>';

return $html;
}

2. checkall()

This method is used to check all the checkboxes in a grid.

  • string $name - The name of the form element
  • string $tip - The text shown as tooltip title instead of $tip
  • string $action - The action to perform on clicking the checkbox
  • return - string
public static function checkall($name = 'checkall-toggle', $tip = 'JGLOBAL_CHECK_ALL', $action = 'Joomla.checkAll(this)')
{
JHtml::_('behavior.core');
JHtml::_('bootstrap.tooltip');

return '<input type="checkbox" name="' . $name . '" value="" class="hasTooltip" title="' . JHtml::_('tooltipText', $tip)
. '" onclick="' . $action . '" />';
}

3. id()

This method is used to create a checkbox for a grid row.

  • integer $rowNum - The row index
  • integer $recId - The record id
  • boolean $checkedOut - True if item is checked out
  • string $name - The name of the form element
  • string $stub - The name of stub identifier
  • return - mixed String of html with a checkbox if item is not checked out, null if checked out
public static function id($rowNum, $recId, $checkedOut = false, $name = 'cid', $stub = 'cb')
{
return $checkedOut ? '' : '<input type="checkbox" id="' . $stub . $rowNum . '" name="' . $name . '[]" value="' . $recId
. '" onclick="Joomla.isChecked(this.checked);" />';
}

4. checkedOut()

It displays a checked out icon.

  • object &$row - A data object (must contain checkedout as a property)
  • integer $i - The index of the row
  • string $identifier - The property name of the primary key or index of the row
  • return - string
public static function checkedOut(&$row, $i, $identifier = 'id')
{
$user = JFactory::getUser();
$userid = $user->get('id');

if ($row instanceof JTable)
{
$result = $row->isCheckedOut($userid);
}
else
{
$result = false;
}

if ($result)
{
return static::_checkedOut($row);
}
else
{
if ($identifier === 'id')
{
return JHtml::_('grid.id', $i, $row->$identifier);
}
else
{
return JHtml::_('grid.id', $i, $row->$identifier, $result, $identifier);
}
}
}

5. published()

This method is used to create a clickable icon to change the state of an item.

  • integer $i - The index
  • string $img1 - Image for a positive or on value
  • string $img0 - Image for the empty or off value
  • string $prefix - An optional prefix for the task
  • return - string
public static function published($value, $i, $img1 = 'tick.png', $img0 = 'publish_x.png', $prefix = '')
{
if (is_object($value))
{
$value = $value->published;
}

$img = $value ? $img1 : $img0;
$task = $value ? 'unpublish' : 'publish';
$alt = $value ? JText::_('JPUBLISHED') : JText::_('JUNPUBLISHED');
$action = $value ? JText::_('JLIB_HTML_UNPUBLISH_ITEM') : JText::_('JLIB_HTML_PUBLISH_ITEM');

return '<a href="#" onclick="return listItemTask(\'cb' . $i . '\',\'' . $prefix . $task . '\')" title="' . $action . '">'
. JHtml::_('image', 'admin/' . $img, $alt, null, true) . '</a>';
}

6. state()

This method is used to create a select list of states for filtering. By default the filter shows only published and unpublished items.

  • string $filter_state - The initial filter state
  • string $published - The JText string for published
  • string $unpublished - The JText string for Unpublished
  • string $archived - The JText string for Archived
  • string $trashed - The JText string for Trashed
  • return string
public static function state($filter_state = '*', $published = 'JPUBLISHED', $unpublished = 'JUNPUBLISHED', $archived = null, $trashed = null)
{
$state = array('' => '- ' . JText::_('JLIB_HTML_SELECT_STATE') . ' -', 'P' => JText::_($published), 'U' => JText::_($unpublished));

if ($archived)
{
$state['A'] = JText::_($archived);
}

if ($trashed)
{
$state['T'] = JText::_($trashed);
}

return JHtml::_(
'select.genericlist',
$state,
'filter_state',
array(
'list.attr' => 'class="inputbox" size="1" onchange="Joomla.submitform();"',
'list.select' => $filter_state,
'option.key' => null,
)
);
}

7. order()

This method is used to create an icon for saving a new ordering in a grid.

  • array $rows - The array of rows of rows
  • string $image - The image (unused)
  • string $task - The task to use, defaults to save order
  • return - string
public static function order($rows, $image = 'filesave.png', $task = 'saveorder')
{
return '<a href="javascript:saveorder('
. (count($rows) - 1) . ', \'' . $task . '\')" rel="tooltip" class="saveorder btn btn-micro pull-right" title="'
. JText::_('JLIB_HTML_SAVE_ORDER') . '"><span class="icon-menu-2"></span></a>';
}