Advertisement
Guest User

modificar

a guest
Apr 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react';
  2. import 'bootstrap/dist/css/bootstrap.css';
  3. import 'bootstrap/dist/css/bootstrap-theme.css';
  4. import { Button } from 'react-bootstrap';
  5.  
  6. class Modificar extends React.Component{
  7.     constructor(props) {
  8.         super(props);
  9.  
  10.         this.state = {
  11.             products: [],
  12.             producto: {},
  13.             codigo: "",
  14.             nombre: "",
  15.             categoria: "",
  16.             precio: "",
  17.             fecha: "",
  18.             cambio: false,
  19.             isLoading: false
  20.         };
  21.             //this.handleInputChange = this.handleInputChange.bind(this);
  22.         }
  23.         //Cuando se edita los campos del input, se guarda el valor ingresado **
  24.        /* handleInputChange(event) {
  25.             const target = event.target;
  26.             const value =  target.value;
  27.             const name = target.name;
  28.        
  29.             this.setState({
  30.               [name]: value
  31.             });
  32.           }*/
  33.        
  34.           //Evento cuando se presiona el boton editar **Aun no funciona bien :c
  35.         editProduct(e) {
  36.             this.setState({codigo: this.codigop.value});
  37.             this.setState({nombre: this.nombrep.value});
  38.             this.setState({categoria: this.categoriap.value});
  39.             this.setState({precio: this.preciop.value});
  40.             fetch('http://localhost:8081/api/update?id='+e+'&codigo='+this.state.codigo+'&nombre='+this.state.nombre+'&fecha='+this.state.fecha+'&categoria='+this.state.categoria+'&precio='+this.state.precio)
  41.             .then(response => console.log("Producto actualizado "+ this.state.codigo));
  42.             this.setState({products: [], isLoading: false});
  43.             this.componentDidMount();
  44.             this.render();
  45.            
  46.             }
  47.          
  48.     //Carga la lista de productos para que puedan ser modificadas
  49.     componentDidMount() {
  50.         this.setState({isLoading: true});
  51.  
  52.         fetch('http://localhost:8081/api/all')
  53.             .then(response => response.json())
  54.             .then(data => this.setState({products: data, isLoading: false}));
  55.         }
  56.  
  57.     render() {
  58.         const {isLoading} = this.state;
  59.         if (isLoading) {
  60.             return <p>Cargando...</p>;
  61.         }
  62.         else{
  63.             return (
  64.               <div>
  65.                              
  66.                   <table id="t01">
  67.                     <tbody>
  68.                       <tr>
  69.                         <th>Codigo</th>
  70.                         <th>Nombre</th>
  71.                         <th>Categoria</th>
  72.                         <th>Pecio</th>
  73.                         <th>Accion</th>
  74.                       </tr>
  75.                               {this.state.products.map((product) =>
  76.                       <tr key={product.id}>
  77.                         <th><input ref={(codigoc) => this.codigop = codigoc}  name="codigo" type="text" defaultValue = {product.codigo} />{this.state.codigo}</th>
  78.                         <th><input ref={(nombrec) => this.nombrep = nombrec} name="nombre" type="text"  defaultValue = {product.nombre} />{this.state.nombre}</th>
  79.                         <th><input ref={(categoriac) => this.categoriap = categoriac} name="categoria" type="text" defaultValue = {product.categoria} />{this.state.categoria}</th>
  80.                         <th><input ref={(precioc) => this.preciop = precioc} name="precio" type="text" defaultValue = {product.precio}/>{this.state.precio}
  81.                         <input ref={(fechac) => this.fechap = fechac} name="fecha" type="hidden" defaultValue = {product.fecha}/>{this.state.fecha}</th>
  82.                         <th> <button onClick={(e) => this.editProduct(product.id)}>editar</button></th>
  83.                       </tr>
  84.                               )}
  85.                     </tbody>
  86.                   </table>
  87.               </div>
  88.  
  89.                
  90.             );
  91.         }
  92.       }
  93. }
  94.  
  95. export default Modificar;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement