From 6730f2c48640a7c3e67be840cab1c6859d7b5038 Mon Sep 17 00:00:00 2001 From: Cagatay Civici Date: Wed, 9 Dec 2020 16:38:24 +0300 Subject: [PATCH] Fixed #749 - DataTable throws error when there are no columns --- src/components/datatable/DataTable.vue | 51 +++++++++++++++--------- src/components/datatable/TableBody.vue | 11 +++-- src/components/datatable/TableFooter.vue | 2 +- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/components/datatable/DataTable.vue b/src/components/datatable/DataTable.vue index 1a4d08289..45e0a4c61 100755 --- a/src/components/datatable/DataTable.vue +++ b/src/components/datatable/DataTable.vue @@ -1644,6 +1644,9 @@ export default { }, hasGlobalFilter() { return this.filters && Object.prototype.hasOwnProperty.call(this.filters, 'global'); + }, + getChildren() { + return this.$slots.default ? this.$slots.default() : null; } }, computed: { @@ -1662,7 +1665,11 @@ export default { }, columns() { let cols = []; - let children = this.$slots.default(); + let children = this.getChildren(); + + if (!children) { + return; + } children.forEach(child => { if (child.dynamicChildren) @@ -1713,42 +1720,50 @@ export default { return this.frozenColumns.length > 0; }, headerColumnGroup() { - const children = this.$slots.default(); - for (let child of children) { - if (child.type.name === 'columngroup' && child.props?.type === 'header') { - return child; + const children = this.getChildren(); + if (children) { + for (let child of children) { + if (child.type.name === 'columngroup' && child.props?.type === 'header') { + return child; + } } } return null; }, frozenHeaderColumnGroup() { - const children = this.$slots.default(); - for (let child of children) { - if (child.type.name === 'columngroup' && child.props?.type === 'frozenheader') { - return child; + const children = this.getChildren(); + if (children) { + for (let child of children) { + if (child.type.name === 'columngroup' && child.props?.type === 'frozenheader') { + return child; + } } } return null; }, footerColumnGroup() { - const children = this.$slots.default(); - for (let child of children) { - if (child.type.name === 'columngroup' && child.props?.type === 'footer') { - return child; + const children = this.getChildren(); + if (children) { + for (let child of children) { + if (child.type.name === 'columngroup' && child.props?.type === 'footer') { + return child; + } } } return null; }, frozenFooterColumnGroup() { - const children = this.$slots.default(); - for (let child of children) { - if (child.type.name === 'columngroup' && child.props?.type === 'frozenfooter') { - return child; + const children = this.getChildren(); + if (children) { + for (let child of children) { + if (child.type.name === 'columngroup' && child.props?.type === 'frozenfooter') { + return child; + } } - } + } return null; }, diff --git a/src/components/datatable/TableBody.vue b/src/components/datatable/TableBody.vue index 0dda3a09f..cc73bd744 100755 --- a/src/components/datatable/TableBody.vue +++ b/src/components/datatable/TableBody.vue @@ -3,7 +3,7 @@ - + @@ -406,6 +406,11 @@ export default { this.$emit('row-edit-cancel', event); } }, + computed: { + columnsLength() { + return this.columns ? this.columns.length : 0; + } + }, components: { 'DTBodyCell': BodyCell } diff --git a/src/components/datatable/TableFooter.vue b/src/components/datatable/TableFooter.vue index 79c264e14..2ca780f7f 100755 --- a/src/components/datatable/TableFooter.vue +++ b/src/components/datatable/TableFooter.vue @@ -38,7 +38,7 @@ export default { if (this.columnGroup) { hasFooter = true; } - else { + else if (this.columns) { for (let col of this.columns) { if (col.props?.footer || (col.children && col.children.footer)) { hasFooter = true;