TreeNodeDialog.vue 3.52 KB
Newer Older
sunhongwei's avatar
sunhongwei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <el-row :gutter="0">
        <el-form
          ref="elForm"
          :model="formData"
          :rules="rules"
          size="small"
          label-width="100px"
        >
          <el-col :span="24">
            <el-form-item
Marcus's avatar
Marcus committed
21
              :label="$t('选项名')"
sunhongwei's avatar
sunhongwei committed
22 23 24 25
              prop="label"
            >
              <el-input
                v-model="formData.label"
Marcus's avatar
Marcus committed
26
                :placeholder="$t('请输入选项名')"
sunhongwei's avatar
sunhongwei committed
27 28 29 30 31 32
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
Marcus's avatar
Marcus committed
33
              :label="$t('选项值')"
sunhongwei's avatar
sunhongwei committed
34 35 36 37
              prop="value"
            >
              <el-input
                v-model="formData.value"
Marcus's avatar
Marcus committed
38
                :placeholder="$t('请输入选项值')"
sunhongwei's avatar
sunhongwei committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                clearable
              >
                <el-select
                  slot="append"
                  v-model="dataType"
                  :style="{width: '100px'}"
                >
                  <el-option
                    v-for="(item, index) in dataTypeOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                    :disabled="item.disabled"
                  />
                </el-select>
              </el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button
          type="primary"
          @click="handelConfirm"
Marcus's avatar
Marcus committed
63 64
        >{{ $t('确定') }}</el-button>
        <el-button @click="close">{{ $t('取消') }}</el-button>
sunhongwei's avatar
sunhongwei committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { isNumberStr } from '@/utils/index'
import { getTreeNodeId, saveTreeNodeId } from '@/utils/db'

const id = getTreeNodeId()

export default {
  components: {},
  inheritAttrs: false,
  props: [],
  data() {
    return {
      id,
      formData: {
        label: undefined,
        value: undefined
      },
      rules: {
        label: [
          {
            required: true,
Marcus's avatar
Marcus committed
90
            message: this.$t('请输入选项名'),
sunhongwei's avatar
sunhongwei committed
91 92 93 94 95 96
            trigger: 'blur'
          }
        ],
        value: [
          {
            required: true,
Marcus's avatar
Marcus committed
97
            message: this.$t('请输入选项值'),
sunhongwei's avatar
sunhongwei committed
98 99 100 101 102 103 104
            trigger: 'blur'
          }
        ]
      },
      dataType: 'string',
      dataTypeOptions: [
        {
Marcus's avatar
Marcus committed
105
          label: this.$t('字符串'),
sunhongwei's avatar
sunhongwei committed
106 107 108
          value: 'string'
        },
        {
Marcus's avatar
Marcus committed
109
          label: this.$t('数字'),
sunhongwei's avatar
sunhongwei committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
          value: 'number'
        }
      ]
    }
  },
  computed: {},
  watch: {
    // eslint-disable-next-line func-names
    'formData.value': function (val) {
      this.dataType = isNumberStr(val) ? 'number' : 'string'
    },
    id(val) {
      saveTreeNodeId(val)
    }
  },
  created() {},
  mounted() {},
  methods: {
    onOpen() {
      this.formData = {
        label: undefined,
        value: undefined
      }
    },
    onClose() {},
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {
      this.$refs.elForm.validate(valid => {
        if (!valid) return
        if (this.dataType === 'number') {
          this.formData.value = parseFloat(this.formData.value)
        }
        this.formData.id = this.id++
        this.$emit('commit', this.formData)
        this.close()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>