Skip to content

Commit a7b81c5

Browse files
committed
demonstrate native templating
1 parent a92f268 commit a7b81c5

File tree

9 files changed

+131
-62
lines changed

9 files changed

+131
-62
lines changed

example/app.vue

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151
@table-created="doCreated"
5252
>
5353
<template slot="HEAD__details_control">
54-
Show Details
54+
<b>Show Details</b>
55+
</template>
56+
<template
57+
slot="address1"
58+
slot-scope="ctx"
59+
>
60+
<span>{{ ctx.data.street }}, {{ ctx.data.suite }}</span>
5561
</template>
5662
</vdtnet-table>
5763
</div>
@@ -115,9 +121,14 @@ export default {
115121
name: { label: 'Name', sortable: true, searchable: true, defaultOrder: 'desc' },
116122
username: { label: 'Username', sortable: false, searchable: true },
117123
email: { label: 'Email' },
118-
address: {
119-
label: 'Address',
120-
template: '{{ data.street }}, {{ data.suite }}, {{ data.city }} {{ data.zipcode }}'
124+
address1: {
125+
label: 'Address1',
126+
data: 'address'
127+
},
128+
address2: {
129+
label: 'Address2',
130+
data: 'address',
131+
template: '{{ data.city }}, {{ data.zipcode }}'
121132
},
122133
phone: { label: 'Phone' },
123134
website: {

example/index.js

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ __webpack_require__.r(__webpack_exports__);
273273
//
274274
//
275275
//
276+
//
277+
//
278+
//
279+
//
280+
//
281+
//
276282
// this demonstrate with buttons and responsive master/details row
277283

278284
// import buttons and plugins
@@ -341,9 +347,14 @@ __webpack_require__.r(__webpack_exports__);
341347
email: {
342348
label: 'Email'
343349
},
344-
address: {
345-
label: 'Address',
346-
template: '{{ data.street }}, {{ data.suite }}, {{ data.city }} {{ data.zipcode }}'
350+
address1: {
351+
label: 'Address1',
352+
data: 'address'
353+
},
354+
address2: {
355+
label: 'Address2',
356+
data: 'address',
357+
template: '{{ data.city }}, {{ data.zipcode }}'
347358
},
348359
phone: {
349360
label: 'Phone'
@@ -583,20 +594,23 @@ var myUniqueId = 1;
583594
}
584595
},
585596
created: function created() {
586-
var _arguments = arguments;
587597
var vm = this;
588598
var jq = vm.jq;
589-
var orders = [];
590-
var startCol = 0;
591-
var icol = 0;
592599
vm.tableId = vm.id || "vdtnetable".concat(myUniqueId++); // allow user to override default options
593600

594601
if (vm.opts) {
595602
vm.options = jq.extend({}, vm.options, vm.opts);
596-
} // if fields are passed in, generate column definition
603+
}
604+
},
605+
mounted: function mounted() {
606+
var vm = this;
607+
var jq = vm.jq;
608+
var $el = jq(vm.$refs.table);
609+
var orders = [];
610+
var startCol = 0;
611+
var icol = 0; // if fields are passed in, generate column definition
597612
// from our custom fields schema
598613

599-
600614
if (vm.fields) {
601615
var fields = vm.fields;
602616
var cols = vm.options.columns;
@@ -613,8 +627,8 @@ var myUniqueId = 1;
613627

614628
var col = {
615629
title: field.label || field.name,
616-
width: field.width,
617630
data: field.data || field.name,
631+
width: field.width,
618632
name: field.name,
619633
className: field.className
620634
};
@@ -639,8 +653,8 @@ var myUniqueId = 1;
639653
col.searchable = field.searchable;
640654
}
641655

642-
if (field.template) {
643-
field.render = vm.compileTemplate(field);
656+
if (field.template || vm.$scopedSlots[field.name]) {
657+
field.render = vm.compileTemplate(field, vm.$scopedSlots[field.name]);
644658
}
645659

646660
if (field.render) {
@@ -649,7 +663,7 @@ var myUniqueId = 1;
649663
var myRender = field.render;
650664

651665
field.render = function () {
652-
return myRender.apply(vm, _arguments);
666+
return myRender.apply(vm, Array.prototype.slice.call(arguments));
653667
};
654668
})();
655669
}
@@ -728,13 +742,8 @@ var myUniqueId = 1;
728742
if (vm.dataLoader) {
729743
delete vm.options.ajax;
730744
vm.options.serverSide = false;
731-
}
732-
},
733-
mounted: function mounted() {
734-
var _arguments2 = arguments;
735-
var vm = this;
736-
var jq = vm.jq;
737-
var $el = jq(vm.$refs.table); // you can access and update the vm.options and $el here before we create the DataTable
745+
} // you can access and update the vm.options and $el here before we create the DataTable
746+
738747

739748
vm.$emit('table-creating', vm, $el);
740749
vm.dataTable = $el.DataTable(vm.options);
@@ -813,7 +822,7 @@ var myUniqueId = 1;
813822
renderFunc = vm.compileTemplate(vm.details);
814823
} else if (renderFunc) {
815824
renderFunc = function renderFunc() {
816-
return vm.details.render.apply(vm, _arguments2);
825+
return vm.details.render.apply(vm, Array.prototype.slice.call(arguments));
817826
};
818827
} // handle master/details
819828
// Add event listener for opening and closing details
@@ -865,14 +874,30 @@ var myUniqueId = 1;
865874
* Vue.compile a template string and return the compiled function
866875
*
867876
* @param {Object} object with template property
877+
* @param {Object} the slot
868878
* @return {Function} the compiled template function
869879
*/
870-
compileTemplate: function compileTemplate(field) {
880+
compileTemplate: function compileTemplate(field, slot) {
871881
var vm = this;
872882
var jq = vm.jq;
873-
var res = Vue.compile("<div>".concat(field.template, "</div>"));
883+
var res = Vue.compile("<div>".concat(field.template || '', "</div>"));
874884

875885
var renderFunc = function renderFunc(data, type, row, meta) {
886+
var myRender = res.render;
887+
888+
if (slot) {
889+
myRender = function myRender(createElement) {
890+
return createElement('div', [slot({
891+
data: data,
892+
type: type,
893+
row: row,
894+
meta: meta,
895+
vdtnet: vm,
896+
def: field
897+
})]);
898+
};
899+
}
900+
876901
var comp = new Vue({
877902
data: {
878903
data: data,
@@ -882,7 +907,7 @@ var myUniqueId = 1;
882907
vdtnet: vm,
883908
def: field
884909
},
885-
render: res.render,
910+
render: myRender,
886911
staticRenderFns: res.staticRenderFns
887912
}).$mount();
888913
return jq(comp.$el).html();
@@ -22122,11 +22147,25 @@ var render = function() {
2212222147
reloaded: _vm.doAfterReload,
2212322148
"table-creating": _vm.doCreating,
2212422149
"table-created": _vm.doCreated
22125-
}
22150+
},
22151+
scopedSlots: _vm._u([
22152+
{
22153+
key: "address1",
22154+
fn: function(ctx) {
22155+
return [
22156+
_c("span", [
22157+
_vm._v(
22158+
_vm._s(ctx.data.street) + ", " + _vm._s(ctx.data.suite)
22159+
)
22160+
])
22161+
]
22162+
}
22163+
}
22164+
])
2212622165
},
2212722166
[
2212822167
_c("template", { slot: "HEAD__details_control" }, [
22129-
_vm._v("\n Show Details\n ")
22168+
_c("b", [_vm._v("Show Details")])
2213022169
])
2213122170
],
2213222171
2

example/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.

0 commit comments

Comments
 (0)