-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatable.tag
131 lines (108 loc) · 4.29 KB
/
datatable.tag
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
<datatable>
<div class="panel panel-default table-responsive" style="margin-bottom: { opts.marginBottom }">
<table class="table { opts.tableClass }">
<thead if="{ showTableHeader }">
<tr>
<th each={ column, index in columns } style="width: { column.width }">
{ tr(column.label) }
</th>
</tr>
</thead>
<tbody>
<tr if="{ !data || (Array.isArray(data) && data.length == 0) || Object.keys(data).length == 0 }">
<td colspan="{ columns.length }">{ message }</td>
</tr>
<tr class="{ rowClassCallback(elem) }" each="{ elem, index in data }">
<td each="{ column, index2 in columns }" colspan="{ column.colspan }" if="{ column.showBody == undefined }" style="width: { column.width }">
<virtual if="{ column.linkCallback == undefined }">
<span if="{ column.callback != undefined && column.type == 'subtable' }">
<datatable message="{ column.message }" columns="{ column.callback(elem, index) }" data="{ elem }" actions="{ actions }" no-table-footer no-table-header margin-bottom="0"></datatable>
</span>
<span if="{ column.callback != undefined && column.type != 'subtable'}">
{ column.callback(elem, index) }
</span>
<span if="{ column.callback == undefined && elem[column.value] != undefined }">
{ elem[column.value] }
</span>
<span if="{ column.callback == undefined && elem[column.label] != undefined }">
{ elem[column.label] }
</span>
</virtual>
<a if="{ column.linkCallback != undefined }" href="{ column.linkCallback(elem, index) }" target="_blank">
<span if="{ column.callback != undefined && column.type != 'subtable'}">
{ column.callback(elem, index) }
</span>
<span if="{ column.callback == undefined && elem[column.label] != undefined }">
{ elem[column.label] }
</span>
<span if={ column.isRedirectedCallback != undefined && column.isRedirectedCallback(elem) } class="badge">Redirected</span>
</a>
<virtual each="{ action in actions }" if="{ column.label == 'Actions' }">
<a if="{ action.action == 'callback' }"
disabled="{ action.isDisabledCallback !== undefined && action.isDisabledCallback(elem) }"
onclick="{ action.callback.bind(this, elem) }"
class="btn btn-sm btn-{ action.btnType }">
{ (action.labelCallback !== undefined && action.labelCallback(elem)) || tr(action.label) }</a>
<a href="{ action.url }/?id={ encodeURIComponent(elem['ID']) }" if="{ action.action == 'link' }" class="btn-flat">{ tr(action.label) }</a>
<a data-toggle="modal" data-target="{ action.target }" onclick="{ modalOpened }" if="{ action.action == 'modal-link' }" class="btn">{ tr(action.label) }</a>
</virtual>
</td>
</tr>
</tbody>
<tfoot if="{ showTableFooter }">
<tr>
<th each={ column, index in columns }>
{ tr(column.label) }
</th>
</tr>
</tfoot>
</table>
</div>
<script>
var self = this;
self.columns = opts.columns || [];
self.columnNames = opts.columnNames;
self.events = opts.events || riot.observable();
self.disableLinking = opts.disableLinking === 'true';
self.rowClassCallback = opts.rowClassCallback || function() {};
self.showTableHeader = opts.noTableHeader === undefined;
self.showTableFooter = opts.noTableFooter === undefined;
self.data = opts.data;
self.tr = opts.trFunc || function(str) {
return str;
};
self.modalOpened = function(e) {
self.events.trigger('modal-opened', e, this.elem, self);
}
self.linkBaseURL = opts.linkBaseurl || '';
self.actions = opts.actions;
if (opts.url == undefined) {
self.url = '';
} else {
self.url = replacePlaceholdersWithQueryVariables(opts.url);
}
self.message = 'No data loaded or available yet.';
self.on('mount', function() {
self.events.on('create', onCreate);
if (opts.actions !== undefined) {
self.actions = opts.actions;
}
});
self.on('unmount', function() {
self.events.off('create', onCreate);
});
self.on('update', function() {
if (opts.message !== undefined) {
self.message = opts.message;
}
if (opts.actions !== undefined) {
self.actions = opts.actions;
}
});
var onCreate = function(elem) {
self.data.unshift(elem);
self.update();
};
</script>
</datatable>