index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <script>
  2. import { REFER } from "./api/index";
  3. export default {
  4. name: "PopoverTreeSelect",
  5. props: {
  6. // v-model
  7. value: {
  8. type: [Array, String],
  9. require: true,
  10. },
  11. // 参照类型 ,对应后端
  12. referName: {
  13. type: String,
  14. require: true,
  15. },
  16. // 作为 value 唯一标识的键名,绑定值
  17. valueKey: {
  18. type: String,
  19. dafault: () => {
  20. return "code";
  21. },
  22. },
  23. // 默认查询参数
  24. queryParams: {
  25. type: Function,
  26. default: () => {},
  27. },
  28. // 需映射源数据
  29. source: {
  30. type: Object,
  31. default: () => ({}),
  32. },
  33. // 参照内外映射
  34. dataMapping: {
  35. type: Object,
  36. default: () => ({}),
  37. },
  38. defaultProps: {
  39. type: Object,
  40. default: () => {
  41. return {
  42. label: "name",
  43. children: "children",
  44. };
  45. },
  46. },
  47. onlyFinal: {
  48. type: Boolean,
  49. default: false,
  50. },
  51. },
  52. components: {},
  53. data() {
  54. return {
  55. size: "mini",
  56. width: "50%",
  57. visible: false,
  58. loading: false,
  59. model: {
  60. search: "",
  61. isPage: false,
  62. },
  63. isSure: true,
  64. data: [],
  65. selectData: [],
  66. // defaultProps: {
  67. // label: "name",
  68. // children: "children",
  69. // },
  70. };
  71. },
  72. computed: {
  73. innerValue: {
  74. get() {
  75. return this.value;
  76. },
  77. set(value) {
  78. this.$emit("input", value);
  79. },
  80. },
  81. },
  82. watch: {
  83. innerValue: {
  84. handler: function (newValue) {
  85. if (!newValue) {
  86. const {
  87. $props: { source, dataMapping },
  88. } = this;
  89. for (let key in dataMapping) {
  90. source[key] = undefined;
  91. }
  92. this.$emit("update:source", source);
  93. }
  94. },
  95. },
  96. // "model.search": {
  97. // handler: function (newValue) {
  98. // this.$refs.tree.filter(newValue);
  99. // },
  100. // },
  101. },
  102. methods: {
  103. // open dialog
  104. async open() {
  105. this.visible = true;
  106. await this.useReset();
  107. },
  108. // hide dialog
  109. async hide() {
  110. this.visible = false;
  111. },
  112. // fetch list
  113. async fetchList(prop) {
  114. try {
  115. this.loading = true;
  116. const { referName: type, source, queryParams } = this.$props;
  117. const { code, rows } = await REFER({
  118. ...prop,
  119. ...queryParams(source),
  120. type: type,
  121. });
  122. if (code === 200) {
  123. this.data = rows;
  124. }
  125. } catch (err) {
  126. // catch
  127. console.error(err);
  128. } finally {
  129. // finally
  130. this.loading = false;
  131. }
  132. },
  133. // reset list
  134. async useReset() {
  135. this.data = [];
  136. this.model.search = null;
  137. await this.fetchList(this.model);
  138. },
  139. // query list
  140. async useQuery(value) {
  141. await this.$refs.tree.filter(value);
  142. // await this.fetchList(this.model);
  143. },
  144. // auto
  145. async useAutocomplete(prop, cb) {
  146. if (prop) {
  147. this.model.search = prop;
  148. await this.fetchList(this.model);
  149. await cb(this.data);
  150. } else {
  151. cb([]);
  152. }
  153. },
  154. // select
  155. useSelect(prop) {
  156. if (this.$props.onlyFinal) {
  157. this.isSure = prop.children ? false : true;
  158. }
  159. this.selectData = [prop];
  160. },
  161. // filter
  162. useTreeFilter(value, data) {
  163. if (!value) return true;
  164. return data.name.indexOf(value) !== -1;
  165. },
  166. // confirm
  167. useConfirm(prop) {
  168. if (this.isSure) {
  169. this.hide();
  170. const {
  171. $props: { source, valueKey, dataMapping },
  172. } = this;
  173. for (let key in dataMapping) {
  174. source[key] = prop[0][dataMapping[key]];
  175. }
  176. this.innerValue = prop[0][valueKey];
  177. console.log(valueKey, "valueKey");
  178. console.log(prop[0][valueKey], "prop[0][valueKey]");
  179. console.log(this.innerValue, "innerValue");
  180. console.log(source, "source");
  181. this.$emit("update:source", source);
  182. this.$emit("change", prop, this.$props);
  183. } else {
  184. this.$message.warning({
  185. message: "请选择最末级节点",
  186. });
  187. }
  188. },
  189. },
  190. created() {},
  191. mounted() {},
  192. destroyed() {},
  193. };
  194. </script>
  195. <template>
  196. <div class="popover-tree-select">
  197. <el-autocomplete
  198. v-bind="$attrs"
  199. v-model="innerValue"
  200. :value-key="valueKey"
  201. :fetch-suggestions="useAutocomplete"
  202. @select="useConfirm"
  203. style="width: 100%"
  204. >
  205. <i class="el-icon-search" slot="suffix" @click="open"> </i>
  206. <template slot-scope="{ item }">
  207. <p
  208. style="
  209. text-overflow: ellipsis;
  210. overflow: hidden;
  211. line-height: 15px;
  212. margin: 5px 0;
  213. "
  214. >
  215. {{ item.name }}
  216. </p>
  217. <p
  218. style="
  219. font-size: 12px;
  220. color: #b4b4b4;
  221. line-height: 15px;
  222. margin: 5px 0;
  223. "
  224. >
  225. {{ item.code }}
  226. </p>
  227. </template>
  228. </el-autocomplete>
  229. <el-dialog
  230. :width="width"
  231. :show-close="false"
  232. :visible.sync="visible"
  233. :close-on-click-modal="false"
  234. :close-on-press-escape="false"
  235. append-to-body
  236. style="padding: 10px"
  237. >
  238. <div slot="footer">
  239. <el-button
  240. type="primary"
  241. :size="$attrs.size"
  242. :loading="loading"
  243. @click="useConfirm(selectData)"
  244. >确 认</el-button
  245. >
  246. <el-button :size="$attrs.size" :loading="loading" @click="hide"
  247. >取 消</el-button
  248. >
  249. </div>
  250. <el-form
  251. v-loading="loading"
  252. :size="size"
  253. :inline="true"
  254. :model="model"
  255. @submit.native.prevent
  256. >
  257. <el-form-item prop="search">
  258. <el-input v-model="model.search" @change="useQuery"> </el-input>
  259. </el-form-item>
  260. <el-form-item>
  261. <el-button icon="el-icon-refresh" @click="useReset"></el-button>
  262. </el-form-item>
  263. <div style="height: 400px; overflow: auto">
  264. <el-tree
  265. v-loading="loading"
  266. :data="data"
  267. :props="defaultProps"
  268. :filter-node-method="useTreeFilter"
  269. ref="tree"
  270. accordion
  271. node-key="id"
  272. @node-click="useSelect"
  273. >
  274. </el-tree>
  275. </div>
  276. </el-form>
  277. </el-dialog>
  278. </div>
  279. </template>
  280. <style scoped></style>