Looking to hire Laravel developers? Try LaraJobs

laravel-table-sorter maintained by am2studio

Description
Laravel table sorter
Author
Last update
2022/11/22 16:30 (dev-patch-1)
License
Downloads
4 777

Comments
comments powered by Disqus

Laravel-TableSorter

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.