Two or three things about how tag in Vue Elementui interacts with other elements of the page

Source: Internet
Author: User

Objective

Company system in the background with ELEMENTUI development, inevitably encountered some need to go according to the original function, plus some interactive functions.
Today to introduce me in the ELEMENTUI tag tag and multi-box interaction process, things sound very simple, but the more simple things are more prone to some problems. Official tag document: Elementui-tag tag

:

Front-end Boutique tutorial: Baidu Network Disk download

Ideas

One, multi-box check, the corresponding tag appears:

1. Use Watch to monitor the change of the value A (array) bound by the multi-box;
2. According to the change of a, the loop gets the name corresponding to the ID of the check box, and the ID and the corresponding name make up the new object array;
3. An array of objects obtained in the previous step, the deduplication (product requirements, the occurrence of the tag can not be duplicated) to obtain the result B;
4. Assign the B value to tags and show it in a loop;

Second, click on the tag on the delete button, delete the current tag, and the corresponding check box to uncheck the check:

1. Click the tag to delete the button, get the current tag ID C;
2. Implement the method to remove C from A;
3.watch Event re-entry to the first step of the method;

Summary: Listen to the multi-box corresponding to the model A, according to a change, take the corresponding ID and name, assign a value to tag to display, tag delete event in turn to control a change, re-enter the Watch event method

Sounds very simple, the idea is probably also clear, first of all the above-mentioned ideas corresponding to the code, and then talk about the problems encountered, pits

Code

Front-end Boutique tutorial: Baidu Network Disk download

Copy the whole piece of code into your Elementui project and you'll see the effect
.
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134 <template> <div> <el-row type="flex" justify="bettwen">  <el-col :span="15">  <!-- 表单 -->  <el-form :model="tempForm" ref="tempForms">   <el-form-item label="请选择人员">   <!-- 多选人员 -->   <el-checkbox-group v-model="tempForm.checkboxGroup5" size="small">    <el-checkbox border v-for="(item,index) in checkBox" @change="perChange(item)" :label="item.id" :key="index">{{item.name}}</el-checkbox>   </el-checkbox-group>   <!-- 多选人员 end-->   </el-form-item>  </el-form>  <!-- 表单 end-->  <!-- tag展示区 -->  <el-row>   <el-tag class="tagClass" v-for="(tag,index) in tags" :key="index" closable @close="handleClose(tag)" :type="tag.id">   {{tag.name}}   </el-tag>   <el-button v-if="tags.length>0" @click="clearAll" plain>全部删除</el-button>  </el-row>  <!-- tag展示区 end-->  </el-col>   </el-row> </div></template><script>export default { name: ‘kk‘, mounted() {}, data() { return {  msg: ‘Welcome to Your Vue.js App‘,  tags: [],  tempForm: {  checkboxGroup5: [], //选择的人员  },  detailData: [],  checkBox: [{   name: ‘小红‘,   id: ‘101‘  },  {   name: ‘小黄‘,   id: ‘100‘  }, {   name: ‘小明‘,   id: ‘102‘  }, {   name: ‘小明‘,   id: ‘102‘  }  ],   } }, methods: { clearAll() { //全部清空数据  this.tags = []  this.tempForm.checkboxGroup5 = [] }, perChange(item) {  this.detailData.push(item) }, handleClose(tag) { //标签的删除事件  // 去掉当前删除的tag  let yourChoseTags = this.tempForm.checkboxGroup5  this.tempForm.checkboxGroup5 = yourChoseTags.filter(item => {  if (tag.id !== item) {   return true  }  })    }, delRepeat(arr) { //数组对象去重  return Object.values(  arr.reduce((obj, next) => {   var key = JSON.stringify(next);   return (obj[key] = next), obj;  }, {}),  ); }, moreArr() {  let yourChose = this.tempForm.checkboxGroup5  let tempTags = []  tempTags = this.baseDataDetail(yourChose, this.checkBox, tempTags)  this.detailData = tempTags }, baseDataDetail(yourChose, baseData, callBack) { //封装的数组方法  let temp = callBack  // 循环两个数据拿到选择的checkbox的id对应的初始数据  yourChose.forEach(item => {  baseData.forEach(itemSecond => {   if (item === itemSecond.id) {   temp.push(itemSecond)   }  })  })  return temp }, }, watch: { detailData() {  let tempArr = Object.assign([], this.detailData)  tempArr = this.delRepeat(tempArr)  // console.log(tempArr)  this.tags = tempArr }, "tempForm.checkboxGroup5" () {  this.moreArr() }, }}</script><!-- Add "scoped" attribute to limit CSS to this component only --><style scoped>.tempArea { /*width: 100%;*/}.tagClass{ margin-right: 10px;}</style>

Notable points:

Front-end Boutique tutorial: Baidu Network Disk download

1. I at the end of the method in the multi-box binding value TEMPFORM.CHECKBOXGROUP5 's listener event, I get a possible duplicate data (duplicate ID and name), and then assign this containing duplicate data array object to another array detaildata, In watch monitoring this array, to finish the weight, assign to tags to do the show.
Why do this, because, in our demand, in addition to the current page of the multi-marquee selection of people, there is a choice of all-company staff components, so no matter from which channel selected personnel can finally point to detaildata, ensure the rendering is correct

2. The array object to go heavy, the initial data may have a heavy ID, the name of the object (xiaoming), even if the model value of the binding multi-box will not have duplicate ID, but in the use of the ID to name the time, still will detect a number of, so that tag may show duplicate
So using this method, you can ensure that the last processed data is not duplicated, the tag will not show a number of the same,
But this method is a bit inflexible, is that you have to deal with the data ({id:1,name: ' Xiao Ming ', type:now}) must be ID, Name,type are repeated when the time, will be to go to the weight,
Expand: You can dynamically remove a property based on an attribute in the array object you set

3. I started out in the multi-box change event to do the tag display logic, because the change event can get the current choice of name and ID, but, when the change, you do not know whether this is checked or uncheck, so tags display will be problematic;

This logic may not be perfect, because it is possible that your personnel are selected from other components, so when you delete the tag, there may be a problem (don't discuss it for the time being)

Two or three things about how tag in Vue Elementui interacts with other elements of the page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.