|
@@ -2,9 +2,20 @@
|
|
|
export default {
|
|
|
data() {
|
|
|
return {
|
|
|
- activeName: 'first',
|
|
|
+ activeName: 'second',
|
|
|
activeName2: 'first',
|
|
|
- tabs: [{
|
|
|
+ editableTabsValue: '2',
|
|
|
+ editableTabsValue2: '2',
|
|
|
+ editableTabs: [{
|
|
|
+ title: 'Tab 1',
|
|
|
+ name: '1',
|
|
|
+ content: 'Tab 1 content'
|
|
|
+ }, {
|
|
|
+ title: 'Tab 2',
|
|
|
+ name: '2',
|
|
|
+ content: 'Tab 2 content'
|
|
|
+ }],
|
|
|
+ editableTabs2: [{
|
|
|
title: 'Tab 1',
|
|
|
name: '1',
|
|
|
content: 'Tab 1 content'
|
|
@@ -17,11 +28,62 @@
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
- handleRemove(tab) {
|
|
|
- console.log(tab);
|
|
|
- },
|
|
|
handleClick(tab, event) {
|
|
|
console.log(tab, event);
|
|
|
+ },
|
|
|
+ handleTabsEdit(targetName, action) {
|
|
|
+ if (action === 'add') {
|
|
|
+ let newTabName = ++this.tabIndex + '';
|
|
|
+ this.editableTabs.push({
|
|
|
+ title: 'New Tab',
|
|
|
+ name: newTabName,
|
|
|
+ content: 'New Tab content'
|
|
|
+ });
|
|
|
+ this.editableTabsValue = newTabName;
|
|
|
+ }
|
|
|
+ if (action === 'remove') {
|
|
|
+ let tabs = this.editableTabs;
|
|
|
+ let activeName = this.editableTabsValue;
|
|
|
+ if (activeName === targetName) {
|
|
|
+ tabs.forEach((tab, index) => {
|
|
|
+ if (tab.name === targetName) {
|
|
|
+ let nextTab = tabs[index + 1] || tabs[index - 1];
|
|
|
+ if (nextTab) {
|
|
|
+ activeName = nextTab.name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ this.editableTabsValue = activeName;
|
|
|
+ this.editableTabs = tabs.filter(tab => tab.name !== targetName);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ addTab(targetName) {
|
|
|
+ let newTabName = ++this.tabIndex + '';
|
|
|
+ this.editableTabs2.push({
|
|
|
+ title: 'New Tab',
|
|
|
+ name: newTabName,
|
|
|
+ content: 'New Tab content'
|
|
|
+ });
|
|
|
+ this.editableTabsValue2 = newTabName;
|
|
|
+ },
|
|
|
+ removeTab(targetName) {
|
|
|
+ let tabs = this.editableTabs2;
|
|
|
+ let activeName = this.editableTabsValue2;
|
|
|
+ if (activeName === targetName) {
|
|
|
+ tabs.forEach((tab, index) => {
|
|
|
+ if (tab.name === targetName) {
|
|
|
+ let nextTab = tabs[index + 1] || tabs[index - 1];
|
|
|
+ if (nextTab) {
|
|
|
+ activeName = nextTab.name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ this.editableTabsValue2 = activeName;
|
|
|
+ this.editableTabs2 = tabs.filter(tab => tab.name !== targetName);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -95,37 +157,6 @@ Tabs styled as cards.
|
|
|
```
|
|
|
:::
|
|
|
|
|
|
-### Closable
|
|
|
-
|
|
|
-Closable tabs.
|
|
|
-
|
|
|
-:::demo You can set the closable attribute in el-tabs to make all tabs closable. Also, closable can be set in a tab panel to make that specific tab closable.
|
|
|
-
|
|
|
-```html
|
|
|
-<template>
|
|
|
- <el-tabs type="card" :closable="true" @tab-click="handleClick" @tab-remove="handleRemove">
|
|
|
- <el-tab-pane label="User">User</el-tab-pane>
|
|
|
- <el-tab-pane label="Config">Config</el-tab-pane>
|
|
|
- <el-tab-pane label="Role">Role</el-tab-pane>
|
|
|
- <el-tab-pane label="Task">Task</el-tab-pane>
|
|
|
- </el-tabs>
|
|
|
-</template>
|
|
|
-<script>
|
|
|
- export default {
|
|
|
- methods: {
|
|
|
- handleRemove(tab) {
|
|
|
- console.log(tab);
|
|
|
- },
|
|
|
- handleClick(tab, event) {
|
|
|
- console.log(tab, event);
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-</script>
|
|
|
-```
|
|
|
-
|
|
|
-:::
|
|
|
-
|
|
|
### Border card
|
|
|
|
|
|
Border card tabs.
|
|
@@ -161,11 +192,151 @@ You can use named slot to customize the tab label content.
|
|
|
```
|
|
|
:::
|
|
|
|
|
|
+### Add & close tab
|
|
|
+
|
|
|
+Only card type Tabs support addable & closeable.
|
|
|
+
|
|
|
+:::demo
|
|
|
+```html
|
|
|
+<el-tabs v-model="editableTabsValue" type="card" editable @edit="handleTabsEdit">
|
|
|
+ <el-tab-pane
|
|
|
+ v-for="(item, index) in editableTabs"
|
|
|
+ :label="item.title"
|
|
|
+ :name="item.name"
|
|
|
+ >
|
|
|
+ {{item.content}}
|
|
|
+ </el-tab-pane>
|
|
|
+</el-tabs>
|
|
|
+<script>
|
|
|
+ export default {
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ editableTabsValue: '2',
|
|
|
+ editableTabs: [{
|
|
|
+ title: 'Tab 1',
|
|
|
+ name: '1',
|
|
|
+ content: 'Tab 1 content'
|
|
|
+ }, {
|
|
|
+ title: 'Tab 2',
|
|
|
+ name: '2',
|
|
|
+ content: 'Tab 2 content'
|
|
|
+ }],
|
|
|
+ tabIndex: 2
|
|
|
+ }
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ handleTabsEdit(targetName, action) {
|
|
|
+ if (action === 'add') {
|
|
|
+ let newTabName = ++this.tabIndex + '';
|
|
|
+ this.editableTabs.push({
|
|
|
+ title: 'New Tab',
|
|
|
+ name: newTabName,
|
|
|
+ content: 'New Tab content'
|
|
|
+ });
|
|
|
+ this.editableTabsValue = newTabName;
|
|
|
+ }
|
|
|
+ if (action === 'remove') {
|
|
|
+ let tabs = this.editableTabs;
|
|
|
+ let activeName = this.editableTabsValue;
|
|
|
+ if (activeName === targetName) {
|
|
|
+ tabs.forEach((tab, index) => {
|
|
|
+ if (tab.name === targetName) {
|
|
|
+ let nextTab = tabs[index + 1] || tabs[index - 1];
|
|
|
+ if (nextTab) {
|
|
|
+ activeName = nextTab.name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ this.editableTabsValue = activeName;
|
|
|
+ this.editableTabs = tabs.filter(tab => tab.name !== targetName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+</script>
|
|
|
+```
|
|
|
+:::
|
|
|
+
|
|
|
+### Customized trigger button of new tab
|
|
|
+
|
|
|
+:::demo
|
|
|
+```html
|
|
|
+<div style="margin-bottom: 20px;">
|
|
|
+ <el-button
|
|
|
+ size="small"
|
|
|
+ @click="addTab(editableTabsValue2)"
|
|
|
+ >
|
|
|
+ add tab
|
|
|
+ </el-button>
|
|
|
+</div>
|
|
|
+<el-tabs v-model="editableTabsValue2" type="card" closable @tab-remove="removeTab">
|
|
|
+ <el-tab-pane
|
|
|
+ v-for="(item, index) in editableTabs2"
|
|
|
+ :label="item.title"
|
|
|
+ :name="item.name"
|
|
|
+ >
|
|
|
+ {{item.content}}
|
|
|
+ </el-tab-pane>
|
|
|
+</el-tabs>
|
|
|
+<script>
|
|
|
+ export default {
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ editableTabsValue2: '2',
|
|
|
+ editableTabs2: [{
|
|
|
+ title: 'Tab 1',
|
|
|
+ name: '1',
|
|
|
+ content: 'Tab 1 content'
|
|
|
+ }, {
|
|
|
+ title: 'Tab 2',
|
|
|
+ name: '2',
|
|
|
+ content: 'Tab 2 content'
|
|
|
+ }],
|
|
|
+ tabIndex: 2
|
|
|
+ }
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ addTab(targetName) {
|
|
|
+ let newTabName = ++this.tabIndex + '';
|
|
|
+ this.editableTabs2.push({
|
|
|
+ title: 'New Tab',
|
|
|
+ name: newTabName,
|
|
|
+ content: 'New Tab content'
|
|
|
+ });
|
|
|
+ this.editableTabsValue2 = newTabName;
|
|
|
+ },
|
|
|
+ removeTab(targetName) {
|
|
|
+ let tabs = this.editableTabs2;
|
|
|
+ let activeName = this.editableTabsValue2;
|
|
|
+ if (activeName === targetName) {
|
|
|
+ tabs.forEach((tab, index) => {
|
|
|
+ if (tab.name === targetName) {
|
|
|
+ let nextTab = tabs[index + 1] || tabs[index - 1];
|
|
|
+ if (nextTab) {
|
|
|
+ activeName = nextTab.name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ this.editableTabsValue2 = activeName;
|
|
|
+ this.editableTabs2 = tabs.filter(tab => tab.name !== targetName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+</script>
|
|
|
+```
|
|
|
+:::
|
|
|
+
|
|
|
### Tabs Attributes
|
|
|
| Attribute | Description | Type | Accepted Values | Default |
|
|
|
|---------- |-------- |---------- |------------- |-------- |
|
|
|
| type | type of Tab | string | card/border-card | — |
|
|
|
| closable | whether Tab is closable | boolean | — | false |
|
|
|
+| addable | whether Tab is addable | boolean | — | false |
|
|
|
+| editable | whether Tab is addable and closable | boolean | — | false |
|
|
|
| active-name(deprecated) | name of the selected tab | string | — | name of first tab |
|
|
|
| value | name of the selected tab | string | — | name of first tab |
|
|
|
|
|
@@ -173,7 +344,9 @@ You can use named slot to customize the tab label content.
|
|
|
| Event Name | Description | Parameters |
|
|
|
|---------- |-------- |---------- |
|
|
|
| tab-click | triggers when a tab is clicked | clicked tab |
|
|
|
-| tab-remove | triggers when a tab is removed | removed tab |
|
|
|
+| tab-remove | triggers when tab-remove button is clicked | name of the removed tab |
|
|
|
+| tab-add | triggers when tab-add button is clicked | — |
|
|
|
+| edit | triggers when tab-add button or tab-remove is clicked | (targetName, action) |
|
|
|
|
|
|
### Tab-pane Attributes
|
|
|
| Attribute | Description | Type | Accepted Values | Default |
|