需求
需要一个iput框输入,离焦或回车后生成一个标签,提交时判断符不符合规则
思路
input框的值不能为数组,所以用一个div设置样式模拟输入框,隐藏输入框的样式;前半段循环显示tag, 利用@keyup.enter=”addTags” (回车)和@blur=”addTags”(离焦)添加tag, 规则判断需要用到循环;
缺点
在输入值时未验证,需改进
代码
<template>
<div style="width:800px">
<el-form ref="form" :model="form" label-width="120px" :rules="rule">
<el-form-item label="白名单" prop="allowedHosts">
<div class="contaion" @click="onclick">
<!-- 生成的标签 -->
<div v-for="(item, index) in form.allowedHosts" :key="index" class="spanbox">
<span class="tagspan">{{ item }}</span>
<i class="iClose" @click="removeTag(item,index)"></i>
</div>
<!-- 输入框 -->
<input
v-model="form.inputValue"
@keyup.enter="addTags"
@blur="addTags"
:style="inputStyle"
class="inputTag"
ref="inputTag"
type="text"
/>
</div>
</el-form-item>
<el-form-item>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">保存</el-button>
<el-button>取消</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
props:{
allowedHosts:{
type:Object,
}
},
data(){
var validIp = (rule, value, callback) => {
var ipreg = /((2(5[0-5]|[0-4]d))|[0-1]?d{1,2})(.((2(5[0-5]|[0-4]d))|[0-1]?d{1,2})){3}/;
if (value.length > 0) {
let flag = true;
value.forEach(item => {
if (item != "*" && !ipreg.test(item) ) {
flag = false;
}
})
if (!flag) {
callback(new Error("白名单中含有错误的IP地址,请重新输入"));
} else {
callback();
}
}else{
callback(new Error("请输入白名单"));
}
};
return{
form:{
allowedHosts:[], //tag
inputValue: "", //输入框
},
//输入框宽度
inputLength: "",
//计算删除位置
n: 0,
rule:{
allowedHosts:[{required:true,message:'请输入需要加入白名单的主机',trigger:'change'},
{ required: true, validator: validIp, trigger: "change" }],
}
}
},
watch: {
//监听输入的值越多,输入框越长
"form.inputValue"(val) {
// 实时改变input输入框宽度,防止输入内容超出input默认宽度显示不全
this.inputLength = this.$refs.inputTag.value.length * 12 + 50;
},
},
computed: {
//计算属性:计算出动态输入框宽度
inputStyle() {
let style = {};
style.width = `${this.inputLength}px`;
return style;
}
},
methods:{
//点击叉叉删除tag
removeTag(item,index) {
this.form.allowedHosts.splice(index, 1);
},
//回车增加tag
addTags() {
//新增函数中可以加一些你所需要的校验规则。比如只能是数子,或者不能输入‘,’等
if (this.form.inputValue) {
//添加tag
this.form.allowedHosts.push(this.form.inputValue);
//清空输入框
this.form.inputValue = "";
}
},
//点击父盒子输入框获取焦点
onclick() {
this.$nextTick(() => {
this.$refs.inputTag.focus();
})
}
}
}
</script>
<style scoped>
.contaion {
width: 600px;
box-sizing: border-box;
background-color: white;
border: 1px solid #409EFF;
border-radius: 4px;
font-size: 12px;
text-align: left;
padding-left: 5px;
word-wrap: break-word;
overflow: hidden;
}
/* 标签 */
.spanbox {
display: inline-block;
font-size: 14px;
margin: 3px 4px 3px 0;
background-color: #ecf5ff;
border: 1px solid #e8eaec;
border-radius: 3px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
}
/* 标签文字 */
.tagspan {
height: 24px;
line-height: 22px;
max-width: 99%;
position: relative;
display: inline-block;
padding-left: 8px;
color: #409EFF;
font-size: 14px;
opacity: 1;
vertical-align: middle;
overflow: hidden;
transition: 0.25s linear;
}
/* tag的叉叉 */
.iClose {
padding: 0 6px 0 4px;
opacity: 1;
-webkit-filter: none;
filter: none;
color: #409EFF;
/* font-weight: 600; */
cursor:pointer;
}
/* 鼠标经过叉叉 */
.iClose:hover{
background-color: #409EFF;
border-radius: 50%;
color: #fff;
}
.iClose:after {
content: "0D7";
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
/* line-height: 27px; */
transition: 0.3s, color 0s;
}
/* input */
.inputTag {
font-size: 16px;
border: none;
box-shadow: none;
outline: none;
background-color: transparent;
padding: 0;
width: auto;
min-width: 150px;
vertical-align: top;
height: 32px;
color: #495060;
line-height: 32px;
}
/* 输入框提示文字大小 */
input:placeholder-shown {
font-size: 10px;
}
</style>
结果
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...