2016-11-13 03:58:45 +00:00
< script >
export default {
data() {
return {
2017-01-12 14:46:26 +00:00
activeName: 'second',
2016-12-21 16:02:51 +00:00
activeName2: 'first',
2017-01-12 14:46:26 +00:00
editableTabsValue: '2',
editableTabsValue2: '2',
editableTabs: [{
title: 'Tab 1',
name: '1',
content: 'Tab 1 content'
}, {
title: 'Tab 2',
name: '2',
content: 'Tab 2 content'
}],
editableTabs2: [{
2016-12-21 16:02:51 +00:00
title: 'Tab 1',
name: '1',
content: 'Tab 1 content'
}, {
title: 'Tab 2',
name: '2',
content: 'Tab 2 content'
}],
tabIndex: 2
2016-11-13 03:58:45 +00:00
}
},
methods: {
handleClick(tab, event) {
console.log(tab, event);
2017-01-12 14:46:26 +00:00
},
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);
2016-11-13 03:58:45 +00:00
}
}
}
< / script >
2016-11-10 13:46:55 +00:00
## Tabs
2016-11-13 03:58:45 +00:00
Divide data collections which are related yet belong to different types.
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
### Basic usage
2016-11-10 13:46:55 +00:00
Basic and concise tabs.
2016-12-21 16:02:51 +00:00
:::demo Tabs provide a selective card functionality. By default the first tab is selected as active, and you can activate any tab by setting the `value` attribute.
2016-11-10 13:46:55 +00:00
```html
< template >
2016-12-21 16:02:51 +00:00
< el-tabs v-model = "activeName" @tab -click = "handleClick" >
2016-11-16 08:14:48 +00:00
< el-tab-pane label = "User" name = "first" > User< / el-tab-pane >
< el-tab-pane label = "Config" name = "second" > Config< / el-tab-pane >
< el-tab-pane label = "Role" name = "third" > Role< / el-tab-pane >
< el-tab-pane label = "Task" name = "fourth" > Task< / el-tab-pane >
< / el-tabs >
2016-11-10 13:46:55 +00:00
< / template >
< script >
export default {
data() {
return {
2016-11-16 08:14:48 +00:00
activeName: 'first'
2016-11-10 13:46:55 +00:00
};
2016-12-21 16:02:51 +00:00
},
methods: {
handleClick(tab, event) {
console.log(tab, event);
}
2016-11-10 13:46:55 +00:00
}
};
< / script >
```
:::
### Card Style
2016-11-13 03:58:45 +00:00
Tabs styled as cards.
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
:::demo Set `type` to `card` can get a card-styled tab.
2016-11-10 13:46:55 +00:00
```html
< template >
2016-12-21 16:02:51 +00:00
< el-tabs type = "card" @tab -click = "handleClick" >
2016-11-16 08:14:48 +00:00
< 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 >
2016-11-10 13:46:55 +00:00
< / el-tabs >
< / template >
< script >
export default {
2016-12-21 16:02:51 +00:00
data() {
return {
activeName: 'first'
};
},
2016-11-10 13:46:55 +00:00
methods: {
2016-11-13 03:58:45 +00:00
handleClick(tab, event) {
console.log(tab, event);
2016-11-10 13:46:55 +00:00
}
}
};
< / script >
```
:::
### Border card
Border card tabs.
:::demo Set `type` to `border-card` .
```html
< el-tabs type = "border-card" >
2016-11-16 08:14:48 +00:00
< 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 >
2016-11-10 13:46:55 +00:00
< / el-tabs >
```
:::
2016-12-22 06:03:03 +00:00
### Custom Tab
2016-12-29 08:19:12 +00:00
You can use named slot to customize the tab label content.
2016-12-22 06:03:03 +00:00
2016-12-29 08:19:12 +00:00
:::demo
2016-12-22 06:03:03 +00:00
```html
< el-tabs type = "border-card" >
2016-12-29 08:19:12 +00:00
< el-tab-pane >
< span slot = "label" > < i class = "el-icon-date" > < / i > Route< / span >
Route
< / el-tab-pane >
2016-12-22 06:03:03 +00:00
< 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 >
```
:::
2017-01-12 14:46:26 +00:00
### 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 >
```
:::
2016-11-10 13:46:55 +00:00
### Tabs Attributes
2016-11-13 03:58:45 +00:00
| Attribute | Description | Type | Accepted Values | Default |
2016-11-10 13:46:55 +00:00
|---------- |-------- |---------- |------------- |-------- |
2016-11-13 03:58:45 +00:00
| type | type of Tab | string | card/border-card | — |
| closable | whether Tab is closable | boolean | — | false |
2017-01-12 14:46:26 +00:00
| addable | whether Tab is addable | boolean | — | false |
| editable | whether Tab is addable and closable | boolean | — | false |
2016-12-21 16:02:51 +00:00
| active-name(deprecated) | name of the selected tab | string | — | name of first tab |
| value | name of the selected tab | string | — | name of first tab |
2016-11-10 13:46:55 +00:00
### Tabs Events
2016-11-13 03:58:45 +00:00
| Event Name | Description | Parameters |
2016-11-10 13:46:55 +00:00
|---------- |-------- |---------- |
2016-11-13 03:58:45 +00:00
| tab-click | triggers when a tab is clicked | clicked tab |
2017-01-12 14:46:26 +00:00
| 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) |
2016-11-10 13:46:55 +00:00
### Tab-pane Attributes
2016-11-13 03:58:45 +00:00
| Attribute | Description | Type | Accepted Values | Default |
2016-11-10 13:46:55 +00:00
|---------- |-------- |---------- |------------- |-------- |
2016-11-13 03:58:45 +00:00
| label | title of the tab | string | — | — |
2016-12-29 12:53:33 +00:00
| disabled | whether Tab is disabled | boolean | — | false |
2016-11-13 03:58:45 +00:00
| name | identifier corresponding to the activeName of Tabs, representing the alias of the tab-pane | string | — | ordinal number of the tab-pane in the sequence, i.e. the first tab-pane is '1' |
2016-12-21 16:02:51 +00:00
| closable | whether Tab is closable | boolean | — | false |