Newer
Older
KaiFengPC / src / views / preassess / calculate / tables / mixins / index.js
@zhangdeliang zhangdeliang on 23 May 1 KB 初始化项目

import { computed } from 'vue'

export default function useTable(proxy) {
  const numberValidate = (rule, value, callback) => {
    if (value === '') {
      callback()
    } else {
      const reg = /^\d+(\.\d+)?$/
      if(reg.test(value)) callback()
      else callback(new Error('请输入数字'))
    }
  }

  const scopeValidate = (rule, value, callback) => {
    if (value === '') {
      callback()
    } else {
      const reg = /^\d+(\.\d+)?$/
      if(reg.test(value)){
        const props = rule.field.split('.')
        const { downValue, upValue } = formModel.value[props[0]][props[1]]
        if(parseFloat(value) >= downValue * 1 && parseFloat(value) <= upValue * 1){
          callback()
        } else {
          callback(new Error('请输入参考值范围的值'))
        }
      } else {
        callback(new Error('请输入参考值范围的值'))
      }
    }
  }

  const formModel = computed(() => {
    return {
      list: proxy.$props.list
    }
  })

  const rules = reactive({
    facilityArea: [{ validator: numberValidate, trigger: ['blur']  }],
    effectiveStorageDepth: [{ validator: numberValidate, trigger: ['blur']  }],
    facilityServiceArea: [{ validator: numberValidate, trigger: ['blur']  }],
    pollutionRemovalRate: [{ validator: scopeValidate, trigger: ['blur']  }]
  })

  const objectSpanMethod = ({
    row,
    column,
    rowIndex,
    columnIndex,
  }) => {
    if (columnIndex === 0) {
      if (rowIndex === 0) {
        return {
          rowspan: formModel.value.list.length,
          colspan: 1,
        }
      } else {
        return {
          rowspan: 0,
          colspan: 0,
        }
      }
    }
  }

  const validate = () => {
    return proxy.$refs.tableForm.validate()
  }

  return {
    formModel,
    rules,
    objectSpanMethod,
    validate
  }
}