tree.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view>
  3. <cu-custom bgColor="bg-blue" :isBack="true">
  4. <block slot="backText"></block>
  5. <block slot="content">选择关联工程部位</block>
  6. <block slot="right">
  7. <text @click="preservation">保存</text>
  8. </block>
  9. </cu-custom>
  10. <view class="hc-tree-box">
  11. <ly-tree ref="ptree" lazy showCheckbox checkStrictly accordion
  12. nodeKey="id" :load="getTreeData"
  13. :defaultCheckedKeys="defaultChecked"
  14. :defaultExpandedKeys="defaultExpanded"
  15. />
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. project: {},
  24. userid: '',
  25. defaultExpanded: [],//默认展开
  26. defaultChecked: [],//默认选中
  27. }
  28. },
  29. onLoad() {
  30. const {id} = uni.getStorageSync("userInfo")
  31. this.project = uni.getStorageSync(`porject_${id}`);
  32. this.userid = id;
  33. this.setTreeChecked();
  34. },
  35. methods: {
  36. setTreeChecked() {
  37. this.defaultChecked = [];
  38. let selects = getApp().globalData.relationId;
  39. if (selects) {
  40. let relationIds = selects ? selects.split(',') : []
  41. this.defaultChecked = relationIds;
  42. this.$refs?.ptree?.setCheckedKeys(relationIds)
  43. }
  44. },
  45. async getTreeData(node, resolve) {
  46. let form = {type: 0, id: ''};
  47. if (node.level === 0) {
  48. form = {type: 0, id: ''};
  49. } else {
  50. form.id = (node && node.data) ? node.data.id : '';
  51. }
  52. //发起请求
  53. this.http.request('/app/qualityModifyInfo/getDataByparantId', {
  54. ...form,
  55. projectId: this.project.id,
  56. contractId: this.project.contractId,
  57. }).then((result) => {
  58. if (result.datas != null) {
  59. resolve(result.datas);
  60. } else {
  61. resolve([]);
  62. }
  63. })
  64. },
  65. //设置保存
  66. preservation() {
  67. uni.showLoading({title: '处理数据中...', mask: true})
  68. let relation = '', relationId = ''
  69. const nodes = this.$refs?.ptree?.getCheckedNodes()
  70. for (let i = 0; i < nodes.length; i++) {
  71. const { id, name } = nodes[i]
  72. relation = relation ? relation + ',' + name : name;
  73. relationId = relationId ? relationId + ',' + id : id;
  74. }
  75. //返回数据
  76. uni.$emit("relation", {
  77. relation,
  78. relationId,
  79. })
  80. uni.hideLoading()
  81. //返回上级
  82. uni.navigateBack({
  83. delta:1
  84. })
  85. },
  86. }
  87. }
  88. </script>
  89. <style>
  90. page{
  91. background-color: white;
  92. }
  93. .hc-tree-box {
  94. position: relative;
  95. overflow: auto;
  96. }
  97. </style>