vue中下拉框组件的封装

原理

vue element中,需要封装一个对应的下拉款组件。
第一步:在api_domain.js中添加后台的请求接口

//获取下拉框的接口 从redis中
  domainGetDomainKeyRedis: params => { 
	  return axios.post('domain-manager/domain/getDomainKeyRedis',qs.stringify(params));
},

//获取下拉框的接口 从DB中
  domainGetDomainKeyDB: params => { 
	  return axios.post('domain-manager/domain/getDomainKeyDB',qs.stringify(params));
},

第二步,在文件夹api中新建getSelect.js,内容


/**
 * _this为传过来的this
 * 
 * 根据参数str,去获取到对应的下拉框的值
 * 
 * paramName,接收的数组,如'type'
 * 
 * 先去redis总查询,如果没有值,再去数据库中查询
 */
import api from '@/api/api_domain'


export function GetSelect(_this,str,paramName) {
	
    let para = {
		key: str
	};
	
	if(typeof str === "undefined" || str == ""){
	//	return options;
	}else{
		_this.$api.domain.domainGetDomainKeyRedis(para).then((res) => {
																									
			_this[paramName] = res.data.data.listDomainDefine;
												
		}).catch((err)=>{
	    console.log(err);
	  });
	}

}


使用

引入js

import {GetSelect} from '@/api/getSelect'

取值

//获取资源类型
GetSelect(this,'resType','type');

resType,是传递到后台方法的查询条件,
在这里插入图片描述
就是在【域定义管理】中简称,点击【域值】按钮可看到对应的下拉框数据
在这里插入图片描述
type,是接受查询出的list的参数,在页面中我定义了type: [],用来接收,资源类型下拉框中的值

在页面中

<el-form-item label="类型" prop="resType">	
	<el-select v-model="addForm.resType" filterable placeholder="请选择" style="width:100%">
		<el-option v-for="item in type" :key="item.key " :label="item.name" :value="item.key">
		</el-option>
	</el-select>					
</el-form-item>

在这里插入图片描述
在table中怎么去显示类型为中文名称

<el-table-column prop="ptType" label="类型" min-width="10%" :formatter="formatType" >
			</el-table-column>

注意: :formatter=“formatType”

然后写一个方法formatType

formatType: function (row, column) {//类型转换
				
				for(var a = 0 ;a< this.options.length ; a++){
					if(row.ptType == this.options[a].key){
						return this.options[a].name;
					}
				}
				
			},
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Vue 下拉框组件封装及引用代码: ```vue <template> <div class="select-container"> <div class="select-header" @click="showOptions = !showOptions"> {{ selectedOption || placeholder }} <i v-if="showOptions" class="fa fa-sort-up"></i> <i v-else class="fa fa-sort-down"></i> </div> <div class="select-options" v-show="showOptions"> <div v-for="(option, index) in options" :key="index" @click="selectOption(option)"> {{ option }} </div> </div> </div> </template> <script> export default { props: { options: { type: Array, default: () => [] }, placeholder: { type: String, default: "请选择" } }, data() { return { selectedOption: "", showOptions: false }; }, methods: { selectOption(option) { this.selectedOption = option; this.showOptions = false; this.$emit("option-selected", option); } } }; </script> ``` 这个组件的使用方法如下: ```vue <template> <div> <SelectBox :options="options" placeholder="请选择" @option-selected="onOptionSelected"/> </div> </template> <script> import SelectBox from "@/components/SelectBox.vue"; export default { components: { SelectBox }, data() { return { options: ["Option1", "Option2", "Option3"] }; }, methods: { onOptionSelected(option) { console.log(`Selected option: ${option}`); } } }; </script> ``` 在这个例子,我们向 `SelectBox` 组件传递了一个 `options` 属性,这是一个包含下拉框选项的数组。我们还传递了一个 `placeholder` 属性,以指定下拉框未选择选项时显示的文本。`SelectBox` 组件使用 `v-for` 指令为每个选项生成一个 `div` 元素,并在用户选择一个选项时发出一个 `option-selected` 事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值