tabselect.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="nut-tabselect">
  3. <nut-popup
  4. round
  5. closeable
  6. v-model="isShow"
  7. position="bottom"
  8. :style="{ height: '457px' }"
  9. >
  10. <div class="nut-tabselect-main-title" v-html="mainTitle"></div>
  11. <nut-tab @tab-switch="tabSwitchOuter">
  12. <nut-tab-panel
  13. v-for="(value, idx) in tabList"
  14. v-bind:key="value.tabTitle"
  15. :tabTitle="value.tabTitle"
  16. >
  17. <div class="nut-tabselect-sub-title" v-html="subTitle"></div>
  18. <nut-tab
  19. @tab-switch="tabSwitchInner"
  20. positionNav="left"
  21. class="nut-tab-inner"
  22. >
  23. <nut-tab-panel
  24. v-for="(item, index) in value.children"
  25. v-bind:key="item.tabTitle"
  26. :tabTitle="item.tabTitle"
  27. >
  28. <ul>
  29. <template v-if="item.content">
  30. <li
  31. v-for="(sitem, sIndex) in item.content"
  32. v-bind:key="sitem"
  33. @click="choose(idx, index, sIndex, item, sitem)"
  34. class="nut-tab-panel-list"
  35. :class="{
  36. 'nut-tab-panel-list-active': isActive(idx, index, sIndex)
  37. }"
  38. >
  39. {{ sitem }}
  40. </li>
  41. </template>
  42. <template v-else-if="defaultContent">
  43. <li
  44. v-for="(sitem, sIndex) in defaultContent"
  45. v-bind:key="sitem"
  46. @click="choose(idx, index, sIndex, item, sitem)"
  47. class="nut-tab-panel-list"
  48. :class="{
  49. 'nut-tab-panel-list-active': isActive(idx, index, sIndex)
  50. }"
  51. >
  52. {{ sitem }}
  53. </li>
  54. </template>
  55. </ul>
  56. </nut-tab-panel>
  57. </nut-tab>
  58. </nut-tab-panel>
  59. </nut-tab>
  60. <div class="nut-tabselect-btn">
  61. <a href="javascript:;" @click="isShow = false">确定</a>
  62. </div>
  63. </nut-popup>
  64. </div>
  65. </template>
  66. <script>
  67. import nuttab from "../tab/tab.vue";
  68. import "../tab/tab.scss";
  69. import nutpop from "../popup/popup.vue";
  70. import "../popup/popup.scss";
  71. export default {
  72. name: "nut-tabselect",
  73. props: {
  74. mainTitle: {
  75. type: String,
  76. default: ""
  77. },
  78. subTitle: {
  79. type: String,
  80. default: ""
  81. },
  82. defaultContent: {
  83. type: Array,
  84. default: () => []
  85. },
  86. tabList: {
  87. type: Array,
  88. default: () => []
  89. },
  90. show: {
  91. type: Boolean,
  92. default: false
  93. },
  94. multiple: {
  95. type: Boolean,
  96. default: false
  97. },
  98. max: {
  99. type: Number,
  100. default: Infinity
  101. }
  102. },
  103. data() {
  104. return {
  105. isShow: false,
  106. level0: 0,
  107. level1: new Set([0]),
  108. level2: new Set(["0-0"]),
  109. allChoose: new Set([this.getText(0, 0, 0)])
  110. };
  111. },
  112. components: {
  113. [nuttab.name]: nuttab,
  114. [nutpop.name]: nutpop
  115. },
  116. watch: {
  117. show(val) {
  118. this.isShow = val;
  119. },
  120. isShow(val) {
  121. if (!val) {
  122. this.$emit("close");
  123. }
  124. }
  125. },
  126. mounted() {
  127. this.emit();
  128. },
  129. methods: {
  130. emit() {
  131. this.$emit(
  132. "choose",
  133. (this.tabList[this.level0] && this.tabList[this.level0].tabTitle) || "",
  134. [...this.allChoose]
  135. );
  136. },
  137. getText(idx, index, sIndex) {
  138. const tab =
  139. (this.tabList[idx] && this.tabList[idx].children[index]) || {};
  140. const subTit = tab.tabTitle;
  141. const content =
  142. (tab.content && tab.content[sIndex]) || this.defaultContent[sIndex];
  143. return subTit + " " + content;
  144. },
  145. tabSwitchOuter: function(index, event) {
  146. this.level0 = index;
  147. this.level1 = new Set([0]);
  148. this.level2 = new Set(["0-0"]);
  149. this.allChoose = new Set([this.getText(index, 0, 0)]);
  150. this.emit();
  151. },
  152. tabSwitchInner: function(index, event) {
  153. if (!this.multiple) {
  154. this.level1 = new Set([index]);
  155. } else {
  156. this.level1.add(index);
  157. }
  158. },
  159. unChoose(index, sIndex) {
  160. this.level2.delete(index + "-" + sIndex);
  161. this.level2 = new Set(this.level2);
  162. },
  163. choose(idx, index, sIndex) {
  164. if (this.multiple && this.isActive(idx, index, sIndex)) {
  165. this.unChoose(index, sIndex);
  166. this.allChoose.delete(this.getText(idx, index, sIndex));
  167. this.emit();
  168. return;
  169. }
  170. if (!this.multiple) {
  171. this.level2 = new Set([index + "-" + sIndex]);
  172. this.allChoose = new Set([this.getText(index, 0, 0)]);
  173. } else {
  174. if (this.max !== Infinity && this.max === this.level2.size) {
  175. return;
  176. }
  177. this.level2 = new Set([...this.level2.add(index + "-" + sIndex)]);
  178. this.allChoose.add(this.getText(idx, index, sIndex));
  179. }
  180. this.emit();
  181. },
  182. isActive(idx, index, sIndex) {
  183. if (
  184. idx === this.level0 &&
  185. this.level1.has(index) &&
  186. this.level2.has(index + "-" + sIndex)
  187. ) {
  188. return true;
  189. }
  190. return false;
  191. }
  192. }
  193. };
  194. </script>