am2studio/laravel-table-sorter

Laravel table sorter

3.0 2017-02-09 14:11 UTC

README

Package contains helper function for creating sortable colums in table

Install

Via Composer

$ composer require am2studio/laravel-table-sorter

Usage

Code in view :

{{ \AM2Studio\Laravel\TableSorter\TableSorter::sort(
    [
      ['name' => 'first_name', 'title' => trans('ui.user.first_name')],
      ['name' => 'last_name',  'title' => trans('ui.user.last_name')],
      ['name' => 'gender',     'title' => trans('ui.user.gender')],
    ],
    $users,
    [
        'sort_by' => 'name', 'sort_type' => 'ASC',
        'template' => '<th class="%s"><a href="%s">%s</a></th>'
    ])
}}

variable $headings contains name and title for table columns, example for users

[
  ['name' => 'first_name', 'title' => trans('ui.user.first_name')],
  ['name' => 'last_name',  'title' => trans('ui.user.last_name')],
  ['name' => 'gender',     'title' => trans('ui.user.gender')],
]

variable $config contains default sort_by/sort_type and template

[
    'sort_by' => 'name', 'sort_type' => 'ASC',
    'template' => '<th class="%s"><a href="%s">%s</a></th>'
]

Controller code:

public function index()
{
    $users = (new User)->paginate(10);
    return $this->view('index', compact('users'));
}

Full view table :

<table>
	<thead>
		<tr>
			{{ \AM2Studio\Laravel\TableSorter\TableSorter::sort(
					[
						['name' => 'first_name', 'title' => trans('ui.user.first_name')],
						['name' => 'last_name',  'title' => trans('ui.user.last_name')],
						['name' => 'gender',     'title' => trans('ui.user.gender')],
					],
					$users,
					[
						'sort_by' => 'name', 'sort_type' => 'ASC',
						'template' => '<th class="%s"><a href="%s">%s</a></th>'
					])
			}}
		</tr>
	</thead>
	<tbody>
		@foreach($users as $user)
		<tr>

			<td>{{ $user->first_name }}</td>
			<td>{{ $user->last_name }}</td>
			<td>{{ $user->gender }}</td>
		</tr>
		@endforeach
	</tbody>
</table>
<div>{!! $users !!}</div>

Change log

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

License

The MIT License (MIT). Please see License File for more information.