bf1f608d09099d0f122fcccf3e655441260c3040.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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" class="save" 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. <div slot="footer" class="dialog-footer">
  41. <el-button class="downminCancel" size="mini" style="right: 90px;" @click="visible=false">取消</el-button>
  42. <el-button class="miniPrimary" size="mini" type="primary" @click="handleConfirm">确认</el-button>
  43. </div>
  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. roles: {
  54. default: Array
  55. },
  56. disabled: {
  57. default: false
  58. },
  59. filters: {
  60. type: Array
  61. }
  62. },
  63. data() {
  64. return {
  65. visible: false,
  66. filterOperatorOpts: getFilterOperator(),
  67. filterStrs: [],
  68. filteCol: undefined,
  69. filterOperator: undefined,
  70. value1: undefined,
  71. value2: undefined,
  72. value3: undefined,
  73. arrValue: [],
  74. currentFilters: [],
  75. sharedState: store.state,
  76. eng_value: '',
  77. chi_value: ''
  78. }
  79. },
  80. computed: {
  81. currentOperatorParamNum() {
  82. const a = getFilterOperator().find(item => item.operator === this.filterOperator)
  83. return a ? a.paramNum : 1
  84. },
  85. lang() {
  86. return this.$store.state.app.lang
  87. }
  88. },
  89. watch: {
  90. filters: {
  91. deep: true,
  92. handler(value) {
  93. console.log(value, '====value2')
  94. this.currentFilters = value
  95. }
  96. },
  97. lang(val) {
  98. this.filterOperatorOpts = getFilterOperator()
  99. }
  100. },
  101. methods: {
  102. comment_change(e) {
  103. console.log(e)
  104. var eng_value = e
  105. var chi_value = ''
  106. this.sharedState.allCols.forEach(function(i) {
  107. if (i.Column == eng_value) {
  108. chi_value = i.Comment
  109. }
  110. })
  111. this.chi_value = chi_value
  112. console.log('英文参数', eng_value, '中文参数', chi_value)
  113. },
  114. handleClose() {
  115. this.visible = false
  116. },
  117. handleClosefilter(index) {
  118. this.currentFilters.splice(index, 1)
  119. this.handleSubmit()
  120. },
  121. handleEditFilter(filter) {
  122. this.visible = true
  123. this.filteCol = filter.filteCol
  124. this.filterOperator = filter.filterOperator
  125. this.value1 = filter.value.value1
  126. this.value2 = filter.value.value2
  127. this.arrValue = filter.value.arrValue
  128. },
  129. handleAdd() {
  130. this.arrValue.push(this.value3)
  131. this.value3 = undefined
  132. },
  133. handleRemove(value) {
  134. this.arrValue.splice(this.arrValue.indexOf(value), 1)
  135. },
  136. handleConfirm() {
  137. if (!this.filteCol || !this.filterOperator) {
  138. this.$message({
  139. type: 'warning',
  140. message: this.$t('chart.filterCantBeEmpty')
  141. })
  142. return
  143. }
  144. const filterObj = {
  145. filteCol: this.filteCol,
  146. colType: this.sharedState.allCols.find(item => item.Column === this.filteCol).Type,
  147. filterOperator: this.filterOperator,
  148. value: {
  149. value1: this.value1,
  150. value2: this.value2,
  151. arrValue: this.arrValue,
  152. chi_value: this.chi_value
  153. },
  154. operatorParamNum: this.operatorParamNum(this.filterOperator)
  155. }
  156. const filterIndex = this.currentFilters.findIndex(item => {
  157. return item.filteCol === this.filteCol && item.filterOperator === this.filterOperator
  158. })
  159. if (filterIndex >= 0) {
  160. this.currentFilters.splice(filterIndex, 1, filterObj)
  161. } else {
  162. this.currentFilters.push(filterObj)
  163. }
  164. this.handleSubmit()
  165. },
  166. handleSubmit() {
  167. const filterStrs = this.currentFilters.map(buildFilterSentence)
  168. console.log('this.currentFilters====子组件', this.currentFilters)
  169. this.$emit('change', filterStrs.join(' and '))
  170. this.$emit('update: filters', this.currentFilters)
  171. this.visible = false
  172. },
  173. generateFilterSentence(item) {
  174. return buildFilterSentence(item)
  175. },
  176. operatorParamNum(operator) {
  177. const a = getFilterOperator().find(item => item.operator === operator)
  178. return a ? a.paramNum : 1
  179. },
  180. needQuotation(col) {
  181. const colType = this.sharedState.allCols.find(item => item.Column === col).Type
  182. if (colType.indexOf('(') >= 0) {
  183. return dataType.find(type => type.name === colType.split('(')[0].toLowerCase()).needQuotation
  184. } else {
  185. return dataType.find(type => type.name === colType.toLowerCase()).needQuotation
  186. }
  187. },
  188. addQuotation(valueObj) {
  189. return {
  190. value1: `'${valueObj.value1}'`,
  191. value2: `'${valueObj.value2}'`,
  192. chi_value: `'${valueObj.chi_value}'`,
  193. arrValue: valueObj.arrValue.map(value => `'${value}'`)
  194. }
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. .el-form-item {
  201. margin-bottom: 20px;
  202. }
  203. </style>