Skip to content

Commit a7aa081

Browse files
authored
Merge pull request #217 from yajra/layout-view
feat: load layout from view using selector
2 parents 7f17830 + 847a53d commit a7aa081

File tree

3 files changed

+122
-24
lines changed

3 files changed

+122
-24
lines changed

src/Html/Enums/LayoutPosition.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yajra\DataTables\Html\Enums;
6+
7+
enum LayoutPosition: string
8+
{
9+
case Start = 'Start';
10+
case End = 'End';
11+
}

src/Html/Layout.php

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Fluent;
88
use Illuminate\Support\Traits\Macroable;
9+
use Yajra\DataTables\Html\Enums\LayoutPosition;
910

1011
class Layout extends Fluent
1112
{
@@ -18,43 +19,77 @@ public static function make(array $options = []): static
1819

1920
public function topStart(string|array|null $options, int $order = 0): static
2021
{
21-
return $this->top($options, $order, 'Start');
22+
return $this->top($options, $order, LayoutPosition::Start);
23+
}
24+
25+
public function top(array|string|null $options, ?int $order = null, ?LayoutPosition $position = null): static
26+
{
27+
if ($order > 0) {
28+
$this->attributes["top{$order}{$position?->value}"] = $options;
29+
} else {
30+
$this->attributes["top{$position?->value}"] = $options;
31+
}
32+
33+
return $this;
2234
}
2335

2436
public function topEnd(string|array|null $options, int $order = 0): static
2537
{
26-
return $this->top($options, $order, 'End');
38+
return $this->top($options, $order, LayoutPosition::End);
2739
}
2840

29-
public function bottomStart(string|array|null $options, int $order = 0): static
41+
public function topEndView(string $selector, int $order = 0): static
3042
{
31-
return $this->bottom($options, $order, 'Start');
43+
return $this->topView($selector, $order, LayoutPosition::End);
3244
}
3345

34-
public function bottomEnd(string|array|null $options, int $order = 0): static
46+
public function topView(string $selector, int $order = 0, ?LayoutPosition $position = null): static
3547
{
36-
return $this->bottom($options, $order, 'End');
48+
$script = "function() { return $('{$selector}').html(); }";
49+
50+
return $this->top($script, $order, $position);
3751
}
3852

39-
public function top(array|string|null $options, ?int $order = null, ?string $position = null): static
53+
public function bottomStartView(string $selector, int $order = 0): static
4054
{
41-
if ($order > 0) {
42-
$this->attributes["top{$order}{$position}"] = $options;
43-
} else {
44-
$this->attributes["top{$position}"] = $options;
45-
}
55+
return $this->bottomView($selector, $order, LayoutPosition::Start);
56+
}
4657

47-
return $this;
58+
public function bottomView(string $selector, int $order = 0, ?LayoutPosition $position = null): static
59+
{
60+
$script = "function() { return $('{$selector}').html(); }";
61+
62+
return $this->bottom($script, $order, $position);
4863
}
4964

50-
public function bottom(array|string|null $options, ?int $order = null, ?string $position = null): static
65+
public function bottom(array|string|null $options, ?int $order = null, ?LayoutPosition $position = null): static
5166
{
5267
if ($order > 0) {
53-
$this->attributes["bottom{$order}{$position}"] = $options;
68+
$this->attributes["bottom{$order}{$position?->value}"] = $options;
5469
} else {
55-
$this->attributes["bottom{$position}"] = $options;
70+
$this->attributes["bottom{$position?->value}"] = $options;
5671
}
5772

5873
return $this;
5974
}
75+
76+
public function bottomEndView(string $selector, int $order = 0): static
77+
{
78+
return $this->bottomView($selector, $order, LayoutPosition::End);
79+
}
80+
81+
public function topStartView(string $selector, int $order = 0): static
82+
{
83+
return $this->topView($selector, $order, LayoutPosition::Start);
84+
}
85+
86+
public function bottomStart(string|array|null $options, int $order = 0): static
87+
{
88+
return $this->bottom($options, $order, LayoutPosition::Start);
89+
}
90+
91+
public function bottomEnd(string|array|null $options, int $order = 0): static
92+
{
93+
return $this->bottom($options, $order, LayoutPosition::End);
94+
}
6095
}

tests/LayoutTest.php

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\Test;
66
use Yajra\DataTables\Html\Builder;
7+
use Yajra\DataTables\Html\Enums\LayoutPosition;
78
use Yajra\DataTables\Html\Layout;
89

910
class LayoutTest extends TestCase
@@ -36,16 +37,16 @@ public function it_can_set_positions(): void
3637
$layout->bottom('test', 1);
3738
$this->assertEquals('test', $layout->get('bottom1'));
3839

39-
$layout->top('test', 1, 'Start');
40+
$layout->top('test', 1, LayoutPosition::Start);
4041
$this->assertEquals('test', $layout->get('top1Start'));
4142

42-
$layout->bottom('test', 1, 'Start');
43+
$layout->bottom('test', 1, LayoutPosition::Start);
4344
$this->assertEquals('test', $layout->get('bottom1Start'));
4445

45-
$layout->top('test', 1, 'End');
46+
$layout->top('test', 1, LayoutPosition::End);
4647
$this->assertEquals('test', $layout->get('top1End'));
4748

48-
$layout->bottom('test', 1, 'End');
49+
$layout->bottom('test', 1, LayoutPosition::End);
4950
$this->assertEquals('test', $layout->get('bottom1End'));
5051
}
5152

@@ -62,10 +63,10 @@ public function it_can_be_used_in_builder(): void
6263
$layout->bottomEnd('test');
6364
$layout->top('test', 1);
6465
$layout->bottom('test', 1);
65-
$layout->top('test', 1, 'Start');
66-
$layout->bottom('test', 1, 'Start');
67-
$layout->top('test', 1, 'End');
68-
$layout->bottom('test', 1, 'End');
66+
$layout->top('test', 1, LayoutPosition::Start);
67+
$layout->bottom('test', 1, LayoutPosition::Start);
68+
$layout->top('test', 1, LayoutPosition::End);
69+
$layout->bottom('test', 1, LayoutPosition::End);
6970
});
7071

7172
$this->assertArrayHasKey('layout', $builder->getAttributes());
@@ -127,4 +128,55 @@ public function it_can_accept_array_as_parameter_in_builder(): void
127128
$this->assertArrayHasKey('layout', $builder->getAttributes());
128129
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
129130
}
131+
132+
#[Test]
133+
public function it_can_accept_callable_as_parameter_in_builder(): void
134+
{
135+
$builder = resolve(Builder::class);
136+
$builder->layout(fn (Layout $layout) => $layout->top('test'));
137+
138+
$this->assertArrayHasKey('layout', $builder->getAttributes());
139+
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
140+
}
141+
142+
#[Test]
143+
public function it_can_accept_js_selector_for_layout_content(): void
144+
{
145+
$builder = resolve(Builder::class);
146+
$builder->layout(fn (Layout $layout) => $layout->topView('#test'));
147+
148+
$this->assertArrayHasKey('layout', $builder->getAttributes());
149+
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
150+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['top']);
151+
152+
$builder->layout(fn (Layout $layout) => $layout->bottomView('#test'));
153+
$this->assertArrayHasKey('layout', $builder->getAttributes());
154+
$this->assertArrayHasKey('bottom', $builder->getAttributes()['layout']);
155+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['bottom']);
156+
157+
$builder->layout(fn (Layout $layout) => $layout->topStartView('#test'));
158+
$this->assertArrayHasKey('layout', $builder->getAttributes());
159+
$this->assertArrayHasKey('topStart', $builder->getAttributes()['layout']);
160+
$this->assertEquals("function() { return $('#test').html(); }",
161+
$builder->getAttributes()['layout']['topStart']);
162+
163+
$builder->layout(fn (Layout $layout) => $layout->topEndView('#test'));
164+
$this->assertArrayHasKey('layout', $builder->getAttributes());
165+
$this->assertArrayHasKey('topEnd', $builder->getAttributes()['layout']);
166+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['topEnd']);
167+
168+
$builder->layout(fn (Layout $layout) => $layout->bottomStartView('#test'));
169+
$this->assertArrayHasKey('layout', $builder->getAttributes());
170+
$this->assertArrayHasKey('bottomStart', $builder->getAttributes()['layout']);
171+
$this->assertEquals("function() { return $('#test').html(); }",
172+
$builder->getAttributes()['layout']['bottomStart']);
173+
174+
$builder->layout(fn (Layout $layout) => $layout->bottomEndView('#test'));
175+
$this->assertArrayHasKey('layout', $builder->getAttributes());
176+
$this->assertArrayHasKey('bottomEnd', $builder->getAttributes()['layout']);
177+
$this->assertEquals(
178+
"function() { return $('#test').html(); }",
179+
$builder->getAttributes()['layout']['bottomEnd']
180+
);
181+
}
130182
}

0 commit comments

Comments
 (0)