移动端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

189 linhas
4.8 KiB

  1. <!-- 会计科目选择弹出层组件 zhao -->
  2. <template>
  3. <van-popup v-model="popupVisible" position="bottom" :style="{'height': height || 'unset',}">
  4. <van-search
  5. v-model="searchValue"
  6. placeholder="按科目编码/名称筛选"
  7. @input="onSearch"
  8. />
  9. <van-tabs v-model="active" @change="onTabChanged">
  10. <van-tab :title="item.subjectName" :name="item.subjectId" v-for="(item, index) in subjects" :key="index">
  11. <SubjectTreeChooserNodeItem :ref="'tree' + index" :subjects="item.children" @clicked="onItemClicked" :can-select-non-leaf="canSelectNonLeaf"></SubjectTreeChooserNodeItem>
  12. </van-tab>
  13. </van-tabs>
  14. </van-popup>
  15. </template>
  16. <script>
  17. import request from "@/utils/request";
  18. import SubjectTreeChooserNodeItem from "./SubjectTreeChooserNodeItem";
  19. export default {
  20. name: "SubjectTreeChooser",
  21. components: {SubjectTreeChooserNodeItem},
  22. props: [
  23. 'value', 'visible', 'height', 'canSelectNonLeaf',
  24. ],
  25. watch: {
  26. value: function (newVal, oldVal) {
  27. if(newVal != this.internalValue)
  28. {
  29. this.internalValue = newVal;
  30. this.syncIndex();
  31. }
  32. },
  33. visible: function(newVal, oldVal) {
  34. if(this.popupVisible !== newVal)
  35. {
  36. this.popupVisible = newVal;
  37. if(newVal)
  38. this.syncIndex();
  39. }
  40. },
  41. popupVisible: function(newVal, oldVal) {
  42. if(newVal !== this.visible)
  43. this.$emit('update:visible', newVal);
  44. },
  45. },
  46. created() {
  47. this.getSubjects();
  48. },
  49. data() {
  50. return {
  51. popupVisible: false,
  52. internalValue: this.value,
  53. searchValue: '',
  54. active: '1',
  55. subjects: [],
  56. };
  57. },
  58. methods: {
  59. getSubjects() {
  60. this.subjects = [];
  61. let map = {};
  62. this.getDicts('subject_type').then((resp) => {
  63. this.subjects = resp.data.map((x) => {
  64. let item = {
  65. subjectId: x.dictValue,
  66. subjectName: x.dictLabel,
  67. subjectNameAll: x.dictLabel,
  68. subjectType: x.dictValue,
  69. subjectLevel: 0,
  70. children: [],
  71. visible: true,
  72. };
  73. map[x.dictValue] = item;
  74. return item;
  75. });
  76. let url = '/finance/subject/listAll'; // '/open/villageAffairs/public/subjects/153'
  77. request(url).then((resp) => {
  78. let list = this.makeTree(resp.rows || resp.data);
  79. for(let v of list)
  80. {
  81. if(v.subjectId.length === 3)
  82. {
  83. map[v.subjectType].children.push(v);
  84. }
  85. }
  86. })
  87. });
  88. },
  89. onItemClicked(subject) {
  90. if(this.canSelectNonLeaf || subject.is_last === 'Y')
  91. {
  92. this.internalValue = subject.subjectId
  93. this.$emit('input', subject.subjectId);
  94. this.$emit('select', subject);
  95. this.close();
  96. }
  97. },
  98. onItemToggle({subject, on}) {
  99. },
  100. onCancel() {
  101. this.close();
  102. },
  103. makeTree(list) {
  104. function isnull(p) {
  105. return p === null || p === undefined || p === '';
  106. }
  107. function makeTree_r(l, p) {
  108. const isRoot = isnull(p);
  109. let res = [];
  110. for(let v of l)
  111. {
  112. if((isRoot && isnull(v.parentId)) || (!isRoot && v.parentId == p))
  113. {
  114. let arr = makeTree_r(l, v.subjectId);
  115. if(arr.length > 0)
  116. v.children = arr;
  117. else
  118. delete v.children;
  119. res.push(v);
  120. }
  121. }
  122. return res;
  123. }
  124. return makeTree_r(list.map((x) => {
  125. x.visible = true;
  126. return x;
  127. }));
  128. },
  129. onSearch(value) {
  130. function handleTree_r(l, func) {
  131. let res = 0;
  132. for(let v of l)
  133. {
  134. let r = func(v) ? 1 : 0;
  135. if(v.children && Array.isArray(v.children) && v.children.length > 0)
  136. {
  137. r += handleTree_r(v.children, func);
  138. }
  139. v.visible = r > 0;
  140. res += r;
  141. }
  142. return res;
  143. }
  144. for(let v of this.subjects)
  145. {
  146. if(v.subjectId === this.active && value)
  147. {
  148. handleTree_r(v.children, (x) => x.subjectId.startsWith(value) || x.subjectName.indexOf(value) !== -1);
  149. }
  150. else
  151. {
  152. handleTree_r(v.children, (x) => true);
  153. }
  154. }
  155. },
  156. onTabChanged() {
  157. this.searchValue = '';
  158. this.onSearch();
  159. },
  160. folderAll() { // TODO: not work
  161. for(let i in this.subjects)
  162. {
  163. if(this.$refs['tree' + i])
  164. this.$refs['tree' + i][0].folderAll();
  165. }
  166. },
  167. close() {
  168. this.popupVisible = false;
  169. this.folderAll();
  170. this.searchValue = '';
  171. this.active = '1';
  172. this.$emit('cancel');
  173. },
  174. syncIndex() {
  175. if(this.subjects.length === 0 || !this.internalValue)
  176. return;
  177. this.active = this.internalValue[0];
  178. },
  179. },
  180. }
  181. </script>
  182. <style scoped>
  183. </style>