2016-05-12 110 views
0

在我的租金细节编辑页面,有一个图像上传用户更新他们的图像或删除以前上传的图像从previousFile()并可以进一步添加图像。 onDrop()和showFiles()上的显示了进一步添加的图像。我已经定义了handleRemove()删除图像,但它们不能正常工作。所有图像被删除,而不是用户想要删除的特定图像。这可能是什么原因?所有图像被删除而不是删除图标被点击的特定图像reactjs

enter image description here

代码被缩短

export default class RoomDetail extends Component{ 
    constructor(props){ 
     super(props); 
     this.state = { rooms:[], isEditing:false }; 
     this.pk = this.props.data.pk; 
    } 

    componentDidMount(newUser){ 
     $.ajax({ 
      url:'/api/rentals/'+this.props.data.pk+'/', 
      type:'put', 
      contentType: "application/json", 
      data:JSON.stringify(newUser), 
      success: (data) => { 
       console.log('data',data); 
       }, 
       error: (xhr, status, err) => { 
       console.error(xhr, status, err.toString()); 
       } 
      }); 
    } 

    componentWillMount(){ 
     this.loadRoomFromServer(); 
    } 

    loadRoomFromServer(){ 
     $.ajax({ 
      url:'/api/rentals/'+this.props.data.pk, 
      dataType:'json', 
      success: (data) => { 
       console.log('data',data); 
       this.setState({rooms: data}); 
       }, 
       error: (xhr, status, err) => { 
       console.error(xhr, status, err.toString()); 
       } 
      }); 
    } 

    handleRemove(id){ 
     const files = this.state.rooms.gallery.slice(); 
     this.setState({rooms:files.splice(id,1)}); 
    } 

    renderRoomDetailSection(){ 
     let url = this.pk; 
     let imageFile; 
     let firstImage; 
     if(this.state.rooms.gallery){ 
      firstImage = this.state.rooms.gallery[0].image; 
      console.log('firstImage', firstImage); 
      imageFile = _.map(this.state.rooms.gallery, (image) => { 
      return(
        <div className="col-md-3"> 
         <img src={image.image} key={image.id} className="img-fluid img-rounded" /> 
        </div> 
       ); 
      }); 
     } 
     if (this.state.isEditing){ 
      return(
        <EditRent 
         ownerName={this.state.rooms.ownerName} 
         email = {this.state.rooms.email} 
         phoneNumber = {this.state.rooms.phoneNumber} 
         image = {this.state.rooms.gallery} 
         onRemove = {this.handleRemove.bind(this)} 
         pk={this.props.data.pk} /> 
       ) 
     } 
       return(
         <div className="detailListing"> 
         here is the rent details 
         </div> 
       ) 
    } 

    renderUserAction(){ 
     if (this.state.isEditing){ 
      return(
        save and cancel button is here 
       ); 

     } 

     return(
       <div className = "buttons"> 
       edit button is here 
       </div> 
      ); 
     } 

    render() { 
     return (
      <div className = "newRoomDetail" > 
       { this.renderRoomDetailSection() } 
       { this.renderUserAction() } 
      </div> 
     ); 
    } 
    } 

var fieldValues = { 
    ownerName:'name', 
    email:'[email protected]', 
    phoneNumber:'9842333333' 
} 


class EditRent extends React.Component{ 
constructor(props,context) { 
     super(props,context); 
     this.state = { 
      step: 1 
     }; 
     this.pk = this.props.pk; 
    } 

    saveValues(field_value) { 
    return function() { 
     fieldValues = Object.assign({}, fieldValues, field_value) 
    }() 
    } 

    nextStep(step) { 
    var step = this.state.step; 
    var newStep = step+1; 
    this.setState({step:newStep}); 
    } 

    previousStep(step) { 
    var step = this.state.step; 
    var newStep = step-1 
    this.setState({ 
     step : newStep 
    }); 
    } 

    showStep() { 
    switch (this.state.step) { 
    case 1: 
     return <RenderPersonalInfo fieldValues={fieldValues} 
          ownerName={this.props.ownerName} 
          email={this.props.email} 
          phoneNumber={this.props.phoneNumber} 
          nextStep={this.nextStep.bind(this)} 
          previousStep={this.previousStep.bind(this)} 
          saveValues={this.saveValues.bind(this)} /> 
    case 2: 
     return <RenderPhotos fieldValues={fieldValues} 
          image={this.props.image} 
          nextStep={this.nextStep.bind(this)} 
          previousStep={this.previousStep.bind(this)} 
          imageRemove={this.props.onRemove} 
          pk={this.props.pk} /> 
    } 
} 

    render() { 

    return (
     <main className="container"> 
     <div className="row"> 
      <div className="col-sm-12"> 
      <span className="progress-step">Step {this.state.step}</span> 
      </div> 
     </div> 
     <div className="container"> 
      <div className="row"> 
      {this.showStep()} 
      </div> 
     </div> 
     </main> 
    ) 
    } 
}; 

let image = []; 
class RenderPhotos extends React.Component { 
    constructor(props, context) { 
     super(props, context); 
     this.state = { 
      files: [] 
     }; 
    } 

    handleClick(image){ 
     this.props.imageRemove(image); 
    } 

    onDrop(files) { 
     console.log('Received files: ', files); 
     this.setState({ 
      files: files 
     }); 

     image = new FormData(files); 
     $.each(files,function(i,file){ 
     image.append('image',file); 
     }); 

    } 

    previousFile(){ 
     return(
      <ul className="gallery"> 
      { 
      _.map(this.props.image, (image,idx) => { 
         return(
          <li className="col-md-3" key={idx}> 
          <span className="remove"><i onClick= onClick= {this.handleClick.bind(this,image)} className = "fa fa-times" aria-hidden="true"></i></span> 
          <img src={image.image} key={image.id} className="img-fluid img-rounded" /> 
          </li> 
         ) 
        }) 
      } 
      </ul> 
     ) 
    } 

    showFiles() { 
     const { files } = this.state; 
     if (!files.length) { 
      return null; 
     } 
     return (
      <div> 
       <h3>Dropped files: </h3> 
       <ul className="gallery"> 
        { 
         _.map(files, (file, idx) => { 
          return (
           <li className="col-md-3" key={idx}> 
            <img src={file.preview} width={200}/> 
            <div>{file.name}</div> 
           </li> 
          ) 
         }) 

        } 
       </ul> 
      </div> 
     ); 
    } 

    render() { 
     return (
      <div> 
       <div className="col-md-12"> 
       <form method="POST" encType="multipart/form-data"> 
       <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"/> 
        <Dropzone onDrop={this.onDrop.bind(this)} 
          style={style} 
          activeStyle={activeStyle} 
          accept="image/*"> 
        Try dropping some files here, or click to select files to upload. 
       </Dropzone> 
       </form> 
       { this.previousFile() } 
       { this.showFiles() } 

       </div> 
       <div className="row continueBtn text-right"> 
        <button className="btn how-it-works pull-left" onClick={this.props.previousStep.bind(this)}><i className="fa fa-hand-o-left"></i></button> 
        <button className="btn how-it-works pull-right" onClick={this.nextStep.bind(this)}><i className="fa fa-floppy-o"></i></button> 
       </div> 
      </div> 

    ); 
    } 
} 

删除的部分是在RenderPhotos部件

+0

首次在我生命中我没有看到任何开发商评论或aswering在这里:) – pri

回答

1

这里的问题是,您尝试通过将它们设置为本地状态来移除来自道具(previousFile)的图像。如果您要从本地状态渲染图像,那么这种方法可行,但您所做的是直接从道具渲染,这会导致React看不到任何更改。要使图像移除工作,您需要将移除逻辑移至父组件,并为子组件提供触发删除的功能。

class Parent extends Component { 
 
    constructor() { 
 
    this.state = { 
 
     files: [] 
 
    }; 
 
    } 
 

 
    handleRemove(id) { 
 
    const files = this.state.files.slice(); 
 
    this.seState({ 
 
     files: files.splice(id, 1) 
 
    }); 
 
    } 
 

 
    render() { 
 
     return <Files files={this.state.files} onRemove={this.handleRemove.bind(this)} />; 
 
    } 
 
} 
 

 
function handleClick(id, props) { 
 
    props.handleRemove(id); 
 
} 
 

 
function Files(props) { 
 
    return (
 
     <ul> 
 
     {props.files.map(
 
     (file, key) => <li key={key} onClick={handleClick.bind(this, key, props)}>{file.name}</li> 
 
     )} 
 
     </ul> 
 
    ); 
 
}

+0

所以在我的情况,我有更新的房间对象,对不对?因为图像和其他值被加载到房间对象。 – pri

+0

是的,这是正确的'this.state.rooms.gallery' – edvinerikson

+0

我已更新我的代码。当我点击删除图标,而不是删除特定图像时,它会删除所有图像。 – pri