zoukankan      html  css  js  c++  java
  • react增删改查

    react增删改查

    import React, { Component } from 'react'
    import axios from 'axios'
    export default class List extends Component {
        constructor() {
            super()
            this.state = {
                list: [],
                username: "",
                age: ""
            }
        }
        componentDidMount() {
            this.find()
        }
        handleChange = e => {
            this.setState({
                [e.target.id]: e.target.value
            })
        }
        find = () => {
            axios.get("http://localhost:3001/list").then(res => {
                this.setState({
                    list: res.data
                })
            })
        }
        add = () => {
            axios.post("http://localhost:3001/list", {
                username: this.state.username,
                age: this.state.age
            }).then(res => {
                this.find()
            })
        }
        del = (id) => {
            axios.delete("http://localhost:3001/list/" + id).then(res => {
                this.find()
            })
        }
        update = (item) => {
            let value =  prompt("请输入修改内容..",item.username+","+item.age)
            if(value){
                var arr = value.split(",")
                //派发 patch请求
                axios.patch("http://localhost:3001/list/"+item.id,{
                    username:arr[0],
                    age:arr[1]
                }).then(res=>{
                    this.find()
                })
            }
        }
        select = () => {
            axios.get("http://localhost:3001/list?username_like=" + this.state.username).then(res => {
                console.log(res)
                this.setState({
                    list: res.data
                })
            })
        }
        render() {
            let { list, username, age } = this.state
            return (
                <div>
                    <label>用户名:<input id="username" value={username} onChange={this.handleChange} placeholder="请输入用户名"></input></label>
                    <label>年龄:<input id="age" value={age} onChange={this.handleChange} placeholder="请输入年龄"></input></label>
                    <button onClick={this.add}>添加</button>
                    <button onClick={this.select}>查询</button>
                    <ul>
                        {
                            list.map((item, index) => {
                                return <li key={index}>{item.username}---{item.age}
                                    <button onClick={this.del.bind(this, item.id)}>删除</button>
                                    <button onClick={this.update.bind(this, item)}>修改</button>
                                </li>
                            })
                        }
                    </ul>
                </div>
            )
        }
    }
    
    
  • 相关阅读:
    USACO castle
    【求建议】毕业之声——信院IT类毕业学子经验分享交流会
    需求管理之勇于直面需求变更
    spring boot 配置redis
    java Redis工具类
    java多线程之原子变量
    innerHTML与jquery里的html()区别介绍
    解决IE下select标签innerHTML插入option的BUG(兼容
    【转载】html中object标签详解
    最全的CSS浏览器兼容问题http://www.68design.net/Web-Guide/HTMLCSS/37154-1.html
  • 原文地址:https://www.cnblogs.com/cupid10/p/14149440.html
Copyright © 2011-2022 走看看