day 10 完结 ,增加统计金额

day 10 完结 ,增加统计金额

本节目标:增加统计金额

0901

  • 1.画页面

  • src\components\AmountBox.js:

  • import React from "react";
    /* 
    内插变量
    */
    const AmountBox = ({ text ,type}) => {
      return (
        <div className="col">
          <div className="card">
            {/* <div className="card-header bg-success text-white" > */}
            <div className={`card-header bg-${type} text-white`}>
              {text}
            </div>
            <div className="card-body">78</div>
          </div>
        </div>
      );
    };
    
    export default AmountBox;
    
    
  • src\components\Records.js

import React, { Component } from "react";
...
import RecordForm from "./RecordForm";
import AmountBox from "./AmountBox";

class Records extends Component {
  constructor() {...}

  componentDidMount() {...}
  addRecord(record) {...}
  updateRecord(record,data){...}
  deleteRecord(record){...}
  render() {...}
    return (
      <div>
        <h2>Records</h2>
        <div className="row mb-3">
          <AmountBox text="Credit" type="success"/>
          <AmountBox text="Debit" type="danger"/>
          <AmountBox text="Balance" type="info"/>
        </div>
        <RecordForm handleNewRecord={this.addRecord.bind(this)} />
        {recordsComponents}
      </div>
    );
  }
}
export default Records;

这里画出了页面

0901

  • 2.添加函数
  • credits()
  • debits()
  • balance()

src\components\Records.js:

import React, { Component } from "react";
import Record from "./Record";
import * as RecordsAPI from "../utils/RecordsAPI";
import RecordForm from "./RecordForm";
import AmountBox from "./AmountBox";

class Records extends Component {
  constructor() {...}

  componentDidMount() {...}
  addRecord(record) {...}
  updateRecord(record,data){...}
  deleteRecord(record){...}

  credits(){
    //1- 查找过滤出所有的正数
    //2- 将所有的正数相加

    let credits = this.state.records.filter((record)=>{
      return record.amount >= 0;
    });

    return credits.reduce((prev,curr)=>{
      return prev + Number.parseInt(curr.amount,0)
    },0)
  }


  debits(){
    let credits = this.state.records.filter((record)=>{
      return record.amount < 0;
    });

    return credits.reduce((prev,curr)=>{
      return prev + Number.parseInt(curr.amount,0)
    },0)
  }

  balance(){
    return this.credits() + this.debits();
  }

  render() {...}
    return (
      <div>
        <h2>Records</h2>
        <div className="row mb-3">
          <AmountBox text="Credit" type="success" amount = {this.credits()}/>
          <AmountBox text="Debit" type="danger" amount = {this.debits()}/>
          <AmountBox text="Balance" type="info" amount = {this.balance()}/>
        </div>
        <RecordForm handleNewRecord={this.addRecord.bind(this)} />
        {recordsComponents}
      </div>
    );
  }
}
export default Records;

改造src\components\AmountBox.js

import React from "react";
/* 
内插变量
*/
const AmountBox = ({ text ,type ,amount}) => {
  return (
    <div className="col">
      <div className="card">
        {/* <div className="card-header bg-success text-white" > */}
        <div className={`card-header bg-${type} text-white`}>
          {text}
        </div>
        <div className="card-body">
        {/* 78 */}
        {amount}
        </div>
      </div>
    </div>
  );
};

export default AmountBox;

最终效果

0902

Edit 10-finish

https://github.com/lenvo222/10_finish.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值