561a8c75a6c3f5ff52f611acd0bc65f7d45f827a.svn-base 6.2 KB

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