Cascader.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div class="Cascader">
  3. <el-cascader
  4. :options="options"
  5. :props="props"
  6. @expand-change="$expandChange"
  7. @change="$change"
  8. style="width:335px;"
  9. :placeholder="cascaderPlaceholder"
  10. >
  11. <template slot-scope="{ node, data }">
  12. <span>{{ data[props.label] }}</span>
  13. <span v-if="!node.isLeaf">({{ data[props.children].length }})</span>
  14. </template>
  15. </el-cascader>
  16. </div>
  17. </template>
  18. <script>
  19. import { mapActions } from "vuex";
  20. /* 描述说明:
  21. options: 级联选择 树形数据
  22. props:配置项 (可看 https://element.eleme.cn/#/zh-CN/component/cascader )
  23. */
  24. export default {
  25. name: "Cascader",
  26. props: {
  27. options: { type: Array },
  28. props: { type: Object }
  29. },
  30. data() {
  31. return {
  32. cascaderPlaceholder: "请选择/不选择默认根级"
  33. };
  34. },
  35. created() {},
  36. methods: {
  37. ...mapActions(["fetch"]),
  38. $expandChange(value) {
  39. this.$emit("expand-change", value);
  40. this.cascaderPlaceholder = this.findAreaName(value);
  41. this.$children[0].presentText = this.cascaderPlaceholder;
  42. },
  43. $change(value) {
  44. this.$emit("change", value);
  45. },
  46. // 级联选择时 因组件限制 需拼接 名字字符串在级联选择器上显示
  47. findAreaName(valueArr) {
  48. let _this = this;
  49. let i = 0;
  50. let tmpStr = "";
  51. find(this.options);
  52. function find(subTree) {
  53. subTree.forEach(item => {
  54. // debugger
  55. if (item.id == valueArr[i]) {
  56. tmpStr += item[_this.props.label] + "/";
  57. if (
  58. i < valueArr.length &&
  59. item[_this.props.children] &&
  60. item[_this.props.children].length > 0
  61. ) {
  62. i++;
  63. find(item[_this.props.children]);
  64. }
  65. }
  66. });
  67. }
  68. return tmpStr;
  69. }
  70. }
  71. };
  72. </script>