【VUE2,通过回调、插槽、实现父子组件的高度解耦和高度通信】

文章介绍了如何在Vue应用中使用模板和组件,如ResponsiveDescriptions和PlanLeafList,展示项目预算差异数据,包括预算名称、金额、实际发生数和差异分析。组件间通过props和回调函数进行数据交互和加载细节数据。
摘要由CSDN通过智能技术生成

在这里插入图片描述

budgtDiff代码:

<template>
  <el-row>
    <PlanLeafList v-slot="{data}" :plan-id="$route.params.id" :load="load">
      <el-row>
        <ResponsiveDescriptions v-if="data.projectApplication" colon :xs="1" :sm="2" border>
          <el-descriptions-item label="执行机构">{{
            data.projectApplication.dept ? data.projectApplication.dept.name : '无'
          }}
          </el-descriptions-item>
          <el-descriptions-item label="项目编号">{{
            data.projectApplication.projectCode ? data.projectApplication.projectCode
            : '无'
          }}
          </el-descriptions-item>
          <el-descriptions-item label="项目名称">{{
            data.projectApplication.projectName ? data.projectApplication.projectName
            : '无'
          }}
          </el-descriptions-item>
          <el-descriptions-item label="项目所处阶段">{{
            data.projectApplication ?
              (data.projectApplication.planProjec ? dict.label.project_plan_status[data.projectApplication.planProject.planStatus] : '无') : '无'
          }}
          </el-descriptions-item>
        </ResponsiveDescriptions>
        <el-table
          border
          :data="data.budgetExpenditureDiffUnitList"
          row-key="id"
          :cell-class-name="getCellStyle"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :cell-style="{textAlign:'center'}"
          :header-cell-style="{textAlign:'center'}"
          default-expand-all
        >
          <el-table-column label="预算名称" prop="budgetName" width="120">
            <template slot-scope="scope">
              {{ scope.row.budgetName }}
            </template>

          </el-table-column>
          <el-table-column label="预算金额" prop="budgetAmount" width="100" align="right">
            <template #default="scope">
              {{ scope.row.budgetAmount ? scope.row.budgetAmount.toFixed(2) : '' }}
            </template>
          </el-table-column>
          <el-table-column label="实际发生数(元)" prop="expenditureAmount" width="120" align="right">
            <template #default="scope">
              {{ scope.row.expenditureAmount ? scope.row.expenditureAmount.toFixed(2) : '' }}
            </template>
          </el-table-column>
          <el-table-column label="预算与实际差异(预算-实际)(元)" align="right">
            <template #default="scope">
              {{
                scope.row.budgetAmount && scope.row.expenditureAmount ? (scope.row.budgetAmount - scope.row.expenditureAmount).toFixed(2) : ''
              }}
            </template>
          </el-table-column>
          <el-table-column label="完成率 实际/预算*%" align="right">
            <template #default="scope">
              {{
                scope.row.budgetAmount && scope.row.expenditureAmount ? `${(scope.row.expenditureAmount / scope.row.budgetAmount).toFixed(2)}%` : ''
              }}
            </template>
          </el-table-column>
          <!--        <el-table-column label="差异说明" prop="reason" />-->
        </el-table>
      </el-row>
    </PlanLeafList>
  </el-row>
</template>
<script>
import { initData } from '@/api/data'
import ResponsiveDescriptions from '@/components/ResponsiveDescriptions/index.vue'
import PlanLeafList from '@/views/project/statistics/planLeafList.vue'

export default {
  name: 'ProjectBudgetDiff',
  components: { PlanLeafList, ResponsiveDescriptions },
  methods: {
    getCellStyle({ row, column, rowIndex, columnIndex }) {
      console.log('ssss')
      if (columnIndex === 2) {
        return 'cell-budget'
      }
      if (columnIndex === 3) {
        return 'cell-actual'
      }
      if (columnIndex === 4) {
        return 'cell-diff'
      }
      return null
    },
    load(id, callback) {
      console.log('load ', id, callback)
      initData(`api/analysis/project/budget`, {
        id: id
      }).then(result => {
        callback(result)
      }).catch(() => {
        callback(null)
      })
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .cell-budget {
  background: green;
  color: black;
}

::v-deep .cell-actual {
  background: yellow;
  color: black;
}

::v-deep .cell-diff {

}
</style>

planLeafList代码:

<template>
  <el-row v-loading="loading">
    <el-empty v-if="list.length===0" :image-size="200" />
    <ExpandCard
      v-for="(item,index) in list"
      :key="index"
      v-loading="item.loading"
      :header="item.data.projectName"
      :lazy-load="() => lazyLoad(item)"
    >
      <slot v-if="item.detail" :data="item.detail" />
    </ExpandCard>
  </el-row>
</template>

<script>
import { initData } from '@/api/data'
import ExpandCard from '@/components/ExpandCard/index.vue'

export default {
  name: 'PlanLeafList',
  components: { ExpandCard },
  props: {
    planId: String,
    load: Function
  },
  data() {
    return {
      loading: false,
      list: [],
      states: []
    }
  },
  created() {
    this.loading = true
    initData(`api/project/plan/leaf`, {
      id: this.planId
    }).then(result => {
      this.list = result.map(value => {
        return {
          loading: false,
          data: value,
          detail: null
        }
      })
    }).finally(() => {
      this.loading = false
    })
  },
  methods: {
    lazyLoad(item) {
      if (this.load) {
        const loadFn = this.load
        item.loading = true // 下拉框点击时,触发这个方法,这个方法调用了从父组件传来的方法(是可以由父组件自定义的方法),下一个方法就是使用从父组件传过来的方法,然后通过回调,可以从
        loadFn(item.data.id, (data) => {
          item.detail = data
          item.loading = false
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.el-card {
  margin-bottom: 20px ;
  margin-left: 20px;
  margin-right: 20px;

  &:first-child {
    margin-top: 20px;
  }
}
</style>

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值