search.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <div class="searchComponent">
  3. <div class="field">
  4. <el-select v-model="label" size="small" @change="getObject">
  5. <el-option v-for="item in SearchFields" :key="item.value" :label="item.label" :value="item.value"></el-option>
  6. </el-select>
  7. </div>
  8. <div class="value">
  9. <div v-if="field.type == 2" style="display: flex; width: 100%">
  10. <el-date-picker v-model="value" type="date" style="width: 100%" size="small" value-format="yyyy-MM-dd"
  11. :placeholder="getPlaceholder()">
  12. </el-date-picker>
  13. </div>
  14. <div v-else-if="field.type == 3" style="width: 100%">
  15. <el-select v-model="value" size="small" multiple filterable collapse-tags :placeholder="getPlaceholder()"
  16. style="width: 100%">
  17. <el-option v-for="item in field.options || []" :key="item.value" :label="item.label" :value="item.value">
  18. </el-option>
  19. </el-select>
  20. </div>
  21. <div v-else-if="field.type == 4" style="width: 100%">
  22. <el-select v-model="value" size="small" filterable :placeholder="getPlaceholder()"
  23. style="width: 100%">
  24. <el-option v-for="item in field.options || []" :key="item.value" :label="item.label" :value="item.value">
  25. </el-option>
  26. </el-select>
  27. </div>
  28. <div v-else style="width: 100%">
  29. <el-input size="small" style="width: 100%" v-model="value" :placeholder="getPlaceholder()"></el-input>
  30. </div>
  31. </div>
  32. <div class="btn">
  33. <el-button size="small" type="primary" @click="search" class="indicia-top-button" style="width:100%"
  34. :disabled="disabled">搜 索</el-button>
  35. </div>
  36. <div class="option" v-if="searchOptions.length > 0">
  37. <el-popover placement="bottom" title="" width="300" trigger="hover" class="margin-left_10">
  38. <div class="main">
  39. <div v-for="(item, index) in searchOptions" :key="index" class="box">
  40. <el-alert type="success" :closable="false">
  41. <div class="content">
  42. <span class="color-black">{{ item.label }}</span>
  43. <b class="padding_0_5">=</b>
  44. <span class="color-black">
  45. <span v-if="item.type == 3">
  46. <span class="padding_0_5">(</span>
  47. <span v-for="(data, i) in item.searchValue" :key="data.label">
  48. <span class="query-data-label" @click="handleCancelQueryParams(item, index, i)">{{ data.label }}<i
  49. class="el-icon-close"></i></span>
  50. <b v-if="item.searchValue.length - 1 > i" class="padding_0_5 color-primary">OR</b>
  51. </span>
  52. <span class="padding_0_5">)</span>
  53. </span>
  54. <span v-else>
  55. <span class="query-data-label" @click="handleCancelQueryParams(item, index)">{{ item.searchValue.label
  56. }}<i class="el-icon-close"></i></span>
  57. </span>
  58. </span>
  59. </div>
  60. </el-alert>
  61. </div>
  62. </div>
  63. <el-button slot="reference" size="small" type="primary" style="width:100%">
  64. 检索条件<i class="el-icon-arrow-down el-icon--right"></i>
  65. </el-button>
  66. </el-popover>
  67. </div>
  68. </div>
  69. </template>
  70. <script>
  71. export default {
  72. components: {},
  73. props: {
  74. SearchFields: {
  75. default: () => {
  76. return [];
  77. },
  78. },
  79. disabled: {
  80. default: false
  81. },
  82. searchValue: {
  83. default: () => {
  84. return {};
  85. },
  86. },
  87. projectId: {
  88. default:null
  89. },
  90. taskId: {
  91. default:null
  92. }
  93. },
  94. data() {
  95. return {
  96. inner: false,
  97. label: '',
  98. value: null,
  99. field: {},
  100. searchOptions: [],
  101. innerChange: false
  102. };
  103. },
  104. watch: {
  105. SearchFields() {
  106. if (this.innerChange) {
  107. return
  108. }
  109. this.getLabel()
  110. },
  111. searchValue: {
  112. deep: true,
  113. handler(n, o) {
  114. if (!this.inner) {
  115. this.getSearchOptions()
  116. } else {
  117. this.inner = false
  118. }
  119. }
  120. }
  121. },
  122. computed: {},
  123. created() { },
  124. mounted() {
  125. this.getLabel()
  126. this.getSearchOptions()
  127. },
  128. methods: {
  129. getSearchOptions() {
  130. this.searchOptions = []
  131. if (Object.keys(this.searchValue).length > 0) {
  132. Object.keys(this.searchValue).forEach(key => {
  133. if (this.searchValue[key] || (typeof this.searchValue[key] == 'object' && this.searchValue[key].length > 0)) {
  134. var field = this.SearchFields.find(item => {
  135. return item.value == key
  136. })
  137. if(!field){
  138. return false
  139. }
  140. if (typeof this.searchValue[key] == 'object') {
  141. this.searchOptions.push(
  142. {
  143. ...field,
  144. searchValue: field.options.filter(item => {
  145. return this.searchValue[key].indexOf(item.value) != -1
  146. })
  147. }
  148. )
  149. } else {
  150. this.searchOptions.push(
  151. {
  152. ...field,
  153. searchValue: {
  154. label: this.searchValue[key]
  155. }
  156. }
  157. )
  158. }
  159. }
  160. })
  161. }
  162. },
  163. getLabel() {
  164. if (this.SearchFields.length > 0) {
  165. this.label = this.SearchFields[0].value
  166. this.getObject(this.label)
  167. }
  168. },
  169. handleCancelQueryParams(data, index, i) {
  170. this.inner = true
  171. if (data.type == 3) {
  172. data.searchValue.splice(i, 1)
  173. if (data.searchValue.length == 0) {
  174. this.searchOptions.splice(index, 1)
  175. }
  176. } else {
  177. this.searchOptions.splice(index, 1)
  178. }
  179. this.$emit('search', this.searchOptions)
  180. },
  181. search() {
  182. this.inner = true
  183. if (this.value || this.value.length > 0) {
  184. var searchValue = {}
  185. if (this.field.type == 3) {
  186. searchValue = this.field.options.filter(item => {
  187. return this.value.indexOf(item.value) != -1
  188. })
  189. } else {
  190. searchValue = {
  191. label: this.value
  192. }
  193. }
  194. this.searchOptions.push(
  195. {
  196. ...this.field,
  197. searchValue: searchValue
  198. }
  199. )
  200. this.getValue()
  201. }
  202. this.$emit('search', this.searchOptions)
  203. },
  204. getObject(val) {
  205. this.field = this.SearchFields.find(item => {
  206. return item.value == val
  207. })
  208. this.getValue()
  209. this.getOption()
  210. },
  211. //获取选项
  212. getOption() {
  213. if (this.field.type == '3') {
  214. if (this.field.options && this.field.options.length > 0) {
  215. return false
  216. }
  217. switch (this.field.group) {
  218. case 'customField':
  219. this.getCustomField()
  220. break;
  221. case 'product':
  222. this.queryProductCategory(2)
  223. break;
  224. case 'productCategory':
  225. this.queryProductCategory(1)
  226. break;
  227. case 'technical':
  228. this.queryProductCategory(3)
  229. break;
  230. case 'nos':
  231. break;
  232. default:
  233. break
  234. }
  235. } else {
  236. return false
  237. }
  238. },
  239. //获取自定义选项
  240. getCustomField() {
  241. if (this.field.type == 'tree') {
  242. this.queryProductCategory(4)
  243. return false
  244. }
  245. let params = {
  246. customFieldId: this.field.value,
  247. }
  248. this.innerChange = true
  249. this.$api.queryCustomOption(params).then(response => {
  250. if (response.code == 200) {
  251. var a = response.data.data.map(item => {
  252. return {
  253. label: item.name,
  254. value: item.id
  255. }
  256. })
  257. this.$set(this.field, 'options', a)
  258. this.$nextTick(() => {
  259. this.innerChange = false
  260. })
  261. }
  262. }).catch(error => {
  263. // this.field.options = []
  264. })
  265. },
  266. //获取产品或产品类别架构以及技术分类
  267. queryProductCategory(type) {
  268. let params = {
  269. projectId: this.projectId,
  270. taskId: this.taskId,
  271. type: type,//类型:1产品类别,2产品,3技术分类,4自定义树
  272. typeId: this.field.value,//产品或类别id
  273. }
  274. this.innerChange = true
  275. this.$api.queryTreeNodeTree(params).then(res => {
  276. if (res.code == 200) {
  277. this.$set(this.field, 'options', response.data.data)
  278. this.$nextTick(() => {
  279. this.innerChange = false
  280. })
  281. }
  282. }).catch(err => {
  283. })
  284. },
  285. getValue() {
  286. if (this.field.type == 3) {
  287. this.value = []
  288. } else {
  289. this.value = ''
  290. }
  291. },
  292. getPlaceholder() {
  293. var placeholder = '请输入'
  294. if (this.field.placeholder) {
  295. placeholder = this.field.placeholder
  296. } else {
  297. if (this.field.type) {
  298. if (this.field.type == 1) {
  299. placeholder = '请输入'
  300. } else if (this.field.type == 2) {
  301. placeholder = '请选择时间'
  302. } else if (this.field.type == 3) {
  303. placeholder = '请选择'
  304. }
  305. }
  306. }
  307. return placeholder
  308. }
  309. },
  310. };
  311. </script>
  312. <style lang="scss" scoped>
  313. .main {
  314. padding: 0 !important;
  315. height: 250px;
  316. overflow-y: auto;
  317. .box {
  318. margin-top: 10px;
  319. .content {
  320. line-height: 30px;
  321. font-size: 14px;
  322. }
  323. }
  324. }
  325. .query-data-label {
  326. cursor: pointer;
  327. &:hover {
  328. text-decoration: underline;
  329. color: red;
  330. }
  331. }
  332. .searchComponent {
  333. padding: 0 10px;
  334. display: flex;
  335. // width: 100%;
  336. }
  337. .searchComponent>* {
  338. margin-right: 10px;
  339. width: 100%;
  340. }
  341. .value {
  342. width: 100%
  343. }
  344. .btn {
  345. width: 100px;
  346. margin-right: 0;
  347. }
  348. .field {
  349. max-width: 150px;
  350. }
  351. .option {
  352. width: 100px;
  353. margin-right: 0;
  354. }
  355. </style>