ProductAttributes.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <el-col v-for="(item, index) in attributeList" :key="index">
  3. <div>
  4. <el-text class="mx-1">属性名:</el-text>
  5. <el-text class="mx-1">{{ item.name }}</el-text>
  6. </div>
  7. <div>
  8. <el-text class="mx-1">属性值:</el-text>
  9. <el-tag
  10. v-for="(value, valueIndex) in item.attributeValues"
  11. :key="value.name"
  12. :disable-transitions="false"
  13. class="mx-1"
  14. closable
  15. @close="handleClose(index, valueIndex)"
  16. >
  17. {{ value.name }}
  18. </el-tag>
  19. <el-input
  20. v-if="inputVisible"
  21. ref="InputRef"
  22. v-model="inputValue"
  23. class="!w-20"
  24. size="small"
  25. @blur="handleInputConfirm(index)"
  26. @keyup.enter="handleInputConfirm(index)"
  27. />
  28. <el-button v-else class="button-new-tag ml-1" size="small" @click="showInput(index)">
  29. + 添加
  30. </el-button>
  31. </div>
  32. <el-divider class="my-10px" />
  33. </el-col>
  34. </template>
  35. <script lang="ts" name="ProductAttributes" setup>
  36. import { ElInput } from 'element-plus'
  37. const inputValue = ref('') // 输入框值
  38. const inputVisible = ref(false) // 输入框显隐控制
  39. const InputRef = ref<InstanceType<typeof ElInput>>() //标签输入框Ref
  40. const attributeList = ref([])
  41. const props = defineProps({
  42. attributeData: {
  43. type: Object,
  44. default: () => {}
  45. }
  46. })
  47. watch(
  48. () => props.attributeData,
  49. (data) => {
  50. if (!data) return
  51. attributeList.value = data
  52. },
  53. {
  54. deep: true,
  55. immediate: true
  56. }
  57. )
  58. /** 删除标签 tagValue 标签值*/
  59. const handleClose = (index, valueIndex) => {
  60. const av = attributeList.value[index].attributeValues
  61. av.splice(valueIndex, 1)
  62. }
  63. /** 显示输入框并获取焦点 */
  64. const showInput = (index) => {
  65. inputVisible.value = true
  66. nextTick(() => {
  67. InputRef.value[index]!.input!.focus()
  68. })
  69. }
  70. /** 输入框失去焦点或点击回车时触发 */
  71. const handleInputConfirm = (index) => {
  72. if (inputValue.value) {
  73. // 因为ref再循环里,所以需要index获取对应的ref
  74. attributeList.value[index].attributeValues.push({ name: inputValue.value })
  75. }
  76. inputVisible.value = false
  77. inputValue.value = ''
  78. }
  79. </script>