1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Fluent; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @property string data |
9
|
|
|
* @property string name |
10
|
|
|
* @property string orderable |
11
|
|
|
* @property string searchable |
12
|
|
|
* @property string printable |
13
|
|
|
* @property string exportable |
14
|
|
|
* @property string footer |
15
|
|
|
* @property array attributes |
16
|
|
|
* @see https://datatables.net/reference/option/ for possible columns option |
17
|
|
|
*/ |
18
|
|
|
class Column extends Fluent |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param array $attributes |
22
|
|
|
*/ |
23
|
|
|
public function __construct($attributes = []) |
24
|
|
|
{ |
25
|
|
|
$attributes['orderable'] = isset($attributes['orderable']) ? $attributes['orderable'] : true; |
26
|
|
|
$attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true; |
27
|
|
|
$attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true; |
28
|
|
|
$attributes['printable'] = isset($attributes['printable']) ? $attributes['printable'] : true; |
29
|
|
|
$attributes['footer'] = isset($attributes['footer']) ? $attributes['footer'] : ''; |
30
|
|
|
$attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : []; |
31
|
|
|
|
32
|
|
|
// Allow methods override attribute value |
33
|
|
|
foreach ($attributes as $attribute => $value) { |
34
|
|
|
$method = 'parse' . ucfirst(strtolower($attribute)); |
35
|
|
|
if (method_exists($this, $method)) { |
36
|
|
|
$attributes[$attribute] = $this->$method($value); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (! isset($attributes['name']) && isset($attributes['data'])) { |
41
|
|
|
$attributes['name'] = $attributes['data']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
parent::__construct($attributes); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Parse render attribute. |
49
|
|
|
* |
50
|
|
|
* @param mixed $value |
51
|
|
|
* @return string|null |
52
|
|
|
*/ |
53
|
|
|
public function parseRender($value) |
54
|
|
|
{ |
55
|
|
|
/** @var \Illuminate\Contracts\View\Factory $view */ |
56
|
|
|
$view = app('view'); |
57
|
|
|
$parameters = []; |
58
|
|
|
|
59
|
|
|
if (is_array($value)) { |
60
|
|
|
$parameters = array_except($value, 0); |
61
|
|
|
$value = $value[0]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (is_callable($value)) { |
65
|
|
|
return $value($parameters); |
66
|
|
|
} elseif ($view->exists($value)) { |
67
|
|
|
return $view->make($value)->with($parameters)->render(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $value ? $this->parseRenderAsString($value) : null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function toArray() |
77
|
|
|
{ |
78
|
|
|
return array_except($this->attributes, ['printable', 'exportable', 'footer']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Display render value as is. |
83
|
|
|
* |
84
|
|
|
* @param mixed $value |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
private function parseRenderAsString($value) |
88
|
|
|
{ |
89
|
|
|
return "function(data,type,full,meta){return $value;}"; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|