PHP code example of m-a-k-o / nova-custom-table-card
1. Go to this page and download the library: Download m-a-k-o/nova-custom-table-card library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
m-a-k-o / nova-custom-table-card example snippets
// in app/Providers/NovaServiceProvder.php
// ...
public function cards()
{
return [
// ...
// all the parameters are }
// ...
public function cards()
{
return [
// ...
// all the parameters are omTableCard\Table\Cell('Order Number'),
// Set sortable to true in a header Cell to allow its column's sorting
(new \Mako\CustomTableCard\Table\Cell('Price'))->sortable(true)->class('text-right'),
], // header
[
(new \Mako\CustomTableCard\Table\Row(
new \Mako\CustomTableCard\Table\Cell('2018091001'),
(new \Mako\CustomTableCard\Table\Cell('20.50'))->class('text-right')->id('price-2')
))->viewLink('/resources/orders/1'),
(new \Mako\CustomTableCard\Table\Row(
new \Mako\CustomTableCard\Table\Cell('2018091002'),
(new \Mako\CustomTableCard\Table\Cell('201.25'))->class('text-right')->id('price-2')
)),
], // data
'Orders', // title
['label' => 'View All', 'link' => '/resources/orders'], // View All
),
];
}
// ...
public function cards()
{
return [
// ...
// all the parameters are \Mako\CustomTableCard\Table\Cell('Order Number'),
// Set sortable to true in a header Cell to allow its column's sorting
(new \Mako\CustomTableCard\Table\Cell('Price'))->sortable(true)->class('text-right'),
])
->data([
(new \Mako\CustomTableCard\Table\Row(
new \Mako\CustomTableCard\Table\Cell('2018091001'),
(new \Mako\CustomTableCard\Table\Cell('20.50'))->class('text-right')->id('price-2')
))->viewLink('/resources/orders/1'),
(new \Mako\CustomTableCard\Table\Row(
new \Mako\CustomTableCard\Table\Cell('2018091002'),
(new \Mako\CustomTableCard\Table\Cell('201.25'))->class('text-right')->id('price-2')
)),
])
->title('Orders')
->viewAll(['label' => 'View All', 'link' => '/resources/orders']),
];
}
namespace App\Nova\Cards;
use App\Models\Order;
class LatestOrders extends \Mako\CustomTableCard\CustomTableCard
{
public function __construct()
{
$header = collect(['Date', 'Order Number', 'Status', 'Price', 'Name']);
$this->title('Latest Orders');
$this->viewAll(['label' => 'View All', 'link' => '/resources/orders']);
// $orders = Order::all();
// Data from you model
$orders = collect([
['date' => '2018-12-01', 'order_number' => '2018120101', 'status' => 'Ordered', 'price' => '20.55', 'name' => 'John Doe'],
['date' => '2018-12-01', 'order_number' => '2018120101', 'status' => 'Ordered', 'price' => '20.55', 'name' => 'John Doe'],
['date' => '2018-12-01', 'order_number' => '2018120101', 'status' => 'Ordered', 'price' => '20.55', 'name' => 'John Doe'],
['date' => '2018-12-01', 'order_number' => '2018120101', 'status' => 'Ordered', 'price' => '20.55', 'name' => 'John Doe'],
['date' => '2018-12-01', 'order_number' => '2018120101', 'status' => 'Ordered', 'price' => '20.55', 'name' => 'John Doe'],
['date' => '2018-12-01', 'order_number' => '2018120101', 'status' => 'Ordered', 'price' => '20.55', 'name' => 'John Doe'],
]);
$this->header($header->map(function($value) {
// Make the Status column sortable
return ($value === 'Status') ?
(new \Mako\CustomTableCard\Table\Cell($value))->sortable(true) :
new \Mako\CustomTableCard\Table\Cell($value);
})->toArray());
$this->data($orders->map(function($order) {
return new \Mako\CustomTableCard\Table\Row(
new \Mako\CustomTableCard\Table\Cell($order['date']),
new \Mako\CustomTableCard\Table\Cell($order['order_number']),
// Instead of alphabetically ordering the status, set a sortableData value for better representation
(new \Mako\CustomTableCard\Table\Cell($order['status'])->sortableData($this->getStatusSortableData($order['status']))),
new \Mako\CustomTableCard\Table\Cell($order['price']),
new \Mako\CustomTableCard\Table\Cell($order['name'])
);
})->toArray());
}
private function getStatusSortableData (string $status) : int
{
switch ($status) {
case 'Ordered':
return 1;
default:
return 0.
}
}
}
protected function cards()
{
return [
......
new \App\Nova\Cards\LatestOrders,
];
}