移动端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

198 lignes
5.0 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. 'year','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. year: function (newVal, oldVal) {
  34. //console.log(newVal)
  35. if(newVal != this.yearNews)
  36. {
  37. this.yearNews = newVal;
  38. this.getSubjects();
  39. }
  40. },
  41. visible: function(newVal, oldVal) {
  42. if(this.popupVisible !== newVal)
  43. {
  44. this.popupVisible = newVal;
  45. if(newVal)
  46. this.syncIndex();
  47. }
  48. },
  49. popupVisible: function(newVal, oldVal) {
  50. if(newVal !== this.visible)
  51. this.$emit('update:visible', newVal);
  52. },
  53. },
  54. created() {
  55. this.getSubjects();
  56. },
  57. data() {
  58. return {
  59. popupVisible: false,
  60. internalValue: this.value,
  61. yearNews: this.year,
  62. searchValue: '',
  63. active: '1',
  64. subjects: [],
  65. };
  66. },
  67. methods: {
  68. getSubjects() {
  69. this.subjects = [];
  70. let map = {};
  71. this.getDicts('subject_type').then((resp) => {
  72. this.subjects = resp.data.map((x) => {
  73. let item = {
  74. subjectId: x.dictValue,
  75. subjectName: x.dictLabel,
  76. subjectNameAll: x.dictLabel,
  77. subjectType: x.dictValue,
  78. subjectLevel: 0,
  79. children: [],
  80. visible: true,
  81. };
  82. map[x.dictValue] = item;
  83. return item;
  84. });
  85. let url = '/finance/subject/listAll?year='+this.yearNews; // '/open/villageAffairs/public/subjects/153'
  86. request(url).then((resp) => {
  87. let list = this.makeTree(resp.rows || resp.data);
  88. for(let v of list)
  89. {
  90. if(v.subjectId.length === 3)
  91. {
  92. map[v.subjectType].children.push(v);
  93. }
  94. }
  95. })
  96. });
  97. },
  98. onItemClicked(subject) {
  99. if(this.canSelectNonLeaf || subject.is_last === 'Y')
  100. {
  101. this.internalValue = subject.subjectId
  102. this.$emit('input', subject.subjectId);
  103. this.$emit('select', subject);
  104. this.close();
  105. }
  106. },
  107. onItemToggle({subject, on}) {
  108. },
  109. onCancel() {
  110. this.close();
  111. },
  112. makeTree(list) {
  113. function isnull(p) {
  114. return p === null || p === undefined || p === '';
  115. }
  116. function makeTree_r(l, p) {
  117. const isRoot = isnull(p);
  118. let res = [];
  119. for(let v of l)
  120. {
  121. if((isRoot && isnull(v.parentId)) || (!isRoot && v.parentId == p))
  122. {
  123. let arr = makeTree_r(l, v.subjectId);
  124. if(arr.length > 0)
  125. v.children = arr;
  126. else
  127. delete v.children;
  128. res.push(v);
  129. }
  130. }
  131. return res;
  132. }
  133. return makeTree_r(list.map((x) => {
  134. x.visible = true;
  135. return x;
  136. }));
  137. },
  138. onSearch(value) {
  139. function handleTree_r(l, func) {
  140. let res = 0;
  141. for(let v of l)
  142. {
  143. let r = func(v) ? 1 : 0;
  144. if(v.children && Array.isArray(v.children) && v.children.length > 0)
  145. {
  146. r += handleTree_r(v.children, func);
  147. }
  148. v.visible = r > 0;
  149. res += r;
  150. }
  151. return res;
  152. }
  153. for(let v of this.subjects)
  154. {
  155. if(v.subjectId === this.active && value)
  156. {
  157. handleTree_r(v.children, (x) => x.subjectId.startsWith(value) || x.subjectName.indexOf(value) !== -1);
  158. }
  159. else
  160. {
  161. handleTree_r(v.children, (x) => true);
  162. }
  163. }
  164. },
  165. onTabChanged() {
  166. this.searchValue = '';
  167. this.onSearch();
  168. },
  169. folderAll() { // TODO: not work
  170. for(let i in this.subjects)
  171. {
  172. if(this.$refs['tree' + i])
  173. this.$refs['tree' + i][0].folderAll();
  174. }
  175. },
  176. close() {
  177. this.popupVisible = false;
  178. this.folderAll();
  179. this.searchValue = '';
  180. this.active = '1';
  181. this.$emit('cancel');
  182. },
  183. syncIndex() {
  184. if(this.subjects.length === 0 || !this.internalValue)
  185. return;
  186. this.active = this.internalValue[0];
  187. },
  188. },
  189. }
  190. </script>
  191. <style scoped>
  192. </style>