gmlo89 / datatable
DataTable / Laravel
dev-master
2019-12-05 19:00 UTC
This package is auto-updated.
Last update: 2020-01-05 19:10:29 UTC
README
DataTable & 可搜索特性
安装
运行 composer
composer require gmlo89/datatable @dev
发布组件以添加到您的 js 中
php artisan vendor:publish --provider="Gmlo\DataTable\DataTableServiceProvider" --tag="vue-components"
在您的 js 文件中导入
require('./../vendor/datatable/app');
可搜索特性
将特性添加到您的模型中,并设置 "searcheable" 属性以指定要搜索的字段。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Gmlo\DataTable\Traits\Searchable; class Products extends Model { use Searchable; protected $searchables = ['name', 'code']; }
使用
$result = Products::search()->orderBy('name')->get();
您可以可选地发送参数
$limit = 20; // Number of items to take. Default: 15. $query_input = 'filter'; // Name of the field sent by the request to search. Default: 'query' $result = Products::search($limit, $query_input)->orderBy('name')->get();
DataTable
VueJs DataTable for Laravel 6
前端示例
<admin-table url-create="/product/create" url-api="/api/product" :headers="[ { value: 'name', text: 'Product', sorteable: true }, { value: 'price', text: 'Price' }, { value: 'brand_name', text: 'Brand' }, { value: 'button', slot: 'columnaction' } ]"> <!-- Title of table --> <template v-slot:title> <h1><i class="fas fa-shopping-basket text-primary"></i> Products</h1> </template> <!-- customized cell --> <template v-slot:columnaction="{ item }"> <a :href="`/product/${item.id}`" class="btn btn-sm"> Detalles <i class="fas fa-caret-right"></i> </a> </template> </admin-table>
后端
$query = Product::join('categories', 'products.category_id', '=', 'categories.id') ->join('brands', 'products.brand_id', '=', 'brands.id') ->selectRaw('products.*, categories.name as category_name, brands.name as brand_name'); return dataTable()->query($query) ->setFilters( 'categories.name', 'products.name', 'products.code', 'products.ean', 'products.upc', 'products.part_number', 'products.model', 'brands.name' ) ->configColumn('price', function($value, $row){ return '$' . number_format($value, 2) . ' ' . $row->currency; }) ->get();
许可
MIT
自由软件,太棒了!