Completed
Push — master ( ddd7b0...215d1a )
by Arjay
01:22
created

Select2::ajax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html\Editor\Fields;
4
5
/**
6
 * @see https://editor.datatables.net/plug-ins/field-type/editor.select2
7
 */
8
class Select2 extends Select
9
{
10
    protected $type = 'select2';
11
12
    /**
13
     * @param bool $state
14
     * @return $this
15
     */
16
    public function allowClear($state = true)
17
    {
18
        $this->attributes['opts']['allowClear'] = $state;
19
20
        return $this;
21
    }
22
23
    /**
24
     * @param string $text
25
     * @param string|null $id
26
     * @return $this
27
     */
28
    public function placeholder($text = '', $id = null)
29
    {
30
        $this->attributes['opts']['placeholder'] = [
31
            'id'   => $id,
32
            'text' => $text,
33
        ];
34
35
        return $this;
36
    }
37
38
    /**
39
     * Set select2 ajax option.
40
     *
41
     * @param mixed $value
42
     * @return $this
43
     */
44
    public function ajax($value)
45
    {
46
        $this->attributes['opts']['ajax']['url'] = $value;
47
48
        return $this;
49
    }
50
51
    /**
52
     * Set select2 ajaxDelay option.
53
     *
54
     * @param mixed $value
55
     * @return $this
56
     */
57
    public function ajaxDelay($value = 250)
58
    {
59
        $this->attributes['opts']['ajax']['delay'] = $value;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Set select2 ajax data option.
66
     *
67
     * @param mixed $data
68
     * @return $this
69
     */
70
    public function ajaxData($data)
71
    {
72
        if (is_array($data)) {
73
            $script = 'function(params) {';
74
            foreach ($data as $key => $value) {
75
                $script .= " params.{$key} = '{$value}'; ";
76
            }
77
            $script .= 'return params; }';
78
79
            $data = $script;
80
        }
81
82
        $this->attributes['opts']['ajax']['data'] = $data;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Set select2 ajax processResults option to process a paginated results.
89
     *
90
     * @param string $display
91
     * @param string $id
92
     * @return $this
93
     */
94
    public function processPaginatedResults($display = 'text', $id = 'id')
95
    {
96
        $script = 'function(data, params) { ';
97
        $script .= 'params.page = params.page || 1; ';
98
        $script .= "data.data.map(function(e) { e.text = e.{$display}; e.id = e.{$id}; return e; }); ";
99
        $script .= 'return { results: data.data, pagination: { more: data.current_page < data.last_page } };';
100
        $script .= '}';
101
102
        return $this->processResults($script);
103
    }
104
105
    /**
106
     * Set select2 ajax processResults option.
107
     *
108
     * @param string $value
109
     * @return $this
110
     */
111
    public function processResults($value)
112
    {
113
        $this->attributes['opts']['ajax']['processResults'] = $value;
114
115
        return $this;
116
    }
117
}
118