Skip to content

Commit c1a5ff2

Browse files
committed
add master details
1 parent ee9083f commit c1a5ff2

File tree

8 files changed

+186
-60
lines changed

8 files changed

+186
-60
lines changed

dist/index.js

Lines changed: 84 additions & 29 deletions
Large diffs are not rendered by default.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/App.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
:fields="fields"
55
:opts="options"
66
:selectable="true"
7+
:details="childOptions"
78
@edit="alert('row edit button clicked')"
89
@delete="alert('row delete button clicked')"
910
/>
@@ -78,6 +79,9 @@ export default {
7879
defaultContent: '<a href="javascript:void(0);" data-action="edit" class="btn btn-primary btn-sm"><i class="mdi mdi-square-edit-outline"></i> Edit</a>' +
7980
'<span data-action="delete" class="btn btn-danger btn-sm"><i class="mdi mdi-delete"></i> Delete</span>'
8081
}
82+
},
83+
childOptions: {
84+
template: 'I\'m a child yall'
8185
}
8286
}
8387
},

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/mix-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"/index.js": "/index.js?id=915839e14c4389063946",
3-
"/index.js.map": "/index.js.map?id=40d6d135deb585d76b5c"
2+
"/index.js": "/index.js?id=ee0fa1ac94620aef931e",
3+
"/index.js.map": "/index.js.map?id=4a2f13a304f773353510"
44
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-datatables-net",
33
"description": "Vue jQuery DataTables.net wrapper component",
4-
"version": "0.8.5",
4+
"version": "0.8.6",
55
"author": "[email protected]",
66
"license": "MIT",
77
"main": "lib/index.js",

src/VdtnetTable.vue

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export default {
6666
*/
6767
selectable: {
6868
type: Boolean
69+
},
70+
details: {
71+
type: Object
6972
}
7073
},
7174
data() {
@@ -92,8 +95,8 @@ export default {
9295
}
9396
},
9497
created() {
95-
const vm = this
96-
const jq = vm.jq
98+
const vm = this
99+
const jq = vm.jq
97100
98101
// allow user to override default options
99102
if (vm.opts) {
@@ -133,21 +136,7 @@ export default {
133136
}
134137
135138
if (field.template) {
136-
// must be string template
137-
const res = Vue.compile(`<div>${field.template}</div>`)
138-
field.render = (data, type, row, meta) => {
139-
const comp = new Vue({
140-
data: {
141-
data: data,
142-
type: type,
143-
row: row,
144-
meta: meta
145-
},
146-
render: res.render,
147-
staticRenderFns: res.staticRenderFns
148-
}).$mount()
149-
return jq(comp.$el).html()
150-
}
139+
field.render = vm.compileTemplate(field.template)
151140
}
152141
153142
if (field.render) {
@@ -161,27 +150,33 @@ export default {
161150
162151
if (vm.selectable) {
163152
// expand column
164-
vm.options.columns = [{
153+
const col = {
165154
orderable: false,
166155
className: 'select-checkbox',
167156
data: null,
168157
defaultContent: '',
169-
title: '<input type="checkbox" class="select-all-checkbox">',
170-
targets: 0
171-
}].concat(vm.options.columns)
158+
title: '<input type="checkbox" class="select-all-checkbox">'
159+
}
160+
vm.options.columns.splice(1, 0, col)
172161
173162
// console.log(vm.options.columns)
174163
vm.options.select = jq.extend(
175164
vm.options.select || {},
176165
{
177166
style: 'os',
178-
selector: 'td:first-child'
167+
selector: 'td.select-checkbox'
179168
}
180169
)
170+
}
181171
182-
if (!vm.options.order) {
183-
vm.options.order = [[1, 'asc']]
172+
if (vm.details) {
173+
const col = {
174+
orderable: false,
175+
className: 'details-control',
176+
data: null,
177+
defaultContent: vm.details.icons || '<span class="details-control-plus" title="Show Details">+</span><span class="details-control-minus" title="Hide Details">-</span>'
184178
}
179+
vm.options.columns.splice(1, 0, col)
185180
}
186181
},
187182
mounted() {
@@ -238,7 +233,7 @@ export default {
238233
// detect if row action
239234
let tr = that.closest('tr')
240235
if (tr) {
241-
if (tr.hasClass('child')) {
236+
if (tr.attr('role') !== 'row') {
242237
tr = tr.prev()
243238
}
244239
const row = vm.dataTable.row(tr)
@@ -251,6 +246,59 @@ export default {
251246
}
252247
}
253248
})
249+
250+
if (vm.details) {
251+
// must be string template
252+
const renderFunc = vm.compileTemplate(vm.details.template)
253+
254+
// handle master/details
255+
// Add event listener for opening and closing details
256+
$el.on('click', 'td.details-control', (e) => {
257+
e.preventDefault()
258+
e.stopPropagation()
259+
const target = jq(e.target)
260+
let that = target
261+
let tr = that.closest('tr')
262+
if (tr.attr('role') !== 'row') {
263+
tr = tr.prev()
264+
}
265+
const row = vm.dataTable.row( tr )
266+
267+
if ( row.child.isShown() ) {
268+
// This row is already open - close it
269+
row.child.hide()
270+
tr.removeClass('shown')
271+
}
272+
else {
273+
// Open this row
274+
const data = row.data()
275+
row.child( renderFunc(data, 'child', row, tr) ).show()
276+
tr.addClass('shown')
277+
}
278+
})
279+
}
280+
},
281+
methods: {
282+
compileTemplate(template) {
283+
const vm = this
284+
const jq = vm.jq
285+
const res = Vue.compile(`<div>${template}</div>`)
286+
const renderFunc = (data, type, row, meta) => {
287+
const comp = new Vue({
288+
data: {
289+
data: data,
290+
type: type,
291+
row: row,
292+
meta: meta
293+
},
294+
render: res.render,
295+
staticRenderFns: res.staticRenderFns
296+
}).$mount()
297+
return jq(comp.$el).html()
298+
}
299+
300+
return renderFunc
301+
}
254302
}
255303
}
256304
</script>
@@ -262,4 +310,23 @@ export default {
262310
float: left;
263311
padding-right: 10px;
264312
}
313+
.shown .details-control-plus
314+
{
315+
cursor: pointer;
316+
display: none;
317+
}
318+
.details-control-minus
319+
{
320+
cursor: pointer;
321+
display: none;
322+
}
323+
.shown .details-control-minus
324+
{
325+
cursor: pointer;
326+
display: inline;
327+
}
328+
.details-control {
329+
cursor: pointer;
330+
font-weight: 700;
331+
}
265332
</style>

0 commit comments

Comments
 (0)