1、渲染页面

  1. //index.js
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import App from './App';
  5. import "antd/dist/antd.css";
  6. ReactDOM.render(<App />, document.getElementById('root'));
  1. //App.js
  2. import React from 'react';
  3. import Wrap from './components/Style';
  4. import { InputNumber } from 'antd';
  5. import { Button } from 'antd';
  6. import axios from 'axios'
  7. class App extends React.Component{
  8. constructor(props){
  9. super(props);
  10. this.state = {
  11. cartList:[],
  12. sumPrice:0,
  13. checked:false
  14. }
  15. }
  16. render(){
  17. return (
  18. <div className="container">
  19. <Wrap>
  20. <h2 className="head">购物车</h2>
  21. <table className="table" border="1px">
  22. <thead>
  23. <tr>
  24. <th>
  25. 全选
  26. <input type="checkbox" />
  27. </th>
  28. <th>商品</th>
  29. <th>单价</th>
  30. <th>数量</th>
  31. <th>小计</th>
  32. <th>操作</th>
  33. </tr>
  34. </thead>
  35. <tbody>
  36. {this.state.data.map(item=>{
  37. return (
  38. <tr key={item.id}>
  39. <td>
  40. <input type="checkbox" />
  41. </td>
  42. <td className="cover-name">
  43. <img className="cover" src={item.productCover} />
  44. <p>{item.productName}</p>
  45. </td>
  46. <td>
  47. ¥{item.productPrice}
  48. </td>
  49. <td>
  50. <InputNumber min={1} max={10} defaultValue={item.productCount} />
  51. </td>
  52. <td>
  53. ¥{item.productPrice*item.productCount}
  54. </td>
  55. <td>
  56. <Button type="danger">删除</Button>
  57. </td>
  58. </tr>
  59. )
  60. })}
  61. </tbody>
  62. </table>
  63. <p className="total">总价:</p>
  64. </Wrap>
  65. </div>
  66. )
  67. }
  68. componentDidMount(){
  69. var url = "http://yapi.demo.qunar.com/mock/36046/cart";
  70. axios.get(url).then(res=>{
  71. // console.log(res.data)
  72. //
  73. res.data.forEach(item=>{
  74. item.isSelected = false
  75. })
  76. var cartList = res.data;
  77. this.setState({
  78. cartList
  79. })
  80. })
  81. }
  82. }
  83. export default App;
  1. //components/Style.js
  2. import styled from 'styled-components'
  3. const Wrap = styled.div`
  4. width:100%;
  5. color:#333;
  6. .head{
  7. text-align:center;
  8. font-size:24px;
  9. color:brown;
  10. }
  11. .table{
  12. width:100%;
  13. text-align:center;
  14. }
  15. .table th{
  16. width:300px;
  17. font-size:18px;
  18. }
  19. .cover{
  20. width:120px;
  21. }
  22. .total{
  23. font-size:22px;
  24. }
  25. `
  26. export default Wrap;

2、实现改变商品数量,小计也跟着变化+总计

  1. this.state = {
  2. cartList:[],
  3. sumPrice:0,
  4. checked:false
  5. }
  1. <InputNumber min={1} max={10} defaultValue={item.productCount}
  2. onChange={this.handleSubTotal.bind(this,index)}/>
  3. <p className="total">总价:{this.state.sumPrice}</p>
  1. sum(){
  2. var total = 0;
  3. this.state.cartList.forEach(item=>{
  4. if(item.isSelected){
  5. total = item.productCount*item.productPrice+total;
  6. }
  7. })
  8. this.setState({
  9. sumPrice:total.toFixed(2)
  10. })
  11. return total;
  12. }
  1. handleSubTotal(index,value){
  2. this.state.cartList[index].productCount = value
  3. this.setState({
  4. cartList:this.state.cartList
  5. });
  6. this.sum();
  7. }

3、全选功能

  1. //全选控制list
  2. <input type="checkbox" checked={this.state.checked}
  3. onChange={this.isCheckedAll.bind(this)} />
  1. isCheckedAll(event){
  2. this.state.cartList.map(item=>{
  3. item.isSelected = event.target.checked
  4. })
  5. this.setState({
  6. cartList:this.state.cartList,
  7. checked:event.target.checked
  8. })
  9. this.sum()
  10. }
  1. //list控制全选
  2. <input type="checkbox" checked={item.isSelected}
  3. onChange={this.checkedChange.bind(this,index)} />
  1. checkedChange(index,event){
  2. this.state.cartList[index].isSelected = event.target.checked
  3. var val = this.state.cartList.every(item=>item.isSelected)
  4. this.setState({
  5. cartList:this.state.cartList,
  6. checked:val
  7. });
  8. this.sum()
  9. }

4、点击删除功能

  1. <Button type="danger" onClick={this.handleDel.bind(this,item.id)}>删除</Button>
  1. handleDel(id){
  2. console.log(id)
  3. var cartList = this.state.cartList.filter(item=>item.id !== id)
  4. this.setState({
  5. cartList
  6. })
  7. this.sum()
  8. }