-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUserAjaxSelectooFactory.php
51 lines (38 loc) · 1.05 KB
/
UserAjaxSelectooFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Dakujem\Selectoo\Examples;
use Nette\Application\LinkGenerator;
/**
* Example Selectoo input factory:
*
* - selects a single user from a list retrieved form an API call using AJAX
*
*
* @author Andrej Rypák <xrypak@gmail.com> [dakujem](https://github.com/dakujem)
*/
class Dkj_UserAjaxSelectooFactory
{
/** @var Dkj_UserRepository */
private $userRepository;
/** @var LinkGenerator */
private $lg;
public function __construct(Dkj_UserRepository $userRepository, LinkGenerator $lg)
{
$this->userRepository = $userRepository;
$this->lg = $lg;
}
public function create($label = null)
{
$input = new Selectoo($label ?? 'User', function($rawValue = null) {
return $this->userRepository->fetchUsers($rawValue); // returns pairs [ id => name ]
}, false);
$link = $this->lg->link('Example:api');
$engine = new Select2Engine();
$engine
->ajax('{url: "' . $link . '", dataType: "json" }')
->placeholder('Select a user', true)
->width('width: 100%', true)
;
$input->setEngine($engine);
return $input;
}
}