791fb2d8c47f2dff55ad7f8639065cc29f97b9f3.svn-base 6.9 KB

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