datatables.net-vue3 1.0.1 → 2.1.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.
package/Readme.md CHANGED
@@ -15,8 +15,13 @@ In your Vue component you can then:
15
15
 
16
16
  ```js
17
17
  import DataTable from 'datatables.net-vue3'
18
+ import DataTablesLib from 'datatables.net';
19
+
20
+ DataTable.use(DataTablesLib);
18
21
  ```
19
22
 
23
+ Note how both the DataTables core and DataTables Vue component must both be imported. This is so the extensions for DataTables can also be imported in this scope and used (see below). This behaviour is **new in v2** of this component and is required for compatibility with DataTables 1.13 and newer.
24
+
20
25
  Which makes a `DataTable` component available. It provides the following parameters:
21
26
 
22
27
  * `columns` - Define the columns array used for [DataTables initialisation](https://datatables.net/reference/option/#datatables%20-%20columns)
@@ -46,13 +51,14 @@ The `DataTable` component provides a single slot that can be used to define the
46
51
 
47
52
  ## Extensions
48
53
 
49
- DataTables provides [a wide range of extensions](https://datatables.net/extensions/index) that singificantly expands its abilities. Extensions can be imported from npm and then registered with DataTables through it's `use()` method - e.g.:
54
+ DataTables provides [a wide range of extensions](https://datatables.net/extensions/index) that significantly expands its abilities. Extensions can be imported from npm as modules directly in the script where you wish to make use of them in your DataTable, and then assign the resulting DataTables library to the DataTables Vue3 component - e.g.:
50
55
 
51
56
  ```js
52
57
  import DataTable from 'datatables.net-vue3'
53
- import Select from 'datatables.net-select';
58
+ import DataTablesLib from 'datatables.net';
59
+ import 'datatables.net-select';
54
60
 
55
- DataTable.use(Select);
61
+ DataTable.use(DataTablesLib);
56
62
  ```
57
63
 
58
64
 
@@ -67,9 +73,31 @@ npm install datatables.net-bs5
67
73
  npm install datatables.net-select-bs5
68
74
  ```
69
75
 
76
+ Your JS import would then be:
77
+
78
+ ```js
79
+ import DataTable from 'datatables.net-vue3'
80
+ import DataTablesLib from 'datatables.net-bs5';
81
+ import 'datatables.net-select-bs5';
82
+
83
+ DataTable.use(DataTablesLib);
84
+ ```
85
+
70
86
  And in your Vue application's CSS (assuming your are using Vite or some other builder which can resolve the `style` property for npm packages):
71
87
 
72
88
  ```css
73
89
  @import 'datatables.net-dt';
74
90
  @import 'datatables.net-select-dt';
75
91
  ```
92
+
93
+
94
+ ## Development
95
+
96
+ Clone this repo and then in your terminal:
97
+
98
+ ```
99
+ npm install
100
+ npm run dev
101
+ ```
102
+
103
+ You will then be able to access various examples in your browser by clicking the link shown in your terminal.
@@ -0,0 +1,83 @@
1
+ import { AllowedComponentProps } from 'vue';
2
+ import type { Api } from 'datatables.net';
3
+ import { ComponentCustomProps } from 'vue';
4
+ import { ComponentOptionsMixin } from 'vue';
5
+ import type { Config } from 'datatables.net';
6
+ import type { ConfigColumns } from 'datatables.net';
7
+ import { DefineComponent } from 'vue';
8
+ import { ExtractPropTypes } from 'vue';
9
+ import { Plugin as Plugin_2 } from 'vue';
10
+ import { PropType } from 'vue';
11
+ import { Ref } from 'vue';
12
+ import { VNodeProps } from 'vue';
13
+
14
+ declare type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
15
+
16
+ declare type __VLS_TypePropsToRuntimeProps<T> = {
17
+ [K in keyof T]-?: {} extends Pick<T, K> ? {
18
+ type: PropType<__VLS_NonUndefinedable<T[K]>>;
19
+ } : {
20
+ type: PropType<T[K]>;
21
+ required: true;
22
+ };
23
+ };
24
+
25
+ declare type __VLS_WithTemplateSlots<T, S> = T & {
26
+ new (): {
27
+ $slots: S;
28
+ };
29
+ };
30
+
31
+ declare const _default: InstallableComponent;
32
+ export default _default;
33
+
34
+ declare const _default_2: __VLS_WithTemplateSlots<DefineComponent<__VLS_TypePropsToRuntimeProps<{
35
+ /**
36
+ * Load data for the table's content from an Ajax source
37
+ */
38
+ ajax?: Config['ajax'];
39
+ /**
40
+ * Set column specific initialisation properties.
41
+ */
42
+ columns?: ConfigColumns[] | undefined;
43
+ /**
44
+ * Data to use as the display data for the table.
45
+ */
46
+ data?: any;
47
+ /**
48
+ * DataTables options
49
+ */
50
+ options?: Config | undefined;
51
+ }>, {
52
+ /**
53
+ * DataTable instance
54
+ */
55
+ dt: Ref<Api<any> | undefined>;
56
+ }, unknown, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, string[], string, VNodeProps & AllowedComponentProps & ComponentCustomProps, Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps<{
57
+ /**
58
+ * Load data for the table's content from an Ajax source
59
+ */
60
+ ajax?: Config['ajax'];
61
+ /**
62
+ * Set column specific initialisation properties.
63
+ */
64
+ columns?: ConfigColumns[] | undefined;
65
+ /**
66
+ * Data to use as the display data for the table.
67
+ */
68
+ data?: any;
69
+ /**
70
+ * DataTables options
71
+ */
72
+ options?: Config | undefined;
73
+ }>>> & {
74
+ [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
75
+ }, {}>, {
76
+ default: (_: {}) => any;
77
+ }>;
78
+
79
+ export declare type InstallableComponent = typeof _default_2 & {
80
+ install: Exclude<Plugin_2['install'], undefined>;
81
+ };
82
+
83
+ export { }
@@ -0,0 +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 d;const w={name:"Datatables.netVue",inheritAttrs:!1,use(o){d=o}},g=r.defineComponent({...w,props:{ajax:null,columns:null,data:null,options:null},emits:v,setup(o,{expose:u}){const s=o,c=r.ref(null),a=r.ref(),b=r.ref([]);r.watch(()=>s.data,e=>{var l,i,p,m;let t=(l=a.value)==null?void 0:l.data().toArray();for(let n of e)t!=null&&t.includes(n)||(i=a.value)==null||i.row.add(n);if(typeof t<"u")for(let n of t)e.includes(n)||(p=a.value)==null||p.row((k,y)=>y===n).remove();(m=a.value)==null||m.rows().invalidate().draw(!1),f(e)},{deep:!0}),r.onMounted(()=>{const e=r.getCurrentInstance();let t=s.options||{};if(s.data&&(t.data=s.data,f(t.data)),s.columns&&(t.columns=s.columns),s.ajax&&(t.ajax=s.ajax),!d)throw new Error("DataTables library not set. See https://datatables.net/tn/19 for details.");a.value=new d(r.unref(c),t);for(let l of v)a.value&&e&&a.value.on(l,i=>{e.emit(l,{event:i,dt:a})})}),r.onBeforeUnmount(()=>{var e;(e=a.value)==null||e.destroy(!0)});function f(e){b.value=e.value?e.value.slice():e.slice()}return u({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:c},e.$attrs,{style:{width:"100%"}}),[r.renderSlot(e.$slots,"default")],16)]),r.setBlockTracking(1),t[0])}}),h=(()=>{const o=g;return o.install=u=>{u.component("Datatables.netVue",o)},o})();module.exports=h;
@@ -0,0 +1,119 @@
1
+ import { defineComponent as h, ref as u, watch as k, onMounted as x, getCurrentInstance as D, unref as _, onBeforeUnmount as j, setBlockTracking as v, createElementVNode as b, mergeProps as S, renderSlot as A } from "vue";
2
+ const y = [
3
+ "childRow",
4
+ "column-sizing",
5
+ "column-visibility",
6
+ "destroy",
7
+ "draw",
8
+ "error",
9
+ "init",
10
+ "length",
11
+ "order",
12
+ "page",
13
+ "preDraw",
14
+ "preInit",
15
+ "preXhr",
16
+ "processing",
17
+ "requestChild",
18
+ "search",
19
+ "stateLoadParams",
20
+ "stateLoaded",
21
+ "stateSaveParams",
22
+ "xhr",
23
+ "autoFill",
24
+ "preAutoFill",
25
+ "buttons-action",
26
+ "buttons-processing",
27
+ "column-reorder",
28
+ "key",
29
+ "key-blur",
30
+ "key-focus",
31
+ "key-refocus",
32
+ "key-return-submit",
33
+ "responsive-display",
34
+ "responsive-resize",
35
+ "rowgroup-datasrc",
36
+ "pre-row-reorder",
37
+ "row-reorder",
38
+ "row-reordered",
39
+ "dtsb-inserted",
40
+ "deselect",
41
+ "select",
42
+ "select-blur",
43
+ "selectItems",
44
+ "selectStyle",
45
+ "user-select",
46
+ "stateRestore-change"
47
+ ];
48
+ let d;
49
+ const C = {
50
+ name: "Datatables.netVue",
51
+ inheritAttrs: !1,
52
+ use(s) {
53
+ d = s;
54
+ }
55
+ }, E = /* @__PURE__ */ h({
56
+ ...C,
57
+ props: {
58
+ ajax: null,
59
+ columns: null,
60
+ data: null,
61
+ options: null
62
+ },
63
+ emits: y,
64
+ setup(s, { expose: i }) {
65
+ const r = s, c = u(null), a = u(), w = u([]);
66
+ k(
67
+ () => r.data,
68
+ (e) => {
69
+ var o, n, p, m;
70
+ let t = (o = a.value) == null ? void 0 : o.data().toArray();
71
+ for (let l of e)
72
+ t != null && t.includes(l) || (n = a.value) == null || n.row.add(l);
73
+ if (typeof t < "u")
74
+ for (let l of t)
75
+ e.includes(l) || (p = a.value) == null || p.row((I, g) => g === l).remove();
76
+ (m = a.value) == null || m.rows().invalidate().draw(!1), f(e);
77
+ },
78
+ {
79
+ deep: !0
80
+ }
81
+ ), x(() => {
82
+ const e = D();
83
+ let t = r.options || {};
84
+ if (r.data && (t.data = r.data, f(t.data)), r.columns && (t.columns = r.columns), r.ajax && (t.ajax = r.ajax), !d)
85
+ throw new Error(
86
+ "DataTables library not set. See https://datatables.net/tn/19 for details."
87
+ );
88
+ a.value = new d(_(c), t);
89
+ for (let o of y)
90
+ a.value && e && a.value.on(o, (n) => {
91
+ e.emit(o, { event: n, dt: a });
92
+ });
93
+ }), j(() => {
94
+ var e;
95
+ (e = a.value) == null || e.destroy(!0);
96
+ });
97
+ function f(e) {
98
+ w.value = e.value ? e.value.slice() : e.slice();
99
+ }
100
+ return i({
101
+ dt: a
102
+ }), (e, t) => t[0] || (v(-1), t[0] = b("div", { class: "datatable" }, [
103
+ b("table", S({
104
+ ref_key: "table",
105
+ ref: c
106
+ }, e.$attrs, { style: { width: "100%" } }), [
107
+ A(e.$slots, "default")
108
+ ], 16)
109
+ ]), v(1), t[0]);
110
+ }
111
+ }), P = /* @__PURE__ */ (() => {
112
+ const s = E;
113
+ return s.install = (i) => {
114
+ i.component("Datatables.netVue", s);
115
+ }, s;
116
+ })();
117
+ export {
118
+ P as default
119
+ };
@@ -0,0 +1 @@
1
+ (function(e,o){typeof exports=="object"&&typeof module<"u"?module.exports=o(require("vue")):typeof define=="function"&&define.amd?define(["vue"],o):(e=typeof globalThis<"u"?globalThis:e||self,e.datatables=e.datatables||{},e.datatables["net-vue3"]=o(e.Vue))})(this,function(e){"use strict";const o=["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 u;const y={name:"Datatables.netVue",inheritAttrs:!1,use(n){u=n}},h=e.defineComponent({...y,props:{ajax:null,columns:null,data:null,options:null},emits:o,setup(n,{expose:c}){const r=n,f=e.ref(null),s=e.ref(),w=e.ref([]);e.watch(()=>r.data,t=>{var l,d,m,b;let a=(l=s.value)==null?void 0:l.data().toArray();for(let i of t)a!=null&&a.includes(i)||(d=s.value)==null||d.row.add(i);if(typeof a<"u")for(let i of a)t.includes(i)||(m=s.value)==null||m.row((x,k)=>k===i).remove();(b=s.value)==null||b.rows().invalidate().draw(!1),p(t)},{deep:!0}),e.onMounted(()=>{const t=e.getCurrentInstance();let a=r.options||{};if(r.data&&(a.data=r.data,p(a.data)),r.columns&&(a.columns=r.columns),r.ajax&&(a.ajax=r.ajax),!u)throw new Error("DataTables library not set. See https://datatables.net/tn/19 for details.");s.value=new u(e.unref(f),a);for(let l of o)s.value&&t&&s.value.on(l,d=>{t.emit(l,{event:d,dt:s})})}),e.onBeforeUnmount(()=>{var t;(t=s.value)==null||t.destroy(!0)});function p(t){w.value=t.value?t.value.slice():t.slice()}return c({dt:s}),(t,a)=>a[0]||(e.setBlockTracking(-1),a[0]=e.createElementVNode("div",{class:"datatable"},[e.createElementVNode("table",e.mergeProps({ref_key:"table",ref:f},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})()});
package/package.json CHANGED
@@ -1,59 +1,37 @@
1
1
  {
2
2
  "name": "datatables.net-vue3",
3
- "version": "1.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Vue3 component for DataTables",
5
- "main": "dist/datatables.net-vue.ssr.js",
5
+ "main": "datatables.net-vue3.umd.js",
6
6
  "browser": "dist/datatables.net-vue.esm.js",
7
- "module": "dist/datatables.net-vue.esm.js",
8
- "unpkg": "dist/datatables.net-vue.min.js",
9
- "types": "dist/types/src/entry.esm.d.ts",
7
+ "module": "dist/datatables.net-vue3.mjs",
8
+ "require": "dist/datatables.net-vue3.js",
9
+ "unpkg": "dist/datatables.net-vue3.umd.js",
10
+ "types": "dist/datatables.net-vue3.d.ts",
10
11
  "files": [
11
12
  "dist/*",
12
13
  "src/**/*.vue"
13
14
  ],
14
15
  "sideEffects": false,
15
16
  "scripts": {
16
- "serve": "vue-cli-service serve dev/serve.ts",
17
- "prebuild": "rimraf ./dist",
18
- "build": "NODE_ENV=production rollup --config build/rollup.config.js",
19
- "build:ssr": "NODE_ENV=production rollup --config build/rollup.config.js --format cjs",
20
- "build:es": "NODE_ENV=production rollup --config build/rollup.config.js --format es",
21
- "build:unpkg": "NODE_ENV=production rollup --config build/rollup.config.js --format iife",
22
- "postbuild": "rimraf ./dist/types/dev ./dist/types/src/entry.d.ts"
23
- },
24
- "dependencies": {
25
- "datatables.net": "1.12.1",
26
- "jquery": "^3.6.0"
17
+ "dev": "vite --host",
18
+ "build": "cross-env NODE_ENV=production vite build && npm run build-types",
19
+ "build-types": "vue-tsc --emitDeclarationOnly -p ./tsconfig.types.json && api-extractor run && rimraf ./temp"
27
20
  },
28
21
  "devDependencies": {
29
- "@babel/core": "^7.14.6",
30
- "@babel/preset-env": "^7.14.7",
31
- "@babel/preset-typescript": "^7.14.5",
32
- "@rollup/plugin-alias": "^3.1.2",
33
- "@rollup/plugin-babel": "^5.3.0",
34
- "@rollup/plugin-commonjs": "^14.0.0",
35
- "@rollup/plugin-node-resolve": "^9.0.0",
36
- "@rollup/plugin-replace": "^2.4.2",
22
+ "@microsoft/api-extractor": "^7.33.7",
37
23
  "@types/jquery": "^3.5.14",
38
- "@vue/cli-plugin-babel": "^4.5.13",
39
- "@vue/cli-plugin-typescript": "^4.5.13",
40
- "@vue/cli-service": "^4.5.13",
41
- "@vue/compiler-sfc": "^3.0.11",
42
- "@zerollup/ts-transform-paths": "^1.7.18",
24
+ "@vitejs/plugin-vue": "^4.0.0",
43
25
  "cross-env": "^7.0.3",
44
- "minimist": "^1.2.5",
45
- "postcss": "^8.2.10",
26
+ "datatables.net-select-dt": "^1.5.0",
46
27
  "rimraf": "^3.0.2",
47
- "rollup": "^2.52.8",
48
- "rollup-plugin-postcss": "^4.0.0",
49
- "rollup-plugin-terser": "^7.0.2",
50
- "rollup-plugin-typescript2": "^0.30.0",
51
- "rollup-plugin-vue": "^6.0.0",
52
- "ttypescript": "^1.5.12",
53
- "typescript": "^4.0.3",
54
- "vue": "^3.0.5"
28
+ "typescript": "^4.8.4",
29
+ "vue": "^3.2.45",
30
+ "vue-tsc": "^1.0.16"
55
31
  },
56
32
  "peerDependencies": {
33
+ "datatables.net": "^1.13.1",
34
+ "jquery": "^3.6.0",
57
35
  "vue": "^3.0.5"
58
36
  },
59
37
  "engines": {
@@ -66,5 +44,8 @@
66
44
  "author": {
67
45
  "name": "SpryMedia Ltd",
68
46
  "url": "http://datatables.net"
47
+ },
48
+ "dependencies": {
49
+ "datatables.net-responsive-dt": "^2.4.0"
69
50
  }
70
51
  }
@@ -1,123 +1,145 @@
1
1
  <script lang="ts">
2
+ export let DataTablesLib: any
2
3
 
3
- import { defineComponent } from 'vue';
4
-
5
- // Load in DataTables based and jQuery
6
- import jQuery from 'jquery';
7
- import * as DataTables from 'datatables.net';
8
-
9
- export interface IData {
10
- _dt: null | any,
11
- oldData: Array<any>
12
- }
13
-
14
- // Register DataTables
15
- (DataTables.default as any)(window, jQuery);
16
-
17
- const Comp = defineComponent({
18
- name: 'DataTable',
19
- expose: [
20
- 'dt',
21
- ],
22
- data(): IData {
23
- return {
24
- _dt: null,
25
- oldData: [],
26
- };
27
- },
28
- computed: {},
29
- methods: {
30
- dt() {
31
- return this._dt;
32
- },
33
- saveOld(d: any) {
34
- this.oldData = d.value
35
- ? d.value.slice()
36
- : d.slice();
37
- }
38
- },
39
- mounted() {
40
- // Component shown so we can initialise DataTables of the table now
41
- let table = this.$el.querySelector('table');
42
- let options: any = this.options;
43
-
44
- if (this.data) {
45
- options.data = this.data;
46
- this.saveOld(options.data);
47
- }
48
-
49
- if (this.columns) {
50
- options.columns = this.columns;
4
+ export default {
5
+ name: 'Datatables.netVue',
6
+ inheritAttrs: false,
7
+ use(lib: any) {
8
+ DataTablesLib = lib;
51
9
  }
10
+ }
11
+ </script>
52
12
 
53
- if (this.ajax) {
54
- options.ajax = this.ajax;
55
- }
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, (event) => {
115
+ inst.emit(eName, { event: event, dt: dt });
116
+ });
117
+ }
118
+ }
119
+ });
56
120
 
57
- this._dt = jQuery(table).DataTable(options);
58
- },
59
- beforeUnmount() {
60
- this._dt.destroy(true);
61
- },
62
- props: {
63
- ajax: null,
64
- class: {
65
- type: String,
66
- default: '',
67
- },
68
- columns: {
69
- type: Array,
70
- default: null,
71
- },
72
- data: {
73
- type: Array,
74
- default: null,
75
- },
76
- options: {
77
- default: {}
78
- }
79
- },
80
- watch: {
81
- data: {
82
- handler(newVal) {
83
- let known = this._dt.data().toArray();
84
-
85
- // Find any new rows
86
- for (let n of newVal) {
87
- if (! known.includes(n)) {
88
- this._dt.row.add(n);
89
- }
90
- }
91
-
92
- // Remove any old rows
93
- for (let k of known) {
94
- if (! newVal.includes(k)) {
95
- this._dt.row((_idx: any, d: any) => d === k).remove();
96
- }
97
- }
98
-
99
- // Data in other rows might have changes, so we need to invalidate the rows
100
- this._dt.rows().invalidate().draw(false);
101
-
102
- this.saveOld(newVal);
103
- },
104
- deep: true
105
- }
106
- }
121
+ onBeforeUnmount(() => {
122
+ dt.value?.destroy(true);
107
123
  });
108
124
 
109
- // Expose a static method that can be used to add extensions
110
- Comp.use = function(lib: any) {
111
- lib(window, jQuery);
125
+ // methods
126
+ function saveOld(d: any) {
127
+ oldData.value = d.value ? d.value.slice() : d.slice();
112
128
  }
113
129
 
114
- export default Comp;
130
+ // expose
131
+ defineExpose({
132
+ /**
133
+ * DataTable instance
134
+ */
135
+ dt,
136
+ });
115
137
 
116
138
  </script>
117
139
 
118
140
  <template>
119
141
  <div class="datatable" v-once>
120
- <table :class="class">
142
+ <table ref="table" v-bind="$attrs" style="width:100%">
121
143
  <slot></slot>
122
144
  </table>
123
145
  </div>