7bc885d13f53e647046e3b58daf0167f5254fb8b.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <el-form-item :label="$t('chart.filters')">
  3. <el-tag v-for="(item,index) in currentFilters" :key="index" closable @close="handleClosefilter(index)" @click="handleEditFilter(item)">
  4. {{ generateFilterSentence(item) }}
  5. </el-tag>
  6. <el-button :disabled="disabled" type="primary" size="mini" style="width: 150px;" @click="visible=true">
  7. {{ $t('chart.addFilters') }}
  8. </el-button>
  9. <el-dialog :visible="visible" width="500px" :title="$t('chart.addFilters')">
  10. <el-form label-width="150px">
  11. <el-form-item :label="$t('chart.filterField')" class="el-form-item">
  12. <el-select v-model="filteCol" size="mini" :placeholder="$t('chart.selectFilterField')" style="width: 220px;">
  13. <el-option v-for="item in sharedState.allCols" :key="item.Column" :label="item.Column" :value="item.Column" />
  14. </el-select>
  15. </el-form-item>
  16. <el-form-item :label="$t('chart.filterOperator')" class="el-form-item">
  17. <el-select v-model="filterOperator" size="mini" :placeholder="$t('chart.selectFilterOperator')" style="width: 220px;">
  18. <el-option v-for="item in filterOperatorOpts" :key="item.name" :label="`${item.name} (${item.operator})`" :value="item.operator" />
  19. </el-select>
  20. </el-form-item>
  21. <el-form-item v-if="currentOperatorParamNum===-1" :label="$t('chart.comparedValue')" class="el-form-item">
  22. <el-tag v-for="(item,index) in arrValue" :key="index" closable @close="handleRemove">
  23. {{ item }}
  24. </el-tag>
  25. <el-input v-model="value3" size="mini" type="text" style="width: 220px;" />
  26. <el-button size="mini" type="primary" @click="handleAdd">
  27. Add
  28. </el-button>
  29. </el-form-item>
  30. <el-form-item v-else :label="$t('chart.comparedValue')" class="el-form-item">
  31. <el-input v-model="value1" size="mini" type="text" style="width: 220px;" />
  32. <span v-show="currentOperatorParamNum===2">~</span>
  33. <el-input v-show="currentOperatorParamNum===2" v-model="value2" style="width: 220px;" type="text" />
  34. </el-form-item>
  35. </el-form>
  36. <span slot="footer" class="dialog-footer">
  37. <el-button size="mini" @click="visible=false">{{ $t('common.cancel') }}</el-button>
  38. <el-button size="mini" type="primary" @click="handleConfirm">{{ $t('common.confirm') }}</el-button>
  39. </span>
  40. </el-dialog>
  41. </el-form-item>
  42. </template>
  43. <script>
  44. import { getFilterOperator, dataType } from '@/utils/configs'
  45. import { buildFilterSentence } from '@/utils/buildSentence'
  46. import store from '../store'
  47. export default {
  48. props: {
  49. disabled: {
  50. default: false
  51. },
  52. filters: {
  53. type: Array
  54. }
  55. },
  56. data() {
  57. return {
  58. visible: false,
  59. filterOperatorOpts: getFilterOperator(),
  60. filterStrs: [],
  61. filteCol: undefined,
  62. filterOperator: undefined,
  63. value1: undefined,
  64. value2: undefined,
  65. value3: undefined,
  66. arrValue: [],
  67. currentFilters: [],
  68. sharedState: store.state
  69. }
  70. },
  71. computed: {
  72. currentOperatorParamNum() {
  73. const a = getFilterOperator().find(item => item.operator === this.filterOperator)
  74. return a ? a.paramNum : 1
  75. },
  76. lang() {
  77. return this.$store.state.app.lang
  78. }
  79. },
  80. watch: {
  81. filters: {
  82. deep: true,
  83. handler(value) {
  84. this.currentFilters = value
  85. }
  86. },
  87. lang(val) {
  88. this.filterOperatorOpts = getFilterOperator()
  89. }
  90. },
  91. methods: {
  92. handleClosefilter(index) {
  93. this.currentFilters.splice(index, 1)
  94. this.handleSubmit()
  95. },
  96. handleEditFilter(filter) {
  97. this.visible = true
  98. this.filteCol = filter.filteCol
  99. this.filterOperator = filter.filterOperator
  100. this.value1 = filter.value.value1
  101. this.value2 = filter.value.value2
  102. this.arrValue = filter.value.arrValue
  103. },
  104. handleAdd() {
  105. this.arrValue.push(this.value3)
  106. this.value3 = undefined
  107. },
  108. handleRemove(value) {
  109. this.arrValue.splice(this.arrValue.indexOf(value), 1)
  110. },
  111. handleConfirm() {
  112. if (!this.filteCol || !this.filterOperator) {
  113. this.$message({
  114. type: 'warning',
  115. message: this.$t('chart.filterCantBeEmpty')
  116. })
  117. return
  118. }
  119. const filterObj = {
  120. filteCol: this.filteCol,
  121. colType: this.sharedState.allCols.find(item => item.Column === this.filteCol).Type,
  122. filterOperator: this.filterOperator,
  123. value: {
  124. value1: this.value1,
  125. value2: this.value2,
  126. arrValue: this.arrValue
  127. },
  128. operatorParamNum: this.operatorParamNum(this.filterOperator)
  129. }
  130. const filterIndex = this.currentFilters.findIndex(item => {
  131. return item.filteCol === this.filteCol && item.filterOperator === this.filterOperator
  132. })
  133. if (filterIndex >= 0) {
  134. this.currentFilters.splice(filterIndex, 1, filterObj)
  135. } else {
  136. this.currentFilters.push(filterObj)
  137. }
  138. this.handleSubmit()
  139. },
  140. handleSubmit() {
  141. const filterStrs = this.currentFilters.map(buildFilterSentence)
  142. this.$emit('change', filterStrs.join(' and '))
  143. this.$emit('update: filters', this.currentFilters)
  144. this.visible = false
  145. },
  146. generateFilterSentence(item) {
  147. return buildFilterSentence(item)
  148. },
  149. operatorParamNum(operator) {
  150. const a = getFilterOperator().find(item => item.operator === operator)
  151. return a ? a.paramNum : 1
  152. },
  153. needQuotation(col) {
  154. const colType = this.sharedState.allCols.find(item => item.Column === col).Type
  155. if (colType.indexOf('(') >= 0) {
  156. return dataType.find(type => type.name === colType.split('(')[0].toLowerCase()).needQuotation
  157. } else {
  158. return dataType.find(type => type.name === colType.toLowerCase()).needQuotation
  159. }
  160. },
  161. addQuotation(valueObj) {
  162. return {
  163. value1: `'${valueObj.value1}'`,
  164. value2: `'${valueObj.value2}'`,
  165. arrValue: valueObj.arrValue.map(value => `'${value}'`)
  166. }
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. .el-form-item {
  173. margin-bottom: 20px;
  174. }
  175. </style>