From 9900963b03b302ff179d21e3d97a3e805c228980 Mon Sep 17 00:00:00 2001 From: cagataycivici Date: Thu, 23 May 2019 16:52:22 +0300 Subject: [PATCH] Two-way binding support for Tabview --- src/components/tabview/TabView.vue | 4 +- src/views/tabview/TabViewDemo.vue | 30 ++++--- src/views/tabview/TabViewDoc.vue | 123 ++++++++++++++++++----------- 3 files changed, 99 insertions(+), 58 deletions(-) diff --git a/src/components/tabview/TabView.vue b/src/components/tabview/TabView.vue index 43de12087..a7daf4610 100644 --- a/src/components/tabview/TabView.vue +++ b/src/components/tabview/TabView.vue @@ -26,7 +26,9 @@ export default { }, activateTab(tab) { for (let i = 0; i < this.tabs.length; i++) { - this.tabs[i].d_active = this.tabs[i] === tab; + let active = this.tabs[i] === tab; + this.tabs[i].d_active = active; + this.tabs[i].$emit('update:active', active); } }, onTabKeydown(event, tab) { diff --git a/src/views/tabview/TabViewDemo.vue b/src/views/tabview/TabViewDemo.vue index 236aebfa5..b152c7517 100644 --- a/src/views/tabview/TabViewDemo.vue +++ b/src/views/tabview/TabViewDemo.vue @@ -29,29 +29,30 @@

Programmatic

-
- + The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, but does not intend to become part of his father's business. Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family. - + Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, TheGodfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall, deepening The Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy. - + The Godfather Part III is set in 1979 and 1980. Michael has moved back to New York and taken great strides to remove the family from crime. He turns over his New York criminal interests to longtime enforcer Joey Zasa. He uses his wealth in an attempt to rehabilitate his reputation through numerous philanthropic acts, administered by a foundation named after his father. A decade earlier, he gave custody of his two children to Kay, who has since remarried. -

Disabled

+

Disabled

The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, @@ -114,15 +115,16 @@ import TabViewDoc from './TabViewDoc'; export default { data() { return { - activeIndex: 0 + active: [true, false, false] } }, methods: { - next() { - this.activeIndex = this.activeIndex === 2 ? 0 : this.activeIndex + 1; - }, - prev() { - this.activeIndex = this.activeIndex === 0 ? 2 : this.activeIndex - 1; + activate(index) { + let activeArray = [...this.active]; + for (let i = 0 ; i < activeArray.length; i++) { + activeArray[i] = (i === index); + } + this.active = activeArray; } }, components: { @@ -141,4 +143,8 @@ export default { margin: 0 .5em; } } + +.p-button { + margin-right: .25em; +} \ No newline at end of file diff --git a/src/views/tabview/TabViewDoc.vue b/src/views/tabview/TabViewDoc.vue index 94c07b600..01ebe6fad 100644 --- a/src/views/tabview/TabViewDoc.vue +++ b/src/views/tabview/TabViewDoc.vue @@ -23,43 +23,35 @@ import {TabView, TabPanel} from 'primevue/tabview'; </TabView> -

Programmatic Control

-

Tabs can be controlled programmatically using active property that defines the active tab.

+

Active

+

Visibility of the content is specified with the active property that supports one or two-way binding.

-<div> - <Button icon="pi pi-chevron-left" @click="prev"/> - <Button icon="pi pi-chevron-right" @click="next"/> -</div> - <TabView> - <TabPanel header="Header I" :active="activeIndex === 0"> + <TabPanel header="Header I"> Content I </TabPanel> - <TabPanel header="Header II" :active="activeIndex === 1"> + <TabPanel header="Header II" :active="true"> Content II </TabPanel> - <TabPanel header="Header III" :active="activeIndex === 2"> + <TabPanel header="Header III"> Content III </TabPanel> </TabView> - -export default { - data() { - return { - activeIndex: 0 - } - }, - methods: { - next() { - this.activeIndex = this.activeIndex === 2 ? 0 : this.activeIndex + 1; - }, - prev() { - this.activeIndex = this.activeIndex === 0 ? 2 : this.activeIndex - 1; - } - } -} +

Two-way binding requires the sync operator.

+ +<TabView> + <TabPanel header="Header I" :active.sync="active1"> + Content I + </TabPanel> + <TabPanel header="Header II" :active.sync="active2"> + Content II + </TabPanel> + <TabPanel header="Header III" :active.sync="active3"> + Content III + </TabPanel> +</TabView>

Disabled

@@ -97,6 +89,46 @@ export default { Content II </TabPanel> </TabView> +
+ +

Programmatic Control

+

Tabs can be controlled programmatically using active property that defines the active tab.

+ +<Button @click="activate(0)" class="p-button-secondary" label="Activate 1st" /> +<Button @click="activate(1)" class="p-button-secondary" label="Activate 2st" /> +<Button @click="activate(2)" class="p-button-secondary" label="Activate 3st" /> + +<TabView> + <TabPanel header="Header I" :active.sync="active[0]"> + Content I + </TabPanel> + <TabPanel header="Header II" :active.sync="active[1]"> + Content II + </TabPanel> + <TabPanel header="Header III" :active.sync="active[2]"> + Content III + </TabPanel> +</TabView> + + + + +export default { + data() { + return { + active: [true, false, false] + } + }, + methods: { + activate(index) { + let activeArray = [...this.active]; + for (let i = 0 ; i < activeArray.length; i++) { + activeArray[i] = (i === index); + } + this.active = activeArray; + } + }, +}

Properties of TabPanel

@@ -234,22 +266,23 @@ export default { <h3>Programmatic</h3> <div style="padding: .5em 0"> - <Button icon="pi pi-chevron-left" @click="prev" class="p-button-secondary" /> - <Button icon="pi pi-chevron-right" @click="next" style="margin-left: .5em" class="p-button-secondary"/> + <Button @click="activate(0)" class="p-button-secondary" label="Activate 1st" /> + <Button @click="activate(1)" class="p-button-secondary" label="Activate 2st" /> + <Button @click="activate(2)" class="p-button-secondary" label="Activate 3st" /> </div> <TabView> - <TabPanel header="Godfather I" :active="activeIndex === 0"> + <TabPanel header="Godfather I" :active.sync="active[0]"> The story begins as Don Vito Corleone, the head of a New York Mafia family, overseeshis daughter's wedding. His beloved son ichael has just come home from the war, but does not intend to become part of his father's business. Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family. </TabPanel> - <TabPanel header="Godfather II" :active="activeIndex === 1"> + <TabPanel header="Godfather II" :active.sync="active[1]"> Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, TheGodfather parallels the young Vito Corleone's rise with his son Michael's spiritual fall, deepening The Godfather's depiction of the dark side of the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy. </TabPanel> - <TabPanel header="Godfather III" :active="activeIndex === 2"> + <TabPanel header="Godfather III" :active.sync="active[2]"> The Godfather Part III is set in 1979 and 1980. Michael has moved back to New York and taken great strides to remove the family from crime. He turns over his New York criminal interests to longtime enforcer Joey Zasa. He uses his wealth in an attempt to rehabilitate his reputation through numerous philanthropic acts, administered by a foundation named after his father. A decade earlier, he gave custody of his two children to Kay, who has since remarried. @@ -315,20 +348,20 @@ export default { export default { - data() { - return { - activeIndex: 0 - } - }, - methods: { - next() { - this.activeIndex = this.activeIndex === 2 ? 0 : this.activeIndex + 1; - }, - prev() { - this.activeIndex = this.activeIndex === 0 ? 2 : this.activeIndex - 1; - } - } -} + data() { + return { + active: [true, false, false] + } + }, + methods: { + activate(index) { + let activeArray = [...this.active]; + for (let i = 0 ; i < activeArray.length; i++) { + activeArray[i] = (i === index); + } + this.active = activeArray; + } + }