codeSelect.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* * 使用方式
  2. <code-select
  3. v-model="params.enableFlag"
  4. dictname="ENABLE_FLAG"
  5. @changed="handler"
  6. placeholder="启用状态"
  7. style="width: 120px;"
  8. />
  9. * 根据/data/basecode.js中字典值,生成下拉列表 * @property dictname ENABLE_FLAG *
  10. @property placeholder * @property style */
  11. <template>
  12. <el-select
  13. :name="inputName"
  14. v-model="selectValue"
  15. :disabled="disabled"
  16. :placeholder="placeholder"
  17. :style="mystyle"
  18. clearable
  19. class="filter-item code-selected"
  20. @change="selectChange"
  21. @visible-change="drowShow"
  22. >
  23. <el-option
  24. v-for="(item, index) in optionList"
  25. :key="index"
  26. :label="item[label]"
  27. :value="item[valWord]"
  28. />
  29. </el-select>
  30. </template>
  31. <script>
  32. import request from "@/api/axios";
  33. import { getStorageItem } from "@/utils/storage";
  34. export default {
  35. props: {
  36. value: {
  37. type: [String, Number],
  38. default: ""
  39. },
  40. //用localStrage中的basecode生成下拉
  41. dictname: {
  42. type: String,
  43. default: ""
  44. },
  45. //远程请求,生成下拉
  46. remoteurl: {
  47. type: String,
  48. default: ""
  49. },
  50. remoteParams: {
  51. type: Object,
  52. default: () => {}
  53. },
  54. inputName: {
  55. type: String,
  56. default: ""
  57. },
  58. placeholder: {
  59. type: String,
  60. default: "请选择"
  61. },
  62. mystyle: {
  63. type: String,
  64. default: "width: 100px"
  65. },
  66. disabled: {
  67. type: Boolean,
  68. default: false
  69. },
  70. label: {
  71. type: String,
  72. default: "label"
  73. },
  74. valWord: {
  75. type: String,
  76. default: "value"
  77. }
  78. },
  79. data() {
  80. return {
  81. selectValue: "",
  82. optionList: [],
  83. dictList: []
  84. };
  85. },
  86. watch: {
  87. value: {
  88. handler(newValue, oldValue) {
  89. if (typeof newValue === "string") {
  90. this.selectValue = newValue;
  91. } else {
  92. this.selectValue = this.parseString(newValue);
  93. }
  94. },
  95. immediate: true
  96. }
  97. },
  98. computed: {},
  99. created() {
  100. if (this.dictname !== "") {
  101. this.optionList = this.getListFromBaseCode();
  102. }
  103. if (this.remoteurl !== "") {
  104. this.getListFromAjax();
  105. }
  106. },
  107. mounted() {},
  108. methods: {
  109. getListFromBaseCode() {
  110. let basecode = getStorageItem("queryForCodeSelect");
  111. let list = [];
  112. if (!basecode.hasOwnProperty(this.dictname)) {
  113. return [];
  114. }
  115. this.dictList = basecode[this.dictname];
  116. for (let i = 0; i < this.dictList.length; i++) {
  117. let codeItem = this.dictList[i];
  118. list.push({ value: codeItem.value.toString(), label: codeItem.label });
  119. }
  120. return list;
  121. },
  122. getListFromAjax() {
  123. //将url中的参数转换成json,提交
  124. let reqData = {};
  125. /*let params = this.remoteurl.slice(this.remoteurl.indexOf('?') + 1).split('&');
  126. for (let i = 0; i < params.length; i++) {
  127. let map = params[i].split('=');
  128. let key = map[0];
  129. let val = map[1];
  130. if(','.indexOf(val)>0 && val.split(',').length>1){
  131. val = val.split(',');
  132. }
  133. reqData[map[0]] = val;
  134. }*/
  135. if (this.remoteParams != null) {
  136. reqData = this.remoteParams;
  137. }
  138. request({
  139. url: this.remoteurl,
  140. method: "post",
  141. data: reqData
  142. }).then(response => {
  143. if (response.repCode == "0000") {
  144. this.optionList = response.repData;
  145. }
  146. });
  147. },
  148. selectChange(val) {
  149. this.$emit("input", val);
  150. let list = this.optionList;
  151. for (let i in list) {
  152. let item = list[i];
  153. if (item[this.valWord] === val) {
  154. this.$emit("changed", item);
  155. break;
  156. }
  157. }
  158. },
  159. drowShow(val) {
  160. this.$emit("show", val);
  161. this.$emit("click", val);
  162. }
  163. }
  164. };
  165. </script>
  166. <!--<style rel="stylesheet/scss" lang="less" scoped>-->
  167. <!--</style>-->