Skip to content

Instantly share code, notes, and snippets.

@jdavidbakr
Last active February 20, 2016 10:15
Show Gist options
  • Save jdavidbakr/bf2bbcb01cd94e12e43b to your computer and use it in GitHub Desktop.
Save jdavidbakr/bf2bbcb01cd94e12e43b to your computer and use it in GitHub Desktop.
Laravel: Ajax pagination table header generator
<?php
namespace App\Services;
class TableHeaderGenerator {
/**
* Returns an array for the table view's header data that includes sort links
* @param Array $data array of arrays with keys 'label' and 'col'
* @param string $current_sort
* @param string $current_sort_dir
* @return Array Processed array ready for the table view
*/
static public function render($data, $current_sort, $current_sort_dir, $sort_link)
{
$final = [];
$final['attributes'] = ['class'=>'header'];
$cells = [];
foreach($data as $column_data) {
$label = $column_data['label'];
$col = $column_data['col'];
$cell = [];
$cell['dom'] = 'th';
$cell['content'] = '<a onclick="return paginate(this)" href="'.$sort_link.'&sort='.$col.'&sort_dir='.(($current_sort==$col&&$current_sort_dir=='desc')?'asc':'desc').'">'.$label.($current_sort==$col?' <img src="'.asset('images/table/'.$current_sort_dir.'.png').'">':'').'</a>';
$cells[] = $cell;
}
$final['cells'] = $cells;
return $final;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment