123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <template>
- <el-form-item :label="$t('chart.filters')">
- <!-- <el-tag v-for="(item,index) in currentFilters" :key="index" closable @close="handleClosefilter(index)" @click="handleEditFilter(item)">
- {{ generateFilterSentence(item) }}
- </el-tag> -->
- <el-tag v-for="(item,index) in currentFilters" :key="index" closable @close="handleClosefilter(index)">
- {{ item.value.chi_value }} {{ item.filterOperator }} {{ item.value.value1 }}
- </el-tag>
- <el-button :disabled="disabled" class="save" style="width: 150px;" @click="visible=true">
- 添加筛选条件
- </el-button>
- <el-dialog :visible="visible" width="500px" title="添加筛选条件" :before-close="handleClose">
- <el-form label-width="150px">
- <el-form-item label="筛选字段" class="el-form-item">
- <el-select v-model="filteCol" size="mini" placeholder="请选择筛选字段" style="width: 220px;" @change="comment_change">
- <!-- <el-option v-for="item in sharedState.allCols" :key="item.Column" :label="item.Column" :value="item.Column" /> -->
- <el-option v-for="item in sharedState.allCols" :key="item.Column" :label="item.Comment" :value="item.Column" />
- </el-select>
- </el-form-item>
- <el-form-item label="筛选操作符" class="el-form-item">
- <el-select v-model="filterOperator" size="mini" placeholder="请选择筛选字段" style="width: 220px;">
- <el-option v-for="item in filterOperatorOpts" :key="item.name" :label="`${item.name} (${item.operator})`" :value="item.operator" />
- </el-select>
- </el-form-item>
- <el-form-item v-if="currentOperatorParamNum===-1" :label="$t('chart.comparedValue')" class="el-form-item">
- <el-tag v-for="(item,index) in arrValue" :key="index" closable @close="handleRemove">
- {{ item }}
- </el-tag>
- <el-input v-model="value3" size="mini" type="text" style="width: 220px;" />
- <el-button size="mini" type="primary" @click="handleAdd">
- Add
- </el-button>
- </el-form-item>
- <el-form-item v-else :label="$t('chart.comparedValue')" class="el-form-item">
- <el-input v-model="value1" size="mini" type="text" style="width: 220px;" />
- <span v-show="currentOperatorParamNum===2">~</span>
- <el-input v-show="currentOperatorParamNum===2" v-model="value2" style="width: 220px;" type="text" />
- </el-form-item>
- </el-form>
- <span slot="footer" class="dialog-footer">
- <el-button size="mini" @click="visible=false">取消</el-button>
- <el-button size="mini" type="primary" @click="handleConfirm">确认</el-button>
- </span>
- </el-dialog>
- </el-form-item>
- </template>
- <script>
- import { getFilterOperator, dataType } from '@/utils/configs'
- import { buildFilterSentence } from '@/utils/buildSentence'
- import store from '../store'
- export default {
- props: {
- disabled: {
- default: false
- },
- filters: {
- type: Array
- }
- },
- data() {
- return {
- visible: false,
- filterOperatorOpts: getFilterOperator(),
- filterStrs: [],
- filteCol: undefined,
- filterOperator: undefined,
- value1: undefined,
- value2: undefined,
- value3: undefined,
- arrValue: [],
- currentFilters: [],
- sharedState: store.state,
- eng_value: '',
- chi_value: ''
- }
- },
- computed: {
- currentOperatorParamNum() {
- const a = getFilterOperator().find(item => item.operator === this.filterOperator)
- return a ? a.paramNum : 1
- },
- lang() {
- return this.$store.state.app.lang
- }
- },
- watch: {
- filters: {
- deep: true,
- handler(value) {
- this.currentFilters = value
- }
- },
- lang(val) {
- this.filterOperatorOpts = getFilterOperator()
- }
- },
- methods: {
- comment_change(e) {
- console.log(e)
- var eng_value = e
- var chi_value = ''
- this.sharedState.allCols.forEach(function(i) {
- if (i.Column == eng_value) {
- chi_value = i.Comment
- }
- })
- this.chi_value = chi_value
- console.log('英文参数', eng_value, '中文参数', chi_value)
- },
- handleClose() {
- this.visible = false
- },
- handleClosefilter(index) {
- this.currentFilters.splice(index, 1)
- this.handleSubmit()
- },
- handleEditFilter(filter) {
- this.visible = true
- this.filteCol = filter.filteCol
- this.filterOperator = filter.filterOperator
- this.value1 = filter.value.value1
- this.value2 = filter.value.value2
- this.arrValue = filter.value.arrValue
- },
- handleAdd() {
- this.arrValue.push(this.value3)
- this.value3 = undefined
- },
- handleRemove(value) {
- this.arrValue.splice(this.arrValue.indexOf(value), 1)
- },
- handleConfirm() {
- if (!this.filteCol || !this.filterOperator) {
- this.$message({
- type: 'warning',
- message: this.$t('chart.filterCantBeEmpty')
- })
- return
- }
- const filterObj = {
- filteCol: this.filteCol,
- colType: this.sharedState.allCols.find(item => item.Column === this.filteCol).Type,
- filterOperator: this.filterOperator,
- value: {
- value1: this.value1,
- value2: this.value2,
- arrValue: this.arrValue,
- chi_value: this.chi_value
- },
- operatorParamNum: this.operatorParamNum(this.filterOperator)
- }
- const filterIndex = this.currentFilters.findIndex(item => {
- return item.filteCol === this.filteCol && item.filterOperator === this.filterOperator
- })
- if (filterIndex >= 0) {
- this.currentFilters.splice(filterIndex, 1, filterObj)
- } else {
- this.currentFilters.push(filterObj)
- }
- this.handleSubmit()
- },
- handleSubmit() {
- const filterStrs = this.currentFilters.map(buildFilterSentence)
- console.log('this.currentFilters====子组件', this.currentFilters)
- this.$emit('change', filterStrs.join(' and '))
- this.$emit('update: filters', this.currentFilters)
- this.visible = false
- },
- generateFilterSentence(item) {
- return buildFilterSentence(item)
- },
- operatorParamNum(operator) {
- const a = getFilterOperator().find(item => item.operator === operator)
- return a ? a.paramNum : 1
- },
- needQuotation(col) {
- const colType = this.sharedState.allCols.find(item => item.Column === col).Type
- if (colType.indexOf('(') >= 0) {
- return dataType.find(type => type.name === colType.split('(')[0].toLowerCase()).needQuotation
- } else {
- return dataType.find(type => type.name === colType.toLowerCase()).needQuotation
- }
- },
- addQuotation(valueObj) {
- return {
- value1: `'${valueObj.value1}'`,
- value2: `'${valueObj.value2}'`,
- chi_value: `'${valueObj.chi_value}'`,
- arrValue: valueObj.arrValue.map(value => `'${value}'`)
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .el-form-item {
- margin-bottom: 20px;
- }
- </style>
|