AM2studio / Laravel-TableSorter by msamec

Laravel table sorter
4,775
2
7
Package Data
Maintainer Username: msamec
Maintainer Contact: filip.horvat@am2studio.hr (Filip Horvat)
Package Create Date: 2016-02-15
Package Last Update: 2022-11-22
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:19:27
Package Statistics
Total Downloads: 4,775
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 2
Total Watchers: 7
Total Forks: 2
Total Open Issues: 0

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.