From 584fa7777e620209a78e5fb1b12fab86ac93ff70 Mon Sep 17 00:00:00 2001 From: Jim Gillett Date: Mon, 8 Jan 2018 17:11:43 +0000 Subject: [PATCH 1/4] [Feature] Add option to only select one row at a time in data tables --- src/mixins/data-iterable.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/mixins/data-iterable.js b/src/mixins/data-iterable.js index 8621b9c5c91..8d5e82c702d 100644 --- a/src/mixins/data-iterable.js +++ b/src/mixins/data-iterable.js @@ -73,6 +73,7 @@ export default { default: 'Items per page:' }, selectAll: [Boolean, String], + radioSelect: [Boolean], search: { required: false }, @@ -275,7 +276,7 @@ export default { ) return this.hideActions && - !this.hasPagination + !this.hasPagination ? items : items.slice(this.pageStart, this.pageStop) }, @@ -316,8 +317,15 @@ export default { } let selected = this.value.slice() - if (value) selected.push(item) - else selected = selected.filter(i => i[keyProp] !== itemKey) + if (value) { + if (this.radioSelect) { + selected.length = 0 + } + selected.push(item) + } + else { + selected = selected.filter(i => i[keyProp] !== itemKey) + } this.$emit('input', selected) } }) From bfb3d0e1cc97d02bcc8fa35a94d580c60c423e8a Mon Sep 17 00:00:00 2001 From: Jim Gillett Date: Tue, 9 Jan 2018 16:20:53 +0000 Subject: [PATCH 2/4] Change name of prop to singleSelect and made changes requested in PR --- src/mixins/data-iterable.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mixins/data-iterable.js b/src/mixins/data-iterable.js index 8d5e82c702d..7c5192565df 100644 --- a/src/mixins/data-iterable.js +++ b/src/mixins/data-iterable.js @@ -73,7 +73,7 @@ export default { default: 'Items per page:' }, selectAll: [Boolean, String], - radioSelect: [Boolean], + singleSelect: [Boolean], search: { required: false }, @@ -276,7 +276,7 @@ export default { ) return this.hideActions && - !this.hasPagination + !this.hasPagination ? items : items.slice(this.pageStart, this.pageStop) }, @@ -318,10 +318,11 @@ export default { let selected = this.value.slice() if (value) { - if (this.radioSelect) { - selected.length = 0 + if (this.singleSelect) { + selected = [item] + } else { + selected.push(item) } - selected.push(item) } else { selected = selected.filter(i => i[keyProp] !== itemKey) From 4b0381ae1a94288e9746e245b9b3ae034f0c5793 Mon Sep 17 00:00:00 2001 From: Jim Gillett Date: Tue, 9 Jan 2018 17:17:58 +0000 Subject: [PATCH 3/4] Refactor to bring function size under 40 lines --- src/mixins/data-iterable.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/mixins/data-iterable.js b/src/mixins/data-iterable.js index 7c5192565df..af45501b7cb 100644 --- a/src/mixins/data-iterable.js +++ b/src/mixins/data-iterable.js @@ -305,10 +305,16 @@ export default { ) }, createProps (item, index) { - const props = { item, index } + let props = { item, index } const keyProp = this.itemKey const itemKey = item[keyProp] + props = this.createSelectedProp(item, props, keyProp, itemKey) + props = this.createExpandedProp(item, props, keyProp, itemKey) + + return props + }, + createSelectedProp (item, props, keyProp, itemKey) { Object.defineProperty(props, 'selected', { get: () => this.selected[item[this.itemKey]], set: (value) => { @@ -331,6 +337,9 @@ export default { } }) + return props + }, + createExpandedProp (item, props, keyProp, itemKey) { Object.defineProperty(props, 'expanded', { get: () => this.expanded[item[this.itemKey]], set: (value) => { From 9ff9826d5854d6b5e49170af11985fed05975857 Mon Sep 17 00:00:00 2001 From: Jim Gillett Date: Wed, 10 Jan 2018 15:51:07 +0000 Subject: [PATCH 4/4] Reduce function arguments --- src/mixins/data-iterable.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/mixins/data-iterable.js b/src/mixins/data-iterable.js index af45501b7cb..e5064348b1b 100644 --- a/src/mixins/data-iterable.js +++ b/src/mixins/data-iterable.js @@ -306,15 +306,16 @@ export default { }, createProps (item, index) { let props = { item, index } - const keyProp = this.itemKey - const itemKey = item[keyProp] - props = this.createSelectedProp(item, props, keyProp, itemKey) - props = this.createExpandedProp(item, props, keyProp, itemKey) + props = this.createSelectedProp(item, props) + props = this.createExpandedProp(item, props) return props }, - createSelectedProp (item, props, keyProp, itemKey) { + createSelectedProp (item, props) { + const keyProp = this.itemKey + const itemKey = item[keyProp] + Object.defineProperty(props, 'selected', { get: () => this.selected[item[this.itemKey]], set: (value) => { @@ -339,7 +340,10 @@ export default { return props }, - createExpandedProp (item, props, keyProp, itemKey) { + createExpandedProp (item, props) { + const keyProp = this.itemKey + const itemKey = item[keyProp] + Object.defineProperty(props, 'expanded', { get: () => this.expanded[item[this.itemKey]], set: (value) => {