Skip to content

Commit 4da6ba3

Browse files
committed
Fix search panes and options
1 parent 15f0df2 commit 4da6ba3

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

src/Html/Editor/Fields/Options.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function yesNo(): static
3333
/**
3434
* Get options from a model.
3535
*
36-
* @param class-string|\Illuminate\Database\Eloquent\Builder $model
36+
* @param class-string<Model>|\Illuminate\Database\Eloquent\Builder $model
3737
* @param string $value
3838
* @param string $key
3939
* @return Collection
@@ -71,13 +71,18 @@ public static function table(
7171
): Collection {
7272
$query = DB::connection($connection)
7373
->table($table)
74-
->select("{$value} as label", "$key as value");
74+
->select("$value as label", "$key as value");
7575

7676
if (is_callable($callback)) {
7777
$callback($query);
7878
}
7979

80-
return $query->get();
80+
return $query->get()->map(function ($row) use ($value, $key) {
81+
return [
82+
'value' => $row->value,
83+
'label' => $row->label,
84+
];
85+
});
8186
}
8287

8388
/**

src/Html/SearchPane.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function hideTotal(bool $value = true): static
211211
/**
212212
* Get options from a model.
213213
*
214-
* @param class-string|EloquentBuilder $model
214+
* @param class-string<\Illuminate\Database\Eloquent\Model>|EloquentBuilder $model
215215
* @param string $value
216216
* @param string $key
217217
* @return $this

tests/SearchPaneTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests;
4+
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
use Yajra\DataTables\Html\SearchPane;
7+
use Yajra\DataTables\Html\Tests\Models\User;
8+
9+
class SearchPaneTest extends TestCase
10+
{
11+
use DatabaseTransactions;
12+
13+
/** @test */
14+
public function it_has_setters()
15+
{
16+
$pane = SearchPane::make();
17+
$pane->className('className')
18+
->header('header')
19+
->show()
20+
->name('name')
21+
->orthogonal('orthogonal')
22+
->hideTotal()
23+
->threshold(1)
24+
->hideCount()
25+
->filterChanged('filterChanged')
26+
->emptyMessage('emptyMessage')
27+
->dtOpts([])
28+
->controls()
29+
->columns([])
30+
->clear()
31+
->cascadePanes();
32+
33+
$this->assertInstanceOf(SearchPane::class, $pane);
34+
$this->assertEquals('className', $pane->className);
35+
$this->assertEquals('header', $pane->header);
36+
$this->assertEquals(true, $pane->show);
37+
$this->assertEquals('name', $pane->name);
38+
$this->assertEquals('orthogonal', $pane->orthogonal);
39+
$this->assertEquals(false, $pane->viewTotal);
40+
$this->assertEquals(1, $pane->threshold);
41+
$this->assertEquals(true, $pane->hideCount);
42+
$this->assertEquals('filterChanged', $pane->filterChanged);
43+
$this->assertEquals('emptyMessage', $pane->emptyMessage);
44+
$this->assertEquals([], $pane->dtOpts);
45+
$this->assertEquals(true, $pane->controls);
46+
$this->assertEquals([], $pane->columns);
47+
$this->assertEquals(true, $pane->clear);
48+
$this->assertEquals(true, $pane->cascadePanes);
49+
50+
$pane->viewTotal();
51+
$this->assertEquals(true, $pane->viewTotal);
52+
}
53+
54+
/** @test */
55+
public function it_has_options()
56+
{
57+
$pane = SearchPane::make()->options([1, 2, 3]);
58+
59+
$this->assertEquals([1, 2, 3], $pane->options);
60+
$this->assertCount(3, $pane->options);
61+
62+
$pane->modelOptions(User::class, 'name');
63+
$this->assertCount(20, $pane->options);
64+
$this->assertIsArray($pane->options[0]);
65+
$this->assertEquals(['value' => 1, 'label' => 'Record-1'], $pane->options[0]);
66+
$this->assertEquals(['value' => 10, 'label' => 'Record-10'], $pane->options[9]);
67+
68+
$pane->tableOptions('users', 'name');
69+
$this->assertCount(20, $pane->options);
70+
$this->assertIsArray($pane->options[0]);
71+
$this->assertEquals(['value' => 1, 'label' => 'Record-1'], $pane->options[0]);
72+
$this->assertEquals(['value' => 10, 'label' => 'Record-10'], $pane->options[9]);
73+
}
74+
}

0 commit comments

Comments
 (0)