Skip to content

Commit e96753f

Browse files
committed
Fixed slot HEAD. Added tfoot and column search. Added class to thead.
Signed-off-by: Dmitry Mazurov <[email protected]>
1 parent 5404803 commit e96753f

File tree

1 file changed

+119
-41
lines changed

1 file changed

+119
-41
lines changed

src/VdtnetTable.vue

100644100755
Lines changed: 119 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,62 @@
99
:class="className"
1010
cellpadding="0"
1111
>
12-
<thead>
12+
<thead
13+
:class="theadClassName"
14+
>
1315
<tr>
1416
<th
1517
v-for="(field, i) in options.columns"
16-
:key="i"
17-
:class="field.className"
18+
:key="'head_'+i"
19+
:class="field.classHeaderName"
1820
>
1921
<slot
2022
:name="'HEAD_'+field.name"
2123
:field="field"
2224
:i="i"
2325
>
24-
<div v-html="field.title" />
26+
<input
27+
v-if="field.name === '_select_checkbox'"
28+
type="checkbox"
29+
class="select-all-checkbox d-print-none"
30+
>
31+
<div
32+
v-else
33+
v-html="field.label"
34+
/>
2535
</slot>
2636
</th>
2737
</tr>
2838
</thead>
39+
<tfoot
40+
v-if="!hideFooter"
41+
:class="tfootClassName"
42+
>
43+
<tr>
44+
<th
45+
v-for="(field, i) in options.columns"
46+
:key="'foot_'+i"
47+
:class="field.classFooterName"
48+
>
49+
<slot
50+
:name="'FOOT_'+field.name"
51+
:field="field"
52+
:i="i"
53+
>
54+
<input
55+
v-if="columnSearch && (field.searchable || typeof field.searchable === 'undefined')"
56+
:placeholder="field.label"
57+
:class="columnSearchClassName"
58+
type="search"
59+
/>
60+
<div
61+
v-else-if="!columnSearch"
62+
v-html="field.label"
63+
/>
64+
</slot>
65+
</th>
66+
</tr>
67+
</tfoot>
2968
</table>
3069
</div>
3170
</template>
@@ -54,6 +93,31 @@ export default {
5493
type: String,
5594
default: 'table-responsive d-print-inline'
5695
},
96+
/**
97+
* Set the input column search classes.
98+
*
99+
* @type String
100+
*/
101+
columnSearchClassName: {
102+
type: String,
103+
default: 'form-control form-control-sm'
104+
},
105+
/**
106+
* Set the tfoot classes.
107+
*
108+
* @type String
109+
*/
110+
tfootClassName: {
111+
type: String,
112+
},
113+
/**
114+
* Set the thead classes.
115+
*
116+
* @type String
117+
*/
118+
theadClassName: {
119+
type: String,
120+
},
57121
/**
58122
* Set the table classes you wish to use, default with bootstrap4
59123
* but you can override with: themeforest, foundation, etc..
@@ -123,6 +187,15 @@ export default {
123187
hideFooter: {
124188
type: Boolean
125189
},
190+
/**
191+
* true to hide the individual column search of the table
192+
*
193+
* @type Boolean
194+
*/
195+
columnSearch: {
196+
type: Boolean,
197+
default: false
198+
},
126199
/**
127200
* The details column configuration of master/details.
128201
*
@@ -173,74 +246,67 @@ export default {
173246
const that = this
174247
const jq = that.jq
175248
249+
let startCol = 0
250+
let icol = 0
251+
176252
that.tableId = that.id || `vdtnetable${myUniqueId++}`
177253
178254
// allow user to override default options
179255
if (that.opts) {
180256
that.options = jq.extend({}, that.options, that.opts)
181257
}
182-
},
183-
mounted() {
184-
const that = this
185-
const jq = that.jq
186-
const $el = jq(that.$refs.table)
187-
const orders = []
188258
189-
let startCol = 0
190-
let icol = 0
191-
192-
// if fields are passed in, generate column definition
193-
// from our custom fields schema
194259
if (that.fields) {
195260
const fields = that.fields
196261
let cols = that.options.columns
262+
let orders = that.options.order
197263
198264
for (let k in fields) {
199265
const field = fields[k]
200266
field.name = field.name || k
201267
202268
// disable search and sort for local field
203269
if (field.isLocal) {
204-
field.searchable = false
205-
field.sortable = false
270+
field.searchable = false
271+
field.sortable = false
206272
}
207273
208274
// generate
209275
let col = {
210-
title: field.label || field.name,
211-
data: field.data || field.name,
212-
width: field.width,
213-
name: field.name,
214-
className: field.className,
215-
index: field.index || (icol + 1)
216-
}
217-
218-
if (field.width) {
219-
col.width = field.width
276+
label: field.label || field.name,
277+
data: field.data || field.name,
278+
width: field.width,
279+
name: field.name,
280+
className: field.className,
281+
index: field.index || (icol + 1)
220282
}
221283
222284
if (field.hasOwnProperty('defaultContent')) {
223-
col.defaultContent = field.defaultContent
285+
col.defaultContent = field.defaultContent
224286
}
225287
226288
if (field.hasOwnProperty('sortable')) {
227-
col.orderable = field.sortable
289+
col.orderable = field.sortable
228290
}
229291
230292
if (field.hasOwnProperty('visible')) {
231-
col.visible = field.visible
293+
col.visible = field.visible
232294
}
233295
234296
if (field.hasOwnProperty('searchable')) {
235-
col.searchable = field.searchable
297+
col.searchable = field.searchable
236298
}
237299
238300
if (field.hasOwnProperty('editField')) {
239301
col.editField = field.editField
240302
}
241303
242-
if (field.template || that.$scopedSlots[field.name]) {
243-
field.render = that.compileTemplate(field, that.$scopedSlots[field.name])
304+
if (field.hasOwnProperty('classHeaderName')) {
305+
col.classHeaderName = field.classHeaderName
306+
}
307+
308+
if (field.hasOwnProperty('classFooterName')) {
309+
col.classFooterName = field.classFooterName
244310
}
245311
246312
if (field.render) {
@@ -263,14 +329,8 @@ export default {
263329
264330
icol++
265331
}
266-
267-
// sort columns
268-
cols = cols.sort((a, b) => a.index - b.index)
269332
}
270333
271-
// apply orders calculated from above
272-
that.options.order = that.options.order || orders
273-
274334
if (that.selectCheckbox) {
275335
that.selectCheckbox = that.selectCheckbox || 1
276336
@@ -282,7 +342,6 @@ export default {
282342
className: 'select-checkbox d-print-none',
283343
data: null,
284344
defaultContent: '',
285-
title: '<input type="checkbox" class="select-all-checkbox d-print-none">',
286345
index: (that.selectCheckbox - 1)
287346
}
288347
that.options.columns.splice(that.selectCheckbox - 1, 0, col)
@@ -319,19 +378,38 @@ export default {
319378
if (startCol > 0) {
320379
if (that.options.order) {
321380
that.options.order.forEach((v) => {
322-
v[0] += startCol
381+
v[0] += startCol
323382
})
324383
} else {
325384
that.options.order = [[startCol, 'asc']]
326385
}
327386
}
387+
},
388+
mounted() {
389+
const that = this
390+
const jq = that.jq
391+
const $el = jq(that.$refs.table)
328392
329393
// handle local data loader
330394
if (that.dataLoader) {
331395
delete that.options.ajax
332396
that.options.serverSide = false
333397
}
334398
399+
if (!that.hideFooter && that.columnSearch) {
400+
that.options.initComplete = function () {
401+
this.api().columns().every(function () {
402+
const that = this;
403+
404+
jq('input', this.footer()).on('keyup change clear search', function () {
405+
if (that.search() !== this.value) {
406+
that.search(this.value).draw();
407+
}
408+
})
409+
})
410+
}
411+
}
412+
335413
// you can access and update the that.options and $el here before we create the DataTable
336414
that.$emit('table-creating', that, $el)
337415

0 commit comments

Comments
 (0)