-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselect.js
executable file
·61 lines (53 loc) · 2.24 KB
/
select.js
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
52
53
54
55
56
57
58
59
60
61
class Select {
constructor(params) {
this.id = params.id ? params.id : 'select-' + Math.floor(Math.random() * 101);
this.holder = params.holder ? params.holder : '';
this.title = params.title ? params.title : undefined;
this.hint = params.hint ? params.hint : '';
this.disable = params.disable ? 'disabled' : '';
this.source = params.source ? params.source : undefined;
this.col = params.col ? params.col : undefined;
this.depend = params.depend ? params.depend : undefined;
this.options = '';
let me = this;
if (this.source.data) {
me.options += `<option value="" disabled selected>Select Me!</option>`
$.each(this.source.data, function (k, v) {
me.options += `<option value="${v[me.source.def.value]}">${v[me.source.def.title]}</option>`
});
}
if (this.source.ajax) {
if(!this.depend){
let res = new Request({
url: this.source.ajax,
type: 'GET',
params: [],
onSuccess: function (data) {
me.options += `<option value="" disabled selected >Select Me!</option>`
$.each(data, function (k, v) {
me.options += `<option value="${v[me.source.def.value]}">${v[me.source.def.title]}</option>`
});
$('#' + me.id).html(me.options);
$('#' + me.id).trigger('bind-' + me.id);
}
}).send();
}
}
if (this.holder) {
$('#' + this.holder).html(this.render());
}
}
render() {
let col = '';
if (this.col) {
col = `col-md-${this.col}`;
}
return `<div class="form-group ${col}">
<label for="exampleFormControlSelect1">${this.title}</label>
<select class="form-control" id="${this.id}" name="${this.id}" ${this.disable}>
${this.options}
</select>
<small id="${this.id}Help" class="form-text text-muted">${this.hint}</small>
</div>`;
}
}