anji-checkbox.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!--
  2. * @Descripttion:
  3. * @Author: qianlishi
  4. * @Date: 2021-12-13 10:42:24
  5. * @Last Modified by: qianlishi
  6. * @Last Modified time: 2022-5-4 10:04:24
  7. !-->
  8. <template>
  9. <el-checkbox-group v-model="selectValue" class="filter-item" @change="change">
  10. <el-checkbox
  11. v-for="item in options"
  12. :key="item.id"
  13. :label="item.id"
  14. :disabled="disabled"
  15. >{{ item.text }}</el-checkbox
  16. >
  17. </el-checkbox-group>
  18. </template>
  19. <script>
  20. import request from "@/utils/request";
  21. export default {
  22. props: {
  23. dictCode: null, // 当传入dictCode时,可以不用传递url
  24. url: null,
  25. value: null,
  26. placeholder: null,
  27. label: {
  28. type: String,
  29. default: "text"
  30. },
  31. option: {
  32. type: String,
  33. default: "id"
  34. },
  35. multiple: null,
  36. localOptions: null,
  37. disabled: null,
  38. clearable: {
  39. type: Boolean,
  40. default: true
  41. },
  42. collapseTags: {
  43. type: Boolean,
  44. default: false
  45. }
  46. },
  47. data() {
  48. return {
  49. options: null,
  50. selectValue: []
  51. };
  52. },
  53. computed: {
  54. // 根据dictCode和url拼出最终的请求url
  55. requestUrl() {
  56. if (this.url != null && this.url.trim() != "") {
  57. return this.url;
  58. }
  59. if (this.dictCode != null && this.dictCode.trim() != "") {
  60. return `/tms/gaeaDict/select/${this.dictCode}`;
  61. }
  62. return null;
  63. }
  64. },
  65. watch: {
  66. value: function(val, oldVal) {
  67. this.echoCheckboxValue(val);
  68. }
  69. },
  70. mounted() {
  71. this.echoCheckboxValue(this.value);
  72. if (this.requestUrl == null) {
  73. this.options = this.localOptions;
  74. return;
  75. }
  76. this.queryData();
  77. },
  78. methods: {
  79. change(value) {
  80. const strValue = value.join(",");
  81. if (value === "") {
  82. value = null;
  83. }
  84. this.$emit("input", strValue);
  85. this.$emit("change", strValue, this.options);
  86. },
  87. // 从本地localStorage取 gaeaDict
  88. getOptionsFromLocalStorage() {
  89. let dicts = JSON.parse(localStorage.getItem("AJReportDict"));
  90. let options = [];
  91. if (!dicts.hasOwnProperty(this.dictCode)) {
  92. return [];
  93. }
  94. let dictItems = dicts[this.dictCode];
  95. for (let i = 0; i < dictItems.length; i++) {
  96. let dictItem = dictItems[i];
  97. options.push({ id: dictItem.id.toString(), text: dictItem.text });
  98. }
  99. return options;
  100. },
  101. queryData() {
  102. // 所有从本地localStorage取,因为在App.vue中已经请求远程保存到本地了
  103. let options = this.getOptionsFromLocalStorage();
  104. if (this.isNotBlank(options)) {
  105. this.options = options;
  106. return;
  107. }
  108. // 本地localStorage取不到,再从远程接口取
  109. if (this.requestUrl == null) {
  110. return;
  111. }
  112. request({
  113. url: this.requestUrl,
  114. params: {
  115. multiple: this.multiple == null ? null : 1
  116. }
  117. }).then(response => {
  118. this.options = response.data;
  119. });
  120. },
  121. // 回显
  122. echoCheckboxValue(val) {
  123. if (!val) {
  124. this.selectValue = [];
  125. } else {
  126. const arr = val.split(",");
  127. this.selectValue = arr;
  128. }
  129. }
  130. }
  131. };
  132. </script>