Code Monkey home page Code Monkey logo

laravel-table-view's Introduction

laravel-table-view

Laravel 5 Package for easily displaying table views for Eloquent Collections with search and sort functionality built in.

Installation

Update your composer.json file to include this package as a dependency

"witty/laravel-table-view": "dev-master"

Register the TableView service provider by adding it to the providers array in the config/app.php file.

'providers' => array(
    Witty\LaravelTableView\LaravelTableViewServiceProvider::class
)

If you want you can alias the TableView facade by adding it to the aliases array in the config/app.php file.

'aliases' => array(
        'TableView' => Witty\LaravelTableView\Facades\TableView::class,
)

Configuration

Copy the vendor file views and assets into your project by running

php artisan vendor:publish

This will add multiple styles and one script to public/vendor/table-view The plugin depends on jQuery and v1.9.1 will be included under public/vendor/table-view - Bootstrap CSS v3.3.2 - Font Awesome v4.3.0 - jQuery v1.9.1

Usage

Initialize the table view by passing in an instance of \Illuminate\Eloquent\Builder or simply the class name of the model for the tableview

	$users = User::select('id', 'name', 'email', 'created_at');

	$usersTableView = TableView::collection( $users )
	// or $usersTableView = TableView::collection( \App\User::class )

Adding Columns to the tableview

	$usersTableView = $usersTableView
		// you can pass in the title for the column, and the Eloquent\Model property name
		->column('Email', 'email')

		// Add a colon after the Eloquent\Model property name along with sort and/or search to enable these options
		->column('Name', 'name:sort,search')

		// Set the default sorting property with 
		->column('Name', 'name:sort*,search')	// Sorted Ascending by default or specify
		->column('Name', 'name:sort*:asc')
		->column('Name', 'name:sort*:desc')

		// Custom column values are created by passing an array with the Eloquent\Model property name as the key
		//  and a closure function
		->column('Joined At', ['created_at:sort*' => function ($user) 
		{
			return $user->created_at->diffForHumans();
		}])



		// OR
		->column(function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		})
		->column('Email', 'email:sort,search')
		->column(function ($user) 
		{
			return '<a class="btn btn-success" href="/users/' . $user->id . '">View</a>';
		});

Custom column values

	$usersTableView = $usersTableView
		// You can pass in an array for the column's row value with the Eloquent\Model property name as the key
		//  and a closure function
		->column('Joined At', ['created_at:sort*' => function ($user) 
		{
			return $user->created_at->diffForHumans();
		}])

		// OR if sorting and searching is unnecessary, simply pass in the Closure instead of the array
		->column('Image', function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		});
}]);

Columns without titles

	$usersTableView = $usersTableView
		// Just leave the column title out if you don't want to use it
		->column(function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		});

Additional Controls - you can add partial views containing custom controls like a filter button to add additional functionality to your table

	$usersTableView = $usersTableView
		// Just pass in the partial view file path of the custom control
		->headerControl('_types_filter_button');

		// access the TableView data collection with $usersTableView->data()

Finally, build the TableView and pass it to the view

	$usersTableView = $usersTableView->build();

	return view('test', [
		'usersTableView' => $usersTableView
	]);

All together with chaining

Route::get('/', function(\Illuminate\Http\Request $request) 
{
	$users = User::select('id', 'name', 'email', 'created_at');

	$usersTableView = TableView::collection( $users, 'Administrator' )
		->column(function ($user) 
		{
			return '<img src="' . $user->image_path . '" height="60" width="60">';
		})
		->column('Name', 'name:sort,search')
		->column('Email', 'email:sort,search')
		->column('Joined At', ['created_at:sort*' => function ($user) 
		{
			return $user->created_at->diffForHumans();
		}])
		->column(function ($user) 
		{
			return '<a class="btn btn-success" href="/users/' . $user->id . '">View</a>';
		})
		->headerControl('_types_filter_button')
		->build();

	return view('test', [
		'usersTableView' => $usersTableView
	]);
});

Front End

Include stylesheets for Bootstrap and Font Awesome - Bootstrap CSS v3.3.2 and Font Awesome v4.3.0 are included in the vendor

<link href="{{ asset('vendor/table-view/bootstrap.min.css') }}" rel="stylesheet" />
<link href="{{ asset('vendor/table-view/font-awesome/css/font-awesome.min.css') }}" rel="stylesheet" />
<link href="{{ asset('vendor/table-view/css/themes/tableview-a.css') }}" rel="stylesheet" />

Include the tablview in your view, referencing the variable name given to it

@include('table-view::container', ['tableView' => $usersTableView])

Also include the tablview scripts ** Requires jQuery and v1.9.1 will be included under public/vendor/table-view

<script src="{{ asset('vendor/table-view/js/jquery-1.9.1.min.js') }}"></script>

@include('table-view::scripts')

Middleware Cookie Storage

Selected options for the tableview are easily added to cookie storage with built-in Middleware.

Sort options and limits per page are each added to permanent storage. At any point, a user returning to the tableview will see these options filled with the same values that he/she selected in his/her most recent session.

The search query and page number are temporarily stored during the user's current session. With this, a user could visit something http://tableview.com/blog-articles with the tableview listing articles. When a user views a specific article like http://tableview.com/blog-articles/laravel-blog/article, any link back to http://tableview.com/blog-articles will show the tableview with its most recent page number and search query.

All you have to do:

Edit app/Http/Kernel.php, adding a reference to the Middleware

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

        // Laravel TableView Middleware
        'table-view.storage' => \Witty\LaravelTableView\Middleware\TableViewCookieStorage::class,
    ];

Then add it to the route containing the tableview

    Route::get('/', ['middleware' => 'table-view.storage', function () {

That's it!

It's particular but in just a few lines you have a dynamic table view with powerful functionality. Feel free to customize the tableview and element partial views. Additional themes and styles coming soon.

laravel-table-view's People

Contributors

larkinwhitaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-table-view's Issues

Weird exception in Laravel 5.2 while everything works fine.

I don't know why but everytime the page loads an exception is thrown. Otherwise the page loads fine and everything works, but the exception doesn't provide any info that I can use to find where the problem is.

The exception is:

Exception: #0 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Routing/Router.php(823): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#1 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#3 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#4 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#5 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#6 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#7 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#9 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#10 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#11 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#13 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#14 /home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#15 /home/vagrant/Code/proj/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#16 {main} 

Cookie storage works great. The route is inside the 'web' route group. I'm stumped. Besides crowding my logs, nothing else seems to be wrong

Is this dead?

Wanned to use it, but it's untouched in 2ys and the url tableview.com is not available.

Problem with laravel 5.3.26

Been trying to 'composer update' after adding "witty/laravel-table-view": "dev-master" to my composer.json, and all i get is a list of errors regarding older laravel versions..

(a small part of it looks like this -
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.16
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.2
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.20
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.22
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.25
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.28
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.30
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.31
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.41
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.6
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.1.8
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.2.0
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.2.19
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.2.21
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.2.24
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.2.25
- don't install laravel/framework v5.3.0|don't install illuminate/support v5.2.26
)

Call to undefined method Illuminate\Database\Query\Builder::appends()

        $usersTableView = TableView::collection(\App\User::class)
        ->column('Matricule', 'pocket_number:sort,search')
        ->column('Nom', ['last_name:sort*' => function ($user) {
            return $user->first_name . " " . $user->last_name;
        }])
        ->column('Type',function($user) {
                return $user->typeOfUser($user);
        })
        ->column('Telephone','phone_number:sort,search')

        ->column('Statut','status:sort');
        return view('users.list', [
         'usersTableView' => $usersTableView
        ]);
ErrorException in Builder.php line 2405:
Call to undefined method Illuminate\Database\Query\Builder::appends() (View: /www/resources/views/vendor/table-view/container.blade.php) (View: /www/resources/views/vendor/table-view/container.blade.php)
in Builder.php line 2405
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('/www/storage/framework/views/1668170a0c58f761ea0e8ee9e78890adc3269216.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'usersTableView' => object(LaravelTableView))) in CompilerEngine.php line 59
at CompilerEngine->get('/www/resources/views/users/list.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'usersTableView' => object(LaravelTableView))) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 53
at Response->setContent(object(View)) in Response.php line 201
at Response->__construct(object(View)) in Router.php line 1085
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 95
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(UsersController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\UsersController', 'index') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Rbac.php line 25
at Rbac->handle(object(Request), object(Closure), 'can', 'user.update.all|user.update.drivers')
at call_user_func_array(array(object(Rbac), 'handle'), array(object(Request), object(Closure), 'can', 'user.update.all|user.update.drivers')) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 28
at Authenticate->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 28
at Authenticate->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54

Originial Where Clause gets overwritten

Hi,

I have a situation where my table is built on data that already has a query limiting the results.

However when I use the search functionality, my previous where clause gets overwritten and a larger result set gets returned.

Is it possible to keep the original where clause?

I had a look in the search repository at the addsearch function but I couldn't work out how to amend the code to stop this from happening.

not full view loaded

when i try to add

@include('students.container', ['tableView' => $studentsTableView])

this is the result i got in browser like the header and footer not loaded =(

image

is there something i mess to add or something ?

Sort and search column with relation model

Hi any idea how to sort or search column with relation model?

example, i want to show table of Post where belongsTo User, and in some column, i want to display users.name instead just posts.user_id

Also we need to enable search by users.name too ..

Thanks

Invalid argument for column value. Allowed values are sort and search

Hi, Thanks for your package :)

Btw, i have found some bug, if you define some column without argument, then it will throws exception

This is my code, and exception throwed because line of email column

$tableView  = $tableView
            ->column('Nama', 'name:sort,search')
            ->column('Email', 'email')
            ->column(function ($role) 
            {
                return '<a class="btn btn-success btn-xs" href="/users/' . $role->id . '">View</a>';
            })
            ->build();

Thanks

Laravel 5.2

Hi,

Can you add support for Laravel 5.2?

I can't install because the illuminate/support version is 5.1 only

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.