element ui Select选择器 下拉框某条数据设置显示星星* 有几种方法实现
时间: 2024-02-25 09:51:59 浏览: 115
除了在 `el-option` 中使用 `slot` 来自定义下拉框选项的显示内容之外,还有其他几种方法可以实现在下拉框某条数据中显示星号:
1. 在 `el-option` 中使用 `template` 来自定义下拉框选项的显示内容:
```html
<el-select v-model="value">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
<template v-if="item.showStar">
<span>{{ item.label }}*</span>
</template>
<template v-else>
<span>{{ item.label }}</span>
</template>
</el-option>
</el-select>
```
在上面的代码中,我们在 `el-option` 中使用了 `template` 来根据 `item.showStar` 的值来显示不同的内容。
2. 使用 `formatter` 属性来自定义下拉框选项的显示内容:
```html
<el-select v-model="value" :formatter="formatter">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
```
```javascript
methods: {
formatter(row) {
if (row.showStar) {
return `${row.label}*`
} else {
return row.label
}
}
}
```
在上面的代码中,我们使用了 `formatter` 属性来自定义下拉框选项的显示内容,`formatter` 方法会接收一个参数 `row`,表示当前选项的数据对象,我们可以根据 `row.showStar` 的值来显示不同的内容。
3. 使用插槽 `dropdown-item` 来自定义下拉框选项的显示内容:
```html
<el-select v-model="value">
<template v-for="item in options">
<el-option
:key="item.value"
:label="item.label"
:value="item.value"
>
<template v-slot:dropdown-item>
<span>{{ item.label }}</span>
<span v-if="item.showStar">*</span>
</template>
</el-option>
</template>
</el-select>
```
在上面的代码中,我们使用了插槽 `dropdown-item` 来自定义下拉框选项的显示内容,如果 `item.showStar` 的值为 `true`,则会在显示内容后面添加一个星号。
阅读全文
相关推荐















