anji-select.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <!--
  2. * @Descripttion:
  3. * @Author: Devli
  4. * @Date: 2021-7-17 10:42:24
  5. * @Last Modified by: qianlishi
  6. * @Last Modified time: 2023-1-10 13:04:24
  7. !-->
  8. <!--element下拉列表封装
  9. 使用:
  10. <hyb-select ref="hybSelect"
  11. url="/v1/orgs"
  12. v-model="search.orgId" option="orgId" label="orgName"
  13. placeholder multiple localOptions></hyb-select>
  14. 1.url:要显示的下拉列表数据来源
  15. 规范:返回的数据要符合(JSON中对应的字段必须:label和value,不能其他)
  16. [
  17. {label:'显示1',value:'值1'},
  18. {label:'显示2',value:'值2'},
  19. {label:'显示3',value:'值3'},
  20. ...
  21. ]
  22. 2.v-model数据的双向绑定,绑定父组件中的字段
  23. 3.option自定义select的value对应的字段
  24. 4.label自定义select的label对应的字段
  25. 5.placeholder下拉列表提示
  26. 6.multiple是否是多选
  27. 7.localOptions使用本地的数据,不用请求远程数据,注意:使用该属性时Url属性不能要,不然无效
  28. -->
  29. <template>
  30. <div>
  31. <el-select
  32. v-model="selectValue"
  33. :clearable="clearable"
  34. :allow-create="!!allowCreate"
  35. :default-first-option="!!allowCreate"
  36. :collapse-tags="collapseTags"
  37. filterable
  38. class="filter-item"
  39. :placeholder="placeholder"
  40. :disabled="disabled"
  41. :multiple="multiple == null ? false : true"
  42. :remote="remoteFilter"
  43. :remote-method="remoteQuery"
  44. @change="change"
  45. >
  46. <el-option
  47. v-for="(item, index) in options"
  48. :key="index"
  49. :label="getItemLabel(item, label)"
  50. :value="item[option] + ''"
  51. :disabled="isDisabledOption(item)"
  52. >
  53. <template v-if="mergeLabel">
  54. <span style="float: left">{{ getItemLabel(item, label) }}</span>
  55. <span
  56. v-if="!dictCode && !localOptions"
  57. style="float: right; color: #8492a6; font-size: 13px"
  58. >{{ item[option] }}</span
  59. >
  60. </template>
  61. </el-option>
  62. <el-option v-if="totalPage >= 1" value="" disabled
  63. >输入关键词搜索更多</el-option
  64. >
  65. </el-select>
  66. </div>
  67. </template>
  68. <script>
  69. import request from "@/utils/request";
  70. export default {
  71. props: {
  72. dictCode: null, // 当传入dictCode时,可以不用传递url
  73. url: null,
  74. allowCreate: {
  75. type: Boolean,
  76. default: false,
  77. },
  78. method: {
  79. type: String,
  80. default: "get",
  81. },
  82. queryParam: {
  83. type: Object,
  84. default: () => {
  85. return {};
  86. },
  87. },
  88. value: null,
  89. placeholder: null,
  90. label: {
  91. type: String,
  92. default: "text",
  93. },
  94. option: {
  95. type: String,
  96. default: "id",
  97. },
  98. multiple: null,
  99. localOptions: null,
  100. disabled: null,
  101. clearable: {
  102. type: Boolean,
  103. default: true,
  104. },
  105. collapseTags: {
  106. type: Boolean,
  107. default: false,
  108. },
  109. mergeLabel: {
  110. type: Boolean,
  111. default: true,
  112. },
  113. // 禁用的下拉选项
  114. disabledOptions: {
  115. type: String,
  116. default: () => {
  117. return "";
  118. },
  119. },
  120. // 使用远程搜索
  121. remoteFilter: {
  122. type: Boolean,
  123. default: false,
  124. },
  125. },
  126. data() {
  127. return {
  128. options: null,
  129. selectValue: null,
  130. // 如果是分页的,
  131. totalPage: 0,
  132. };
  133. },
  134. computed: {
  135. // 根据dictCode和url拼出最终的请求url
  136. requestUrl() {
  137. if (this.localOptions) {
  138. return null;
  139. }
  140. if (this.url != null && this.url.trim() != "") {
  141. if (this.url.indexOf("?") > 0) {
  142. if (this.option == null) {
  143. // console.log('url-' + this.url.substring(this.url.indexOf('?')))
  144. }
  145. if (this.label == null) {
  146. }
  147. }
  148. return this.url;
  149. }
  150. if (this.dictCode != null && this.dictCode.trim() != "") {
  151. return `/meta/gaeaDict/select/${this.dictCode}`;
  152. }
  153. return null;
  154. },
  155. },
  156. watch: {
  157. dictCode(val) {
  158. if (val) {
  159. this.queryData();
  160. }
  161. },
  162. // 监听接口地址变化时,触发刷新请求
  163. localOptions(val) {
  164. this.options = val;
  165. },
  166. value: {
  167. handler(val) {
  168. if (
  169. typeof val == "string" &&
  170. this.url != null &&
  171. this.url.trim() != ""
  172. ) {
  173. this.remoteQuery(val);
  174. }
  175. if (this.multiple != null) {
  176. if (!this.value) {
  177. this.selectValue = [];
  178. } else {
  179. this.selectValue = this.value;
  180. }
  181. } else {
  182. if (this.value != null && this.value != undefined) {
  183. this.selectValue = this.value + "";
  184. } else {
  185. this.selectValue = "";
  186. }
  187. }
  188. },
  189. immediate: true,
  190. },
  191. url() {
  192. setTimeout(() => {
  193. this.queryData();
  194. }, 500);
  195. },
  196. },
  197. created() {
  198. if (this.multiple != null) {
  199. this.selectValue = this.value;
  200. } else {
  201. if (this.value != null) {
  202. this.selectValue = this.value + "";
  203. }
  204. }
  205. },
  206. mounted() {
  207. if (this.requestUrl == null) {
  208. this.options = this.localOptions;
  209. return;
  210. }
  211. this.queryData();
  212. },
  213. methods: {
  214. // 判断选择是否已经禁用
  215. isDisabledOption(option) {
  216. if (
  217. option == null ||
  218. this.disabledOptions == null ||
  219. this.disabledOptions.length == 0
  220. ) {
  221. return false;
  222. }
  223. let currentOptionVal = option[this.option];
  224. return this.disabledOptions.indexOf(currentOptionVal) >= 0;
  225. },
  226. change(value) {
  227. if (value === "") {
  228. value = "";
  229. }
  230. this.$emit("input", value);
  231. // 根据当前值,找出对应的选项
  232. let optionItem = this.options.find((item) => item[this.option] == value);
  233. this.$emit("change", value, optionItem);
  234. },
  235. // 根据用户配置的label,生成对应的标签
  236. getItemLabel(item, label) {
  237. if (label.indexOf("${") < 0 && label.indexOf("}" < 0)) {
  238. return item[label];
  239. }
  240. let reg = /\$\{[a-zA-Z0-9]*\}/g;
  241. let list = label.match(reg);
  242. // ["${id}", "${text}"]
  243. let result = label;
  244. for (let i = 0; i < list.length; i++) {
  245. let sub = list[i];
  246. let key = sub.replace("${", "").replace("}", "");
  247. result = result.replace(sub, item[key]);
  248. }
  249. return result;
  250. },
  251. // 从本地localStorage取 AJReportDict
  252. getOptionsFromLocalStorage() {
  253. let dicts = JSON.parse(localStorage.getItem("AJReportDict"));
  254. let options = [];
  255. if (!dicts.hasOwnProperty(this.dictCode)) {
  256. return [];
  257. }
  258. let dictItems = dicts[this.dictCode];
  259. for (let i = 0; i < dictItems.length; i++) {
  260. let dictItem = dictItems[i];
  261. options.push({
  262. id: dictItem.id.toString(),
  263. text: dictItem.text,
  264. extend: dictItem.extend,
  265. });
  266. }
  267. return options;
  268. },
  269. queryData() {
  270. // 所有从本地localStorage取,因为在App.vue中已经请求远程保存到本地了
  271. let options = this.getOptionsFromLocalStorage();
  272. if (this.isNotBlank(options)) {
  273. this.options = options;
  274. return;
  275. }
  276. // 本地localStorage取不到,再从远程接口取
  277. if (this.requestUrl == null) {
  278. return;
  279. }
  280. if (
  281. this.method != null &&
  282. this.method.toLocaleLowerCase().trim() == "post"
  283. ) {
  284. this.queryDataByPost();
  285. } else {
  286. this.queryDataByGet();
  287. }
  288. },
  289. queryDataByGet(keyword) {
  290. let param = this.deepClone(this.queryParam);
  291. if (this.isNotBlank(keyword)) {
  292. param["keyword"] = keyword;
  293. }
  294. param["multiple"] = this.multiple == null ? null : 1;
  295. request({
  296. url: this.requestUrl,
  297. headers: { noPrompt: true },
  298. params: param,
  299. }).then((response) => {
  300. this.setOptions(response.data);
  301. });
  302. },
  303. queryDataByPost(keyword) {
  304. let param = this.deepClone(this.queryParam);
  305. if (this.isNotBlank(keyword)) {
  306. param["keyword"] = keyword;
  307. }
  308. request({
  309. url: this.requestUrl,
  310. method: "post",
  311. headers: { noPrompt: true },
  312. data: param,
  313. }).then((response) => {
  314. this.setOptions(response.data);
  315. });
  316. },
  317. setOptions(resData) {
  318. if (resData == null || resData.length == 0) {
  319. this.options = [];
  320. this.totalPage = 0;
  321. return;
  322. }
  323. if (this.isArray(resData)) {
  324. this.options = resData;
  325. this.totalPage = 1;
  326. return;
  327. }
  328. if (
  329. resData.records == null ||
  330. resData.total == null ||
  331. resData.pages == null
  332. ) {
  333. this.options = [];
  334. return;
  335. }
  336. this.totalPage = resData.pages;
  337. this.options = resData.records;
  338. },
  339. remoteQuery(keyword) {
  340. setTimeout(() => {
  341. if (
  342. this.method != null &&
  343. this.method.toLocaleLowerCase().trim() == "post"
  344. ) {
  345. this.queryDataByPost(keyword);
  346. } else {
  347. this.queryDataByGet(keyword);
  348. }
  349. }, 200);
  350. },
  351. },
  352. };
  353. </script>
  354. <style lang="scss" scoped>
  355. .el-select {
  356. width: 100%;
  357. }
  358. .el-select-dropdown__item.selected {
  359. text-align: center;
  360. }
  361. .el-select-dropdown__item.is-disabled {
  362. text-align: center;
  363. }
  364. </style>