datatables.net-vue3 2.1.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,10 +28,7 @@ declare type __VLS_WithTemplateSlots<T, S> = T & {
28
28
  };
29
29
  };
30
30
 
31
- declare const _default: InstallableComponent;
32
- export default _default;
33
-
34
- declare const _default_2: __VLS_WithTemplateSlots<DefineComponent<__VLS_TypePropsToRuntimeProps<{
31
+ export declare const DataTable: __VLS_WithTemplateSlots<DefineComponent<__VLS_TypePropsToRuntimeProps<{
35
32
  /**
36
33
  * Load data for the table's content from an Ajax source
37
34
  */
@@ -76,7 +73,10 @@ options?: Config | undefined;
76
73
  default: (_: {}) => any;
77
74
  }>;
78
75
 
79
- export declare type InstallableComponent = typeof _default_2 & {
76
+ declare const _default: InstallableComponent;
77
+ export default _default;
78
+
79
+ export declare type InstallableComponent = typeof DataTable & {
80
80
  install: Exclude<Plugin_2['install'], undefined>;
81
81
  };
82
82
 
@@ -1 +1 @@
1
- "use strict";const r=require("vue"),v=["childRow","column-sizing","column-visibility","destroy","draw","error","init","length","order","page","preDraw","preInit","preXhr","processing","requestChild","search","stateLoadParams","stateLoaded","stateSaveParams","xhr","autoFill","preAutoFill","buttons-action","buttons-processing","column-reorder","key","key-blur","key-focus","key-refocus","key-return-submit","responsive-display","responsive-resize","rowgroup-datasrc","pre-row-reorder","row-reorder","row-reordered","dtsb-inserted","deselect","select","select-blur","selectItems","selectStyle","user-select","stateRestore-change"];let c;const g={name:"Datatables.netVue",inheritAttrs:!1,use(o){c=o}},h=r.defineComponent({...g,props:{ajax:null,columns:null,data:null,options:null},emits:v,setup(o,{expose:d}){const s=o,f=r.ref(null),a=r.ref(),b=r.ref([]);r.watch(()=>s.data,e=>{var l,n,u,m;let t=(l=a.value)==null?void 0:l.data().toArray();for(let i of e)t!=null&&t.includes(i)||(n=a.value)==null||n.row.add(i);if(typeof t<"u")for(let i of t)e.includes(i)||(u=a.value)==null||u.row((k,y)=>y===i).remove();(m=a.value)==null||m.rows().invalidate().draw(!1),p(e)},{deep:!0}),r.onMounted(()=>{const e=r.getCurrentInstance();let t=s.options||{};if(s.data&&(t.data=s.data,p(t.data)),s.columns&&(t.columns=s.columns),s.ajax&&(t.ajax=s.ajax),!c)throw new Error("DataTables library not set. See https://datatables.net/tn/19 for details.");a.value=new c(r.unref(f),t);for(let l of v)a.value&&e&&a.value.on(l,function(){var n=Array.from(arguments),u=n.shift();n.unshift({event:u,dt:a}),n.unshift(l),e.emit.apply(e,n)})}),r.onBeforeUnmount(()=>{var e;(e=a.value)==null||e.destroy(!0)});function p(e){b.value=e.value?e.value.slice():e.slice()}return d({dt:a}),(e,t)=>t[0]||(r.setBlockTracking(-1),t[0]=r.createElementVNode("div",{class:"datatable"},[r.createElementVNode("table",r.mergeProps({ref_key:"table",ref:f},e.$attrs,{style:{width:"100%"}}),[r.renderSlot(e.$slots,"default")],16)]),r.setBlockTracking(1),t[0])}}),w=(()=>{const o=h;return o.install=d=>{d.component("Datatables.netVue",o)},o})();module.exports=w;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const l=require("vue"),v=["childRow","column-sizing","column-visibility","destroy","draw","error","init","length","order","page","preDraw","preInit","preXhr","processing","requestChild","search","stateLoadParams","stateLoaded","stateSaveParams","xhr","autoFill","preAutoFill","buttons-action","buttons-processing","column-reorder","key","key-blur","key-focus","key-refocus","key-return-submit","responsive-display","responsive-resize","rowgroup-datasrc","pre-row-reorder","row-reorder","row-reordered","dtsb-inserted","deselect","select","select-blur","selectItems","selectStyle","user-select","stateRestore-change"];let f;const k={name:"Datatables.netVue",inheritAttrs:!1,use(i){f=i}},b=l.defineComponent({...k,props:{ajax:null,columns:null,data:null,options:null},emits:v,setup(i,{expose:d}){const s=i,p=l.ref(null),c={},u=l.ref(),h=l.ref([]);l.watch(()=>s.data,t=>{let e=u.value;if(e){for(var n=Object.keys(c),a=0;a<n.length;a++)delete c[n[a]];e.clear(),e.rows.add(t).draw(!1)}},{deep:!0}),l.onMounted(()=>{const t=l.getCurrentInstance();let e=s.options||{};if(s.data&&(e.data=s.data,g(e.data)),s.columns&&(e.columns=s.columns,y(e.columns,t)),s.ajax&&(e.ajax=s.ajax),e.columnDefs||(e.columnDefs=[]),t){let n=Object.keys(t.slots);for(let a=0;a<n.length;a++){let r=n[a];if(r.match(/^column\-/)){let o=r.replace("column-","");e.columnDefs.push({target:o.match(/^\d+$/)?parseInt(o):o+":name",render:"#"+r})}}y(e.columnDefs,t)}if(!f)throw new Error("DataTables library not set. See https://datatables.net/tn/19 for details.");u.value=new f(l.unref(p),e);for(let n of v)u.value&&t&&u.value.on(n,function(){var a=Array.from(arguments),r=a.shift();a.unshift({event:r,dt:u}),a.unshift(n),t.emit.apply(t,a)})}),l.onBeforeUnmount(()=>{var t;(t=u.value)==null||t.destroy(!0)});function g(t){h.value=t.value?t.value.slice():t.slice()}function m(t){return function(e,n,a,r){let o=r.row+","+r.col;if(!c[o]){let w=l.h("div",t({cellData:e,colIndex:r.col,rowData:a,rowIndex:r.row,type:n}));c[o]=document.createElement("div"),l.render(w,c[o])}return c[o]}}function y(t,e){if(e)for(let a=0;a<t.length;a++){let r=t[a];if(typeof r.render=="string"&&r.render.charAt(0)==="#"){var n=r.render.replace("#","");e.slots[n]&&(r.render=m(e.slots[n]))}else if(typeof r.render=="object"&&typeof r.render.display=="string"&&r.render.display.charAt(0)==="#"){var n=r.render.display.replace("#","");e.slots[n]&&(r.render.display=m(e.slots[n]))}}}return d({dt:u}),(t,e)=>e[0]||(l.setBlockTracking(-1),e[0]=l.createElementVNode("div",{class:"datatable"},[l.createElementVNode("table",l.mergeProps({ref_key:"table",ref:p},t.$attrs,{style:{width:"100%"}}),[l.renderSlot(t.$slots,"default")],16)]),l.setBlockTracking(1),e[0])}}),D=(()=>{const i=b;return i.install=d=>{d.component("Datatables.netVue",i)},i})();exports.DataTable=b;exports.default=D;
@@ -1,5 +1,5 @@
1
- import { defineComponent as g, ref as d, watch as k, onMounted as x, getCurrentInstance as D, unref as _, onBeforeUnmount as j, setBlockTracking as v, createElementVNode as b, mergeProps as A, renderSlot as S } from "vue";
2
- const y = [
1
+ import { defineComponent as k, ref as d, watch as x, onMounted as j, getCurrentInstance as I, unref as A, onBeforeUnmount as E, setBlockTracking as v, createElementVNode as b, mergeProps as R, renderSlot as S, h as T, render as _ } from "vue";
2
+ const h = [
3
3
  "childRow",
4
4
  "column-sizing",
5
5
  "column-visibility",
@@ -45,14 +45,14 @@ const y = [
45
45
  "user-select",
46
46
  "stateRestore-change"
47
47
  ];
48
- let c;
48
+ let f;
49
49
  const C = {
50
50
  name: "Datatables.netVue",
51
51
  inheritAttrs: !1,
52
- use(s) {
53
- c = s;
52
+ use(o) {
53
+ f = o;
54
54
  }
55
- }, E = /* @__PURE__ */ g({
55
+ }, L = /* @__PURE__ */ k({
56
56
  ...C,
57
57
  props: {
58
58
  ajax: null,
@@ -60,61 +60,103 @@ const C = {
60
60
  data: null,
61
61
  options: null
62
62
  },
63
- emits: y,
64
- setup(s, { expose: u }) {
65
- const r = s, f = d(null), a = d(), h = d([]);
66
- k(
67
- () => r.data,
68
- (e) => {
69
- var l, o, i, m;
70
- let t = (l = a.value) == null ? void 0 : l.data().toArray();
71
- for (let n of e)
72
- t != null && t.includes(n) || (o = a.value) == null || o.row.add(n);
73
- if (typeof t < "u")
74
- for (let n of t)
75
- e.includes(n) || (i = a.value) == null || i.row((I, w) => w === n).remove();
76
- (m = a.value) == null || m.rows().invalidate().draw(!1), p(e);
63
+ emits: h,
64
+ setup(o, { expose: u }) {
65
+ const s = o, p = d(null), c = {}, i = d(), g = d([]);
66
+ x(
67
+ () => s.data,
68
+ (t) => {
69
+ let e = i.value;
70
+ if (e) {
71
+ for (var n = Object.keys(c), a = 0; a < n.length; a++)
72
+ delete c[n[a]];
73
+ e.clear(), e.rows.add(t).draw(!1);
74
+ }
77
75
  },
78
76
  {
79
77
  deep: !0
80
78
  }
81
- ), x(() => {
82
- const e = D();
83
- let t = r.options || {};
84
- if (r.data && (t.data = r.data, p(t.data)), r.columns && (t.columns = r.columns), r.ajax && (t.ajax = r.ajax), !c)
79
+ ), j(() => {
80
+ const t = I();
81
+ let e = s.options || {};
82
+ if (s.data && (e.data = s.data, w(e.data)), s.columns && (e.columns = s.columns, y(e.columns, t)), s.ajax && (e.ajax = s.ajax), e.columnDefs || (e.columnDefs = []), t) {
83
+ let n = Object.keys(t.slots);
84
+ for (let a = 0; a < n.length; a++) {
85
+ let r = n[a];
86
+ if (r.match(/^column\-/)) {
87
+ let l = r.replace("column-", "");
88
+ e.columnDefs.push({
89
+ target: l.match(/^\d+$/) ? parseInt(l) : l + ":name",
90
+ render: "#" + r
91
+ });
92
+ }
93
+ }
94
+ y(e.columnDefs, t);
95
+ }
96
+ if (!f)
85
97
  throw new Error(
86
98
  "DataTables library not set. See https://datatables.net/tn/19 for details."
87
99
  );
88
- a.value = new c(_(f), t);
89
- for (let l of y)
90
- a.value && e && a.value.on(l, function() {
91
- var o = Array.from(arguments), i = o.shift();
92
- o.unshift({ event: i, dt: a }), o.unshift(l), e.emit.apply(e, o);
100
+ i.value = new f(A(p), e);
101
+ for (let n of h)
102
+ i.value && t && i.value.on(n, function() {
103
+ var a = Array.from(arguments), r = a.shift();
104
+ a.unshift({ event: r, dt: i }), a.unshift(n), t.emit.apply(t, a);
93
105
  });
94
- }), j(() => {
95
- var e;
96
- (e = a.value) == null || e.destroy(!0);
106
+ }), E(() => {
107
+ var t;
108
+ (t = i.value) == null || t.destroy(!0);
97
109
  });
98
- function p(e) {
99
- h.value = e.value ? e.value.slice() : e.slice();
110
+ function w(t) {
111
+ g.value = t.value ? t.value.slice() : t.slice();
112
+ }
113
+ function m(t) {
114
+ return function(e, n, a, r) {
115
+ let l = r.row + "," + r.col;
116
+ if (!c[l]) {
117
+ let D = T("div", t({
118
+ cellData: e,
119
+ colIndex: r.col,
120
+ rowData: a,
121
+ rowIndex: r.row,
122
+ type: n
123
+ }));
124
+ c[l] = document.createElement("div"), _(D, c[l]);
125
+ }
126
+ return c[l];
127
+ };
128
+ }
129
+ function y(t, e) {
130
+ if (e)
131
+ for (let a = 0; a < t.length; a++) {
132
+ let r = t[a];
133
+ if (typeof r.render == "string" && r.render.charAt(0) === "#") {
134
+ var n = r.render.replace("#", "");
135
+ e.slots[n] && (r.render = m(e.slots[n]));
136
+ } else if (typeof r.render == "object" && typeof r.render.display == "string" && r.render.display.charAt(0) === "#") {
137
+ var n = r.render.display.replace("#", "");
138
+ e.slots[n] && (r.render.display = m(e.slots[n]));
139
+ }
140
+ }
100
141
  }
101
142
  return u({
102
- dt: a
103
- }), (e, t) => t[0] || (v(-1), t[0] = b("div", { class: "datatable" }, [
104
- b("table", A({
143
+ dt: i
144
+ }), (t, e) => e[0] || (v(-1), e[0] = b("div", { class: "datatable" }, [
145
+ b("table", R({
105
146
  ref_key: "table",
106
- ref: f
107
- }, e.$attrs, { style: { width: "100%" } }), [
108
- S(e.$slots, "default")
147
+ ref: p
148
+ }, t.$attrs, { style: { width: "100%" } }), [
149
+ S(t.$slots, "default")
109
150
  ], 16)
110
- ]), v(1), t[0]);
151
+ ]), v(1), e[0]);
111
152
  }
112
153
  }), P = /* @__PURE__ */ (() => {
113
- const s = E;
114
- return s.install = (u) => {
115
- u.component("Datatables.netVue", s);
116
- }, s;
154
+ const o = L;
155
+ return o.install = (u) => {
156
+ u.component("Datatables.netVue", o);
157
+ }, o;
117
158
  })();
118
159
  export {
160
+ L as DataTable,
119
161
  P as default
120
162
  };
@@ -1 +1 @@
1
- (function(e,l){typeof exports=="object"&&typeof module<"u"?module.exports=l(require("vue")):typeof define=="function"&&define.amd?define(["vue"],l):(e=typeof globalThis<"u"?globalThis:e||self,e.datatables=e.datatables||{},e.datatables["net-vue3"]=l(e.Vue))})(this,function(e){"use strict";const l=["childRow","column-sizing","column-visibility","destroy","draw","error","init","length","order","page","preDraw","preInit","preXhr","processing","requestChild","search","stateLoadParams","stateLoaded","stateSaveParams","xhr","autoFill","preAutoFill","buttons-action","buttons-processing","column-reorder","key","key-blur","key-focus","key-refocus","key-return-submit","responsive-display","responsive-resize","rowgroup-datasrc","pre-row-reorder","row-reorder","row-reordered","dtsb-inserted","deselect","select","select-blur","selectItems","selectStyle","user-select","stateRestore-change"];let f;const y={name:"Datatables.netVue",inheritAttrs:!1,use(n){f=n}},h=e.defineComponent({...y,props:{ajax:null,columns:null,data:null,options:null},emits:l,setup(n,{expose:c}){const s=n,p=e.ref(null),r=e.ref(),w=e.ref([]);e.watch(()=>s.data,t=>{var i,o,u,b;let a=(i=r.value)==null?void 0:i.data().toArray();for(let d of t)a!=null&&a.includes(d)||(o=r.value)==null||o.row.add(d);if(typeof a<"u")for(let d of a)t.includes(d)||(u=r.value)==null||u.row((k,v)=>v===d).remove();(b=r.value)==null||b.rows().invalidate().draw(!1),m(t)},{deep:!0}),e.onMounted(()=>{const t=e.getCurrentInstance();let a=s.options||{};if(s.data&&(a.data=s.data,m(a.data)),s.columns&&(a.columns=s.columns),s.ajax&&(a.ajax=s.ajax),!f)throw new Error("DataTables library not set. See https://datatables.net/tn/19 for details.");r.value=new f(e.unref(p),a);for(let i of l)r.value&&t&&r.value.on(i,function(){var o=Array.from(arguments),u=o.shift();o.unshift({event:u,dt:r}),o.unshift(i),t.emit.apply(t,o)})}),e.onBeforeUnmount(()=>{var t;(t=r.value)==null||t.destroy(!0)});function m(t){w.value=t.value?t.value.slice():t.slice()}return c({dt:r}),(t,a)=>a[0]||(e.setBlockTracking(-1),a[0]=e.createElementVNode("div",{class:"datatable"},[e.createElementVNode("table",e.mergeProps({ref_key:"table",ref:p},t.$attrs,{style:{width:"100%"}}),[e.renderSlot(t.$slots,"default")],16)]),e.setBlockTracking(1),a[0])}});return(()=>{const n=h;return n.install=c=>{c.component("Datatables.netVue",n)},n})()});
1
+ (function(l,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],r):(l=typeof globalThis<"u"?globalThis:l||self,r((l.datatables=l.datatables||{},l.datatables["net-vue3"]={}),l.Vue))})(this,function(l,r){"use strict";const m=["childRow","column-sizing","column-visibility","destroy","draw","error","init","length","order","page","preDraw","preInit","preXhr","processing","requestChild","search","stateLoadParams","stateLoaded","stateSaveParams","xhr","autoFill","preAutoFill","buttons-action","buttons-processing","column-reorder","key","key-blur","key-focus","key-refocus","key-return-submit","responsive-display","responsive-resize","rowgroup-datasrc","pre-row-reorder","row-reorder","row-reordered","dtsb-inserted","deselect","select","select-blur","selectItems","selectStyle","user-select","stateRestore-change"];let f;const w={name:"Datatables.netVue",inheritAttrs:!1,use(d){f=d}},y=r.defineComponent({...w,props:{ajax:null,columns:null,data:null,options:null},emits:m,setup(d,{expose:p}){const o=d,b=r.ref(null),u={},c=r.ref(),v=r.ref([]);r.watch(()=>o.data,t=>{let e=c.value;if(e){for(var s=Object.keys(u),a=0;a<s.length;a++)delete u[s[a]];e.clear(),e.rows.add(t).draw(!1)}},{deep:!0}),r.onMounted(()=>{const t=r.getCurrentInstance();let e=o.options||{};if(o.data&&(e.data=o.data,D(e.data)),o.columns&&(e.columns=o.columns,g(e.columns,t)),o.ajax&&(e.ajax=o.ajax),e.columnDefs||(e.columnDefs=[]),t){let s=Object.keys(t.slots);for(let a=0;a<s.length;a++){let n=s[a];if(n.match(/^column\-/)){let i=n.replace("column-","");e.columnDefs.push({target:i.match(/^\d+$/)?parseInt(i):i+":name",render:"#"+n})}}g(e.columnDefs,t)}if(!f)throw new Error("DataTables library not set. See https://datatables.net/tn/19 for details.");c.value=new f(r.unref(b),e);for(let s of m)c.value&&t&&c.value.on(s,function(){var a=Array.from(arguments),n=a.shift();a.unshift({event:n,dt:c}),a.unshift(s),t.emit.apply(t,a)})}),r.onBeforeUnmount(()=>{var t;(t=c.value)==null||t.destroy(!0)});function D(t){v.value=t.value?t.value.slice():t.slice()}function h(t){return function(e,s,a,n){let i=n.row+","+n.col;if(!u[i]){let j=r.h("div",t({cellData:e,colIndex:n.col,rowData:a,rowIndex:n.row,type:s}));u[i]=document.createElement("div"),r.render(j,u[i])}return u[i]}}function g(t,e){if(e)for(let a=0;a<t.length;a++){let n=t[a];if(typeof n.render=="string"&&n.render.charAt(0)==="#"){var s=n.render.replace("#","");e.slots[s]&&(n.render=h(e.slots[s]))}else if(typeof n.render=="object"&&typeof n.render.display=="string"&&n.render.display.charAt(0)==="#"){var s=n.render.display.replace("#","");e.slots[s]&&(n.render.display=h(e.slots[s]))}}}return p({dt:c}),(t,e)=>e[0]||(r.setBlockTracking(-1),e[0]=r.createElementVNode("div",{class:"datatable"},[r.createElementVNode("table",r.mergeProps({ref_key:"table",ref:b},t.$attrs,{style:{width:"100%"}}),[r.renderSlot(t.$slots,"default")],16)]),r.setBlockTracking(1),e[0])}}),k=(()=>{const d=y;return d.install=p=>{p.component("Datatables.netVue",d)},d})();l.DataTable=y,l.default=k,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datatables.net-vue3",
3
- "version": "2.1.3",
3
+ "version": "3.0.0",
4
4
  "description": "Vue3 component for DataTables",
5
5
  "main": "dist/datatables.net-vue3.umd.js",
6
6
  "module": "dist/datatables.net-vue3.mjs",
@@ -29,8 +29,7 @@
29
29
  "vue-tsc": "^1.0.16"
30
30
  },
31
31
  "peerDependencies": {
32
- "datatables.net": "^1.13.1",
33
- "jquery": "^3.6.0",
32
+ "datatables.net": "^2",
34
33
  "vue": "^3.0.5"
35
34
  },
36
35
  "engines": {
@@ -43,8 +42,5 @@
43
42
  "author": {
44
43
  "name": "SpryMedia Ltd",
45
44
  "url": "http://datatables.net"
46
- },
47
- "dependencies": {
48
- "datatables.net-responsive-dt": "^2.4.0"
49
45
  }
50
46
  }
@@ -0,0 +1,249 @@
1
+ <script lang="ts">
2
+ export let DataTablesLib: any;
3
+
4
+ export default {
5
+ name: 'Datatables.netVue',
6
+ inheritAttrs: false,
7
+ use(lib: any) {
8
+ DataTablesLib = lib;
9
+ }
10
+ };
11
+ </script>
12
+
13
+ <script lang="ts" setup>
14
+ // imports
15
+ import {
16
+ h,
17
+ ref,
18
+ render,
19
+ unref,
20
+ watch,
21
+ onMounted,
22
+ onBeforeUnmount,
23
+ getCurrentInstance
24
+ } from 'vue';
25
+ import type { Api, Config, ConfigColumns } from 'datatables.net';
26
+ import dtEvents from './dtEvents';
27
+
28
+ // props
29
+ const props = defineProps<{
30
+ /**
31
+ * Load data for the table's content from an Ajax source
32
+ */
33
+ ajax?: Config['ajax'];
34
+ /**
35
+ * Set column specific initialisation properties.
36
+ */
37
+ columns?: ConfigColumns[];
38
+ /**
39
+ * Data to use as the display data for the table.
40
+ */
41
+ data?: any;
42
+ /**
43
+ * DataTables options
44
+ */
45
+ options?: Config;
46
+ }>();
47
+
48
+ // emits
49
+ defineEmits(dtEvents);
50
+
51
+ // setup
52
+ const table = ref<HTMLTableElement | null>(null);
53
+ const elements: { [key: string]: HTMLElement } = {};
54
+
55
+ // data
56
+ const dt = ref<Api<any>>();
57
+ const oldData = ref<any[]>([]);
58
+
59
+ // computed
60
+
61
+ // watch
62
+ watch(
63
+ () => props.data,
64
+ (newVal) => {
65
+ let api = dt.value;
66
+
67
+ if (!api) {
68
+ return;
69
+ }
70
+
71
+ var keys = Object.keys(elements);
72
+
73
+ for (var i = 0; i < keys.length; i++) {
74
+ delete elements[keys[i]];
75
+ }
76
+
77
+ api.clear();
78
+ api.rows.add(newVal).draw(false);
79
+ },
80
+ {
81
+ deep: true
82
+ }
83
+ );
84
+
85
+ // lifecycle
86
+ onMounted(() => {
87
+ const inst = getCurrentInstance();
88
+ let options: any = props.options || {};
89
+
90
+ if (props.data) {
91
+ options.data = props.data;
92
+ saveOld(options.data);
93
+ }
94
+
95
+ if (props.columns) {
96
+ options.columns = props.columns;
97
+ applyRenderers(options.columns, inst);
98
+ }
99
+
100
+ if (props.ajax) {
101
+ options.ajax = props.ajax;
102
+ }
103
+
104
+ // Auto detect column slots (those using a `column-` prefix,
105
+ // if not using a prefix, they need to be manually assigned).
106
+ if (! options.columnDefs) {
107
+ options.columnDefs = [];
108
+ }
109
+
110
+ if (inst) {
111
+ let slots = Object.keys(inst.slots);
112
+
113
+ for (let i=0 ; i<slots.length ; i++) {
114
+ let name = slots[i];
115
+
116
+ if (name.match(/^column\-/)) {
117
+ let part = name.replace('column-', '');
118
+
119
+ options.columnDefs.push({
120
+ target: part.match(/^\d+$/)
121
+ ? parseInt(part)
122
+ : part + ':name',
123
+ render: '#' + name
124
+ });
125
+ }
126
+ }
127
+
128
+ applyRenderers(options.columnDefs, inst);
129
+ }
130
+
131
+ if (!DataTablesLib) {
132
+ throw new Error(
133
+ 'DataTables library not set. See https://datatables.net/tn/19 for details.'
134
+ );
135
+ }
136
+
137
+ // Create the DataTable!
138
+ dt.value = new DataTablesLib(unref(table), options);
139
+
140
+ // Re-export all DataTables events by listening for them using DataTable's
141
+ // `on` method and then emit them to our Vue component
142
+ for (let eName of dtEvents) {
143
+ if (dt.value && inst) {
144
+ dt.value.on(eName, function () {
145
+ var args = Array.from(arguments);
146
+ var event = args.shift();
147
+
148
+ args.unshift({ event: event, dt: dt });
149
+ args.unshift(eName);
150
+
151
+ inst.emit.apply(inst, args as any);
152
+ });
153
+ }
154
+ }
155
+ });
156
+
157
+ onBeforeUnmount(() => {
158
+ dt.value?.destroy(true);
159
+ });
160
+
161
+ // Methods
162
+ function saveOld(d: any) {
163
+ oldData.value = d.value ? d.value.slice() : d.slice();
164
+ }
165
+
166
+ /**
167
+ * Create a DataTables rendering function for a slot
168
+ *
169
+ * @param slot Slot to render
170
+ */
171
+ function createRenderer(slot: any) {
172
+ return function (
173
+ data: any,
174
+ type: string,
175
+ row: any,
176
+ meta: any
177
+ ) {
178
+ let key = meta.row + ',' + meta.col;
179
+
180
+ if (!elements[key]) {
181
+ let content = h('div', slot({
182
+ cellData: data,
183
+ colIndex: meta.col,
184
+ rowData: row,
185
+ rowIndex: meta.row,
186
+ type
187
+ }));
188
+
189
+ elements[key] = document.createElement('div');
190
+ render(content, elements[key]);
191
+ }
192
+
193
+ return elements[key];
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Check and see if there are slots that should be applied to a column
199
+ *
200
+ * @param columns Array of columns (`columns` or `columnDefs`)
201
+ * @param inst Vue instance
202
+ */
203
+ function applyRenderers(columns: any[], inst: any) {
204
+ if (! inst) {
205
+ return;
206
+ }
207
+
208
+ for (let i=0 ; i<columns.length ; i++) {
209
+ let col = columns[i];
210
+
211
+ // Top level rendering given as a slot name
212
+ if (typeof col.render === 'string' && col.render.charAt(0) === '#') {
213
+ var name = col.render.replace('#', '');
214
+
215
+ if (inst!.slots[name]) {
216
+ col.render = createRenderer(inst!.slots[name]);
217
+ }
218
+ }
219
+ else if (
220
+ // Display orhtogonal data point given as a slot name
221
+ typeof col.render === 'object' &&
222
+ typeof col.render.display === 'string' &&
223
+ col.render.display.charAt(0) === '#'
224
+ ) {
225
+ var name = col.render.display.replace('#', '');
226
+
227
+ if (inst!.slots[name]) {
228
+ col.render.display = createRenderer(inst!.slots[name]);
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ // expose
235
+ defineExpose({
236
+ /**
237
+ * DataTable instance
238
+ */
239
+ dt
240
+ });
241
+ </script>
242
+
243
+ <template>
244
+ <div class="datatable" v-once>
245
+ <table ref="table" v-bind="$attrs" style="width: 100%">
246
+ <slot></slot>
247
+ </table>
248
+ </div>
249
+ </template>
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { App, Plugin } from 'vue';
2
2
 
3
3
  // Import vue component
4
- import { default as component } from './datatables.net-vue.vue';
4
+ import { default as DataTable } from './DataTable.vue';
5
5
 
6
6
  // Define typescript interfaces for installable component
7
- export type InstallableComponent = typeof component & { install: Exclude<Plugin['install'], undefined> };
7
+ export type InstallableComponent = typeof DataTable & { install: Exclude<Plugin['install'], undefined> };
8
8
 
9
9
  // Default export is installable instance of component.
10
10
  // IIFE injects install function into component, allowing component
11
11
  // to be registered via Vue.use() as well as Vue.component(),
12
12
  export default /*#__PURE__*/((): InstallableComponent => {
13
13
  // Assign InstallableComponent type
14
- const installable = component as unknown as InstallableComponent;
14
+ const installable = DataTable as unknown as InstallableComponent;
15
15
 
16
16
  // Attach install function executed by Vue.use()
17
17
  installable.install = (app: App) => {
@@ -20,6 +20,10 @@ export default /*#__PURE__*/((): InstallableComponent => {
20
20
  return installable;
21
21
  })();
22
22
 
23
+ export {
24
+ DataTable
25
+ };
26
+
23
27
  // It's possible to expose named exports when writing components that can
24
28
  // also be used as directives, etc. - eg. import { RollupDemoDirective } from 'rollup-demo';
25
29
  // export const RollupDemoDirective = directive;
@@ -1,152 +0,0 @@
1
- <script lang="ts">
2
- export let DataTablesLib: any
3
-
4
- export default {
5
- name: 'Datatables.netVue',
6
- inheritAttrs: false,
7
- use(lib: any) {
8
- DataTablesLib = lib;
9
- }
10
- }
11
- </script>
12
-
13
- <script lang="ts" setup>
14
- // imports
15
- import { ref, unref, watch, onMounted, onBeforeUnmount, getCurrentInstance} from 'vue';
16
- import type { Api, Config, ConfigColumns } from 'datatables.net';
17
- import dtEvents from './dtEvents';
18
-
19
- // props
20
- const props = defineProps<{
21
- /**
22
- * Load data for the table's content from an Ajax source
23
- */
24
- ajax?: Config['ajax'];
25
- /**
26
- * Set column specific initialisation properties.
27
- */
28
- columns?: ConfigColumns[];
29
- /**
30
- * Data to use as the display data for the table.
31
- */
32
- data?: any;
33
- /**
34
- * DataTables options
35
- */
36
- options?: Config;
37
- }>();
38
-
39
- // emits
40
- defineEmits(dtEvents);
41
-
42
- // setup
43
- const table = ref<HTMLTableElement | null>(null);
44
-
45
- // data
46
- const dt = ref<Api<any>>();
47
- const oldData = ref<any[]>([]);
48
-
49
- // computed
50
-
51
- // watch
52
- watch(
53
- () => props.data,
54
- (newVal) => {
55
- let known = dt.value?.data().toArray();
56
-
57
- // Find any new rows
58
- for (let n of newVal) {
59
- if (!known?.includes(n)) {
60
- dt.value?.row.add(n);
61
- }
62
- }
63
-
64
- // Remove any old rows
65
- if (typeof known !== 'undefined') {
66
- for (let k of known) {
67
- if (!newVal.includes(k)) {
68
- dt.value?.row((_idx: any, d: any) => d === k).remove();
69
- }
70
- }
71
- }
72
-
73
- // Data in other rows might have changes, so we need to invalidate the rows
74
- dt.value?.rows().invalidate().draw(false);
75
-
76
- saveOld(newVal);
77
- },
78
- {
79
- deep: true,
80
- }
81
- );
82
-
83
- // lifecycle
84
- onMounted(() => {
85
- const inst = getCurrentInstance();
86
- let options: any = props.options || {};
87
-
88
- if (props.data) {
89
- options.data = props.data;
90
- saveOld(options.data);
91
- }
92
-
93
- if (props.columns) {
94
- options.columns = props.columns;
95
- }
96
-
97
- if (props.ajax) {
98
- options.ajax = props.ajax;
99
- }
100
-
101
- if (!DataTablesLib) {
102
- throw new Error(
103
- 'DataTables library not set. See https://datatables.net/tn/19 for details.'
104
- );
105
- }
106
-
107
- // Create the DataTable!
108
- dt.value = new DataTablesLib(unref(table), options);
109
-
110
- // Re-export all DataTables events by listening for them using DataTable's
111
- // `on` method and then emit them to our Vue component
112
- for (let eName of dtEvents) {
113
- if (dt.value && inst) {
114
- dt.value.on(eName, function() {
115
- var args = Array.from(arguments);
116
- var event = args.shift();
117
-
118
- args.unshift({ event: event, dt: dt });
119
- args.unshift(eName);
120
-
121
- inst.emit.apply(inst, args as any);
122
- });
123
- }
124
- }
125
- });
126
-
127
- onBeforeUnmount(() => {
128
- dt.value?.destroy(true);
129
- });
130
-
131
- // methods
132
- function saveOld(d: any) {
133
- oldData.value = d.value ? d.value.slice() : d.slice();
134
- }
135
-
136
- // expose
137
- defineExpose({
138
- /**
139
- * DataTable instance
140
- */
141
- dt,
142
- });
143
-
144
- </script>
145
-
146
- <template>
147
- <div class="datatable" v-once>
148
- <table ref="table" v-bind="$attrs" style="width:100%">
149
- <slot></slot>
150
- </table>
151
- </div>
152
- </template>