删除列表项后保存更改-反应

时间:2019-07-08 13:47:05

标签: javascript reactjs

有一个jobdetailjson,它最初保存了新作业的详细信息。在我的应用程序中,我正在从该jobdetailsJson中检索位置详细信息。它显示正确。但是问题是删除位置详细信息后,如果我保存更改,它将再次显示初始数据,而不是更新的数据或空列表。我无法更新jobdetailjson。

我认为我错过了在位置组件中更新jobdetailsjson的逻辑如何实现该逻辑

位置面板代码:

export class NewLocationPanel extends React.Component{
    constructor(props){
        super(props);
        this.state={
               open:false,
               configuredList:[],
               clearList:[]
        };
       this.configLocation = this.configLocation.bind(this);
        this.togglePanel = this.togglePanel.bind(this);
        this.handleClick = this.handleClick.bind(this);
        this.allLocations = this.allLocations.bind(this);
        this.clearall = this.clearall.bind(this);
        this.getLocationData = this.getLocationData.bind(this);
        this.handleRemove = this.handleRemove.bind(this);
        this.removeConfigLocation = this.removeConfigLocation.bind(this);
        this.removeLocationAll = this.removeLocationAll.bind(this);
    }

    togglePanel (e){
        this.setState({open : !this.state.open});
    }
    handleRemove(mruCode){
        this.props.removeLocation(mruCode)
     }
    handleClick (mruCode){
      this.props.addLocation(mruCode)
     }
     allLocations (){
       this.props.addAllLocation()
    }
    clearall (){
        this.props.removeAllLocation()
    }

    componentDidMount() {
        this.props.loadData();
      }

    componentDidUpdate(prevProps,prevState){
        if (prevProps.jobId != this.props.jobId || prevProps.jobDetailJson != this.props.jobDetailJson) {
            this.configLocation(this.props.jobDetailJson);
            console.log(this.props.jobDetailJson);
        }

    }

    configLocation(jobDetailJson){
        let configuredList = jobDetailJson.locations.locationDetails;
        this.setState({configuredList});
    }

    removeConfigLocation(index){
        this.setState(({configuredList})=>{
            let remList = [...configuredList]
            remList.splice(index,1)
            return {
                configuredList:remList
            }
        })
    }

    removeLocationAll(){
        if(this.props.jobDetailJson != null){
         let configuredList = this.props.jobDetailJson.locations.locationDetails;
               this.setState({configuredList:[]});
        }
    }

    getLocationData(){
         let saveableLocationlist = [];
         if(this.props.conLocations != null && this.props.conLocations!=undefined && this.props.jobDetailJson !=null && this.props.jobDetailJson !=undefined){
             let confLocationList = this.props.conLocations;
             let retrievedList = this.props.jobDetailJson.locations.locationDetails;
             //let totalList = retrievedList.filter(loc=> !confLocationList.some(newLoc => newLoc.mruCode===loc.mruCode));
            saveableLocationlist = retrievedList.concat(confLocationList);
         }
         else if(this.props.jobDetailJson === null && this.props.conLocations != null){
             let confListNewJob = this.props.conLocations;
             saveableLocationlist = confListNewJob;
         }
         else if(this.props.jobDetailJson===null && this.props.conLocations === null){
             saveableLocationlist= [];
         }
        const locationData = {
            locationDetails : saveableLocationlist
        }
      return locationData;
    }

    render(){
        //const{configuredList} = this.state;
        const _labels = store.getLabels();
        let collapsedToggle = this.props.open ? 'collapsed' : ''
        return(
            <div className="panel panel-default">
            <div className="panel-heading" onClick={(e)=>this.togglePanel(e)}>
              <div className="row">
              <div className="col-xs-12 col-sm-8 col-md-6 col-lg-6 panelHeadingLabel">
                     <span>{this.props.title}</span>
                     </div>
                        <div className="pull-right">
                        <span className="defaultHeaderTextColor">{this.state.configuredList.map((loc,index)=><span key={index}>{loc.mruCode} - {_labels[loc.division]} - {loc.country}</span>)}
                           <span onClick={(e)=>this.togglePanel(e)} className={this.state.open ? "collapse-chevronn" : "collapse-chevron"} aria-hidden="true"></span>
                   </span>
                    </div>
                </div>
           </div>
              {this.state.open?(
                        <div className="panel-body">
                             <div className="row grid-divider">
                             <div className="col-sm-6">
                             <div className="col-padding"><div className="pos-div"><h3>Locations List</h3><button style={{ display: this.props.location.length === this.props.conLocations.length ? "none" : "block" }} className="allLargeBtn" onClick={()=>{this.allLocations()}}>Add all locations</button></div><hr/>
                             {this.props.location.map((item,index)=>(
                             <div key={index}><div><b>{item.mruCode} - {_labels[item.division]} - {item.country}</b>{!this.props.conLocations.find(item2 => item.mruCode === item2.mruCode)&&(<div className="pull-right jd"><button style={{ display: this.state.configuredList.find(item3=> item.mruCode===item3.mruCode) ? "none" : "block" }} className="call-to-action" onClick={()=>{this.handleClick(item.mruCode)}}>Add Location</button></div>)}<hr/></div></div>))}
                            </div>
                             </div> 
                                  <div className="col-sm-6">
                                  <div className="col-padding">
                                  <div className="pos-div"><h3>Configured Location</h3><button className="allLargeBtn" onClick={()=>this.clearall()}>Remove all location</button></div><hr/>
              <div><table className="table"><thead>{this.state.configuredList.map((locc,index)=><tr key={index}><th><b>{locc.mruCode} - {_labels[locc.division]} - {locc.country}</b></th><th className="text-right"><img alt="DeleteIcon" onClick={()=>{this.removeConfigLocation(index)}} className="deleteIconStyle" src="img/delete_large_active.png" /></th></tr>)}</thead><tbody>
                        {this.props.conLocations.map((loct,index)=><tr key={index}>
                           <td><b>{loct.mruCode} - {_labels[loct.division]} - {loct.country}</b></td>
                           <td className="text-right"><img alt="DeleteIcon" onClick={()=>this.handleRemove(loct.mruCode)}className="deleteIconStyle" src="img/delete_large_active.png" /></td>
                        </tr>
                        )}
                    </tbody></table></div>

                                   </div>
                                  </div>
                                  </div> 
                    </div>):null}
            </div>

        );
    }
}

const mapStateToProps = state =>{
    return{
        location:state.locationRed.location,
        conLocations:state.locationRed.conLocations
    };
};

const mapDispatchToProps = (dispatch) => {
    return{
        loadData:()=>{dispatch(loadData())},
        addLocation:(mruCode)=>{dispatch(addLocation(mruCode))},
        addAllLocation:() =>{dispatch(addAllLocation())},
        removeLocation: (mruCode)=>{dispatch(removeLocation(mruCode))},
        removeAllLocation: () =>{dispatch(removeAllLocation())}
    }
}


export default connect(mapStateToProps,mapDispatchToProps,null,{withRef:true})(NewLocationPanel);

有一个我正在调用getLocationData()的组件。保存有效,但无法更新。

我正在使用

getLocationData()保存更新。但是jobdetailJson没有更新。 ComponentDidUpdate()有什么问题吗?请帮助我如何更新jobdetailJson以显示正确的当前状态

0 个答案:

没有答案
相关问题