Skip to content

Commit bb04b47

Browse files
authored
Merge pull request #49 from dima-bzz/master
Fixed slot Header. Added slot footer and column search
2 parents 5404803 + 3f1b4ae commit bb04b47

File tree

2 files changed

+207
-37
lines changed

2 files changed

+207
-37
lines changed

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ Our component parameters:
117117
type: String,
118118
default: 'table-responsive d-print-inline'
119119
},
120+
/**
121+
* Set the input column search classes.
122+
*
123+
* @type String
124+
*/
125+
columnSearchClassName: {
126+
type: String,
127+
default: 'form-control form-control-sm'
128+
},
129+
/**
130+
* Set the tfoot classes.
131+
*
132+
* @type String
133+
*/
134+
tfootClassName: {
135+
type: String,
136+
},
137+
/**
138+
* Set the thead classes.
139+
*
140+
* @type String
141+
*/
142+
theadClassName: {
143+
type: String,
144+
},
120145
/**
121146
* Set the table classes you wish to use, default with bootstrap4
122147
* but you can override with: themeforest, foundation, etc..
@@ -186,6 +211,24 @@ Our component parameters:
186211
hideFooter: {
187212
type: Boolean
188213
},
214+
/**
215+
* true to hide the tfoot of the table
216+
*
217+
* @type Boolean
218+
*/
219+
hideTfoot: {
220+
type: Boolean,
221+
default: true
222+
},
223+
/**
224+
* true to hide the individual column search of the table
225+
*
226+
* @type Boolean
227+
*/
228+
columnSearch: {
229+
type: Boolean,
230+
default: false
231+
},
189232
/**
190233
* The details column configuration of master/details.
191234
*
@@ -217,6 +260,8 @@ fields: {
217260
- `visible` false to hide
218261
- `width` to provide custom width
219262
- `className` set column class names
263+
- `classHeaderName` set header class names
264+
- `classFooterName` set footer class names
220265
- `defaultContent` provide default html when no data available
221266
- `render` custom cell rendering function https://datatables.net/reference/option/columns.render
222267
- `template` simple vue template for the field. See example App.
@@ -341,6 +386,14 @@ Let say you have a column `description`, you can provide table head template for
341386
</template>
342387
```
343388
389+
## Customizable table footer (th) columns
390+
Let say you have a column `description`, you can provide table footer template for the description column like so:
391+
```html
392+
<template slot="FOOT_description">
393+
<h1>desc</h1>
394+
</template>
395+
```
396+
344397
## dom (Searching and Toolbar)
345398
`dom` configuration defines how jQuery DataTables components are rendered - https://datatables.net/reference/option/dom
346399

src/VdtnetTable.vue

100644100755
+154-37
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="!hideTfoot"
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,24 @@ export default {
123187
hideFooter: {
124188
type: Boolean
125189
},
190+
/**
191+
* true to hide the tfoot of the table
192+
*
193+
* @type Boolean
194+
*/
195+
hideTfoot: {
196+
type: Boolean,
197+
default: true
198+
},
199+
/**
200+
* true to hide the individual column search of the table
201+
*
202+
* @type Boolean
203+
*/
204+
columnSearch: {
205+
type: Boolean,
206+
default: false
207+
},
126208
/**
127209
* The details column configuration of master/details.
128210
*
@@ -173,27 +255,20 @@ export default {
173255
const that = this
174256
const jq = that.jq
175257
258+
let startCol = 0
259+
let icol = 0
260+
176261
that.tableId = that.id || `vdtnetable${myUniqueId++}`
177262
178263
// allow user to override default options
179264
if (that.opts) {
180265
that.options = jq.extend({}, that.options, that.opts)
181266
}
182-
},
183-
mounted() {
184-
const that = this
185-
const jq = that.jq
186-
const $el = jq(that.$refs.table)
187-
const orders = []
188-
189-
let startCol = 0
190-
let icol = 0
191267
192-
// if fields are passed in, generate column definition
193-
// from our custom fields schema
194268
if (that.fields) {
195269
const fields = that.fields
196270
let cols = that.options.columns
271+
let orders = that.options.order
197272
198273
for (let k in fields) {
199274
const field = fields[k]
@@ -207,18 +282,14 @@ export default {
207282
208283
// generate
209284
let col = {
210-
title: field.label || field.name,
285+
label: field.label || field.name,
211286
data: field.data || field.name,
212287
width: field.width,
213288
name: field.name,
214289
className: field.className,
215290
index: field.index || (icol + 1)
216291
}
217292
218-
if (field.width) {
219-
col.width = field.width
220-
}
221-
222293
if (field.hasOwnProperty('defaultContent')) {
223294
col.defaultContent = field.defaultContent
224295
}
@@ -236,21 +307,22 @@ export default {
236307
}
237308
238309
if (field.hasOwnProperty('editField')) {
239-
col.editField = field.editField
310+
col.editField = field.editField
240311
}
241312
242-
if (field.template || that.$scopedSlots[field.name]) {
243-
field.render = that.compileTemplate(field, that.$scopedSlots[field.name])
313+
if (field.hasOwnProperty('classHeaderName')) {
314+
col.classHeaderName = field.classHeaderName
244315
}
245316
246-
if (field.render) {
247-
if (!field.render.templated) {
248-
let myRender = field.render
249-
field.render = function() {
250-
return myRender.apply(that, Array.prototype.slice.call(arguments))
251-
}
252-
}
317+
if (field.hasOwnProperty('classFooterName')) {
318+
col.classFooterName = field.classFooterName
319+
}
320+
321+
if (field.template) {
322+
col.template = field.template
323+
}
253324
325+
if (field.render) {
254326
col.render = field.render
255327
}
256328
@@ -263,14 +335,8 @@ export default {
263335
264336
icol++
265337
}
266-
267-
// sort columns
268-
cols = cols.sort((a, b) => a.index - b.index)
269338
}
270339
271-
// apply orders calculated from above
272-
that.options.order = that.options.order || orders
273-
274340
if (that.selectCheckbox) {
275341
that.selectCheckbox = that.selectCheckbox || 1
276342
@@ -282,7 +348,6 @@ export default {
282348
className: 'select-checkbox d-print-none',
283349
data: null,
284350
defaultContent: '',
285-
title: '<input type="checkbox" class="select-all-checkbox d-print-none">',
286351
index: (that.selectCheckbox - 1)
287352
}
288353
that.options.columns.splice(that.selectCheckbox - 1, 0, col)
@@ -325,13 +390,65 @@ export default {
325390
that.options.order = [[startCol, 'asc']]
326391
}
327392
}
393+
},
394+
mounted() {
395+
const that = this
396+
const jq = that.jq
397+
const $el = jq(that.$refs.table)
398+
let cols = that.options.columns
399+
400+
for (let k in cols) {
401+
const col = cols[k]
402+
403+
if (col.template || that.$scopedSlots[col.name]) {
404+
col.render = that.compileTemplate(col, that.$scopedSlots[col.name])
405+
}
406+
407+
if (col.render) {
408+
if (!col.render.templated) {
409+
let myRender = col.render
410+
col.render = function() {
411+
return myRender.apply(that, Array.prototype.slice.call(arguments))
412+
}
413+
}
414+
}
415+
416+
if (col.template) {
417+
delete col.template
418+
}
419+
}
328420
329421
// handle local data loader
330422
if (that.dataLoader) {
331423
delete that.options.ajax
332424
that.options.serverSide = false
333425
}
334426
427+
if (!that.hideFooter && that.columnSearch) {
428+
that.options.initComplete = function () {
429+
let api = this.api();
430+
let state = api.state.loaded();
431+
432+
api.columns().every(function () {
433+
const that = this;
434+
const colIdx = this.index();
435+
436+
if(state){
437+
let colSearch = state.columns[colIdx].search;
438+
if (colSearch.search){
439+
jq('input', this.footer()).val(colSearch.search);
440+
}
441+
}
442+
443+
jq('input', this.footer()).on('keyup change clear search', function () {
444+
if (that.search() !== this.value) {
445+
that.search(this.value).draw();
446+
}
447+
})
448+
})
449+
}
450+
}
451+
335452
// you can access and update the that.options and $el here before we create the DataTable
336453
that.$emit('table-creating', that, $el)
337454

0 commit comments

Comments
 (0)