search.vue 11 KB

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