自定义组件(原创)——组合Ccombine

本组件中使用到了iview的Icon,可以在全局安装了Iview的项目或者局部引入了Icon的页面中自由使用。

目录

效果展示

组合组件效果展示

从左到右分别是:未选中状态、鼠标悬浮、选中、添加组合按钮

功能描述

  1. 添加/删除组合
  2. 单击聚焦组合
  3. 双击修改组合名字
  4. 切换下一个组合或者新增组合时上一个组合内容固定
  5. 组合不能清空,可以设置组合数量上限

结构代码

<template>
  <div class="c-combine">
    <div class="comBox">
      <div v-for="(item,idx) in com_arr" :key="item.id" class="comcell" :class="{'selcomcell': item.selected}" @click="selcom_handle(idx)">
        <span v-if="!item.edit" @dblclick.stop="name_click(2, idx)" @click.stop="name_click(1, idx)">{{item.name}}</span>
        <input v-else v-model="item.name" type="text" placeholder="组合名" maxlength="5" class="name-input" @focus="name_input(idx)" @blur="input_over(idx,item.name)" @click.stop>
        <Icon type="md-close-circle" @click.stop="close_com(idx)" />
      </div>
      <button type="text" class="add-btn" @click="addcom_handle()">+添加</button>
    </div>
  </div>
</template>
  • 根据edit确定编辑状态还是文本显示状态
  • selected确定选中状态还是未选中状态

逻辑代码

// 输入组合名字
name_input (idx) {
  // console.log('聚焦')
},
// 组合名字输入完毕
input_over (idx, name) {
  this.com_arr[idx].edit = false
  // console.log('失焦')
  this.$emit('changename', idx, name)
  this.$emit('synfun', this.com_arr)
},
name_click (_type, idx) {
  var self = this
  clearTimeout(this.timer)
  if (_type === 1) { // 单击某个组合
    if (event.detail === 2) return
    this.timer = setTimeout(function () {
      // console.log('单击')
      let last_sel_id = 0
      if (isArray(self.com_arr)) {
        self.com_arr.forEach(com => {
          if (com.selected)last_sel_id = com.id
          com.edit = false
          com.selected = false
        })
        self.com_arr[idx].selected = true
        self.$emit('fixfun', 'click', last_sel_id, idx)
      }
    }, 100)
  } else {
    // console.log('双击')
    this.com_arr[idx].edit = true
  }
  this.$emit('synfun', this.com_arr)
},
// 选中某个组合
selcom_handle (idx) {
  if (isArray(this.com_arr)) {
    let last_sel_id = 0
    this.com_arr.forEach(com => {
      if (com.selected)last_sel_id = com.id
      com.selected = false
    })
    this.com_arr[idx].selected = true
    this.$emit('synfun', this.com_arr)
    this.$emit('fixfun', 'click', last_sel_id, idx)
  }
},
// 删除某个组合
close_com (idx) {
  if (this.com_arr.length === 1) {
    this.$Message.warning('组合不能清空哦-_-')
  } else {
    alert('删除' + (idx + 1) + 'id: ' + this.com_arr[idx].id)
    let cur_idx = 999
    if (this.com_arr[idx].selected) {
      this.com_arr.splice(idx, 1)
      this.com_arr[0].selected = true
      cur_idx = 0
    } else {
      this.com_arr.splice(idx, 1)
    }
    this.$emit('synfun', this.com_arr)
    this.$emit('delfun', idx, cur_idx)
  }
},
// 新增组合
addcom_handle () {
  if (this.com_arr.length === this.max_com) {
    this.$Message.warning('最多只能添加5个组合~.~')
  } else if (isArray(this.com_arr)) {
    let last_sel_id = 0
    this.com_arr.forEach(com => {
      if (com.selected)last_sel_id = com.id
      com.selected = false
      com.edit = false
    })
    const id_temp = genID(1)
    const addcom = {
      name: '双击命名',
      content: {},
      id: id_temp,
      selected: true,
      edit: true
    }
    this.com_arr.push(addcom)
    this.$emit('synfun', this.com_arr)
    this.$emit('fixfun', 'add', last_sel_id, addcom)
  }
}
  • 处理新增组合事件
  • 删除某个组合事件
  • 选中某个组合事件
  • 单击或双击某个组合事件(单击选中/双击改名)
  • 输入组合名结束事件

组件应用

<div class="combineDiv" v-if="com_show">
  <Ccombine
    :com_arr="com_arr"
    :max_com="max_com"
    @synfun="syn_handle"
    @fixfun="fix_handle"
    @delfun="del_handle"
    @changename="changename_handle"
  />
</div>

import Ccombine from '@/components/c-combine/index.vue'

components: {
    Ccombine
}

data () {
    return {
        max_com: 5, // 组合最大数量
        com_arr: [{ // 默认组合
            name: '组合6',
            content: {},
            id: 0,
            selected: true,
            edit: false
        }]
    }
}
  • import引入——>组件注册——>使用
  • 父——>子传参:com_arr初始组合、max_com组合的最大数量

事件钩子

syn_handle (newValue) {
  this.com_arr = newValue
},
// 上一个组合内容固定
fix_handle (motion, last_sel_id, param) {
    // motion:动作'add'/'click'新增或者切换
    // last_sel_id:上一个聚焦的组合id
    // param:'add'对应addcom/'click'对应cur_idx
},
// 删除某个组合
del_handle (idx, cur_idx) {
    // idx:删除的组合index,cur_idx删除的组合Index
},
// 组合改名字
changename_handle (idx, name) {
    // idx:修改名字组合的idx,name:修改后的新组合名
}
  • syn_handle:同步处理,子组件里的组合更改同步到父组件
  • fix_handle:固定组合内容处理,新增或者切换的时候固定上一个组合的content
  • del_handle:删除某个组合
  • changename_handle:组合改名字

github

完整代码点击这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值