Javascript 反应-单击按钮时禁用行

Javascript 反应-单击按钮时禁用行,javascript,reactjs,disable,Javascript,Reactjs,Disable,我到处寻找这个,但每个例子似乎都不符合我想要的。单击按钮后,对于特定的表行,我需要禁用当前行,或者更好地禁用行上的按钮 我之前编写的代码只是禁用每一行的按钮。这是不对的。有关某些上下文,请参见应用程序im书写的屏幕截图: 当用户单击特定行的生成旅程时,我需要禁用该行的“生成旅程”按钮,以阻止他们再次执行此操作(这将导致服务器端出现问题) 我猜对有经验的人来说,这是一项简单的任务,但我尝试了不同的方法,但每一种方法都不能给我想要的结果 这是我的代码: 上面截图的渲染功能如下: render()

我到处寻找这个,但每个例子似乎都不符合我想要的。单击按钮后,对于特定的表行,我需要禁用当前行,或者更好地禁用行上的按钮

我之前编写的代码只是禁用每一行的按钮。这是不对的。有关某些上下文,请参见应用程序im书写的屏幕截图:

当用户单击特定行的
生成旅程
时,我需要禁用该行的“生成旅程”按钮,以阻止他们再次执行此操作(这将导致服务器端出现问题)

我猜对有经验的人来说,这是一项简单的任务,但我尝试了不同的方法,但每一种方法都不能给我想要的结果

这是我的代码:

上面截图的渲染功能如下:

 render() {
    return (
        <div className="polls-container">
            <div className="dashboard-title" style={{paddingTop: "2%", marginLeft: "-50px"}}>
                <h2>Dashboard</h2>
            </div>
            {
                !this.state.loading && this.state.results.length > 0 ? (
                    <RouteTable buttonDisabled ={this.state.emulateButtonDisabled} results={this.state.results} generateJourney={this.generateJourney} startEmulation={this.getJourneyEmulations}/>
                ) : null
            }
            {
                !this.state.isLoading && this.state.results.length === 0 ? (
                    <div className="no-polls-found">
                        <span>No Active Journey Generations</span>
                    </div>    
                ): null
            }
            {
                this.state.isLoading ? 
                <LoadingIndicator />: null                     
            }
        </div>
    );
}
}  
 export class RouteTable extends Component {

constructor(props) {
    super(props);
}

getStatus(result) {
    let status;
    if (result.customerStatus === 0) {
        status = (<Badge color={'secondary'}> Pre Journey Generation</Badge>)
    } else if(result.customerStatus === 1) {
        status = (<Badge color={'success'}> In Journey Generation</Badge>)
    } else if (result.customerStatus === 2) {
        status = (<Badge color={'info'}> Ready for Emulation</Badge>)
    } else {
        status = (<Badge href="/journeyEmulation" color={'danger'}> In Emulation</Badge>)
    }
    return status;
}

render() {
    const {startEmulation, buttonDisabled} = this.props;
    const items = this.props.results.map(result => {
        const serviceIdList = [];
        const deviceRequests = [];

        result.linkedCustomers.map(x => { const emulationData = {"imei": x.imei, "serviceId": x.serviceId, "deviceType": "CALAMP"}
            deviceRequests.push(emulationData)
        });

         result.linkedCustomers.map(x => {serviceIdList.push(x.serviceId);});

        return (
            <tr key={result.linkedCustomerId}>
                <th scope="row">{result.customerName}</th>
                <td>{result.linkedCustomerId}</td>
                <td>{result.numDevices}</td>
                <td>{result.startDate}</td>
                <td>{result.endDate}</td>
                <td>{result.lat}</td>
                <td>{result.lng}</td>
                <td> {this.getStatus(result)}</td>

                <td>
                    <div style={{width:"100%"}}>
                        <Button style={{width: "50%"}} color="primary" onClick={() => this.props.generateJourney(result.linkedCustomerId, result.startDate, result.endDate, serviceIdList, result.lat, result.lng)} disabled={buttonDisabled}>Generate Journey</Button>
                        {' '}
                        <Button style={{width: "50%", marginTop: "4%"}} color="danger" onClick={() => startEmulation(result.linkedCustomerId, result.customerName, result.startDate, result.endDate, deviceRequests)}>Emulate Journey</Button>
                    </div>
                </td>
            </tr>
        )
    })

    return (
        <div className="tableDesign" style={{marginTop: "2%"}}>
        <Table hover>
            <thead>
            <tr>
                <th>Customer Name</th>
                <th>Kinesis Customer Id</th>
                <th>Number of Devices</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Home Lat</th>
                <th>Home Long</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            {items}
            </tbody>
        </Table>
        </div>
    )
}
没什么特别的,只是简单地调用API来发布或放置数据。下面是
startEmulation={this.getJourneyEmulations}
的代码,单击
Emulate
按钮时会单击该代码。这基本上是在我们可以模拟它之前检查旅程是否已经生成。(等待完成状态)

我的RouteTable类如下所示:

 render() {
    return (
        <div className="polls-container">
            <div className="dashboard-title" style={{paddingTop: "2%", marginLeft: "-50px"}}>
                <h2>Dashboard</h2>
            </div>
            {
                !this.state.loading && this.state.results.length > 0 ? (
                    <RouteTable buttonDisabled ={this.state.emulateButtonDisabled} results={this.state.results} generateJourney={this.generateJourney} startEmulation={this.getJourneyEmulations}/>
                ) : null
            }
            {
                !this.state.isLoading && this.state.results.length === 0 ? (
                    <div className="no-polls-found">
                        <span>No Active Journey Generations</span>
                    </div>    
                ): null
            }
            {
                this.state.isLoading ? 
                <LoadingIndicator />: null                     
            }
        </div>
    );
}
}  
 export class RouteTable extends Component {

constructor(props) {
    super(props);
}

getStatus(result) {
    let status;
    if (result.customerStatus === 0) {
        status = (<Badge color={'secondary'}> Pre Journey Generation</Badge>)
    } else if(result.customerStatus === 1) {
        status = (<Badge color={'success'}> In Journey Generation</Badge>)
    } else if (result.customerStatus === 2) {
        status = (<Badge color={'info'}> Ready for Emulation</Badge>)
    } else {
        status = (<Badge href="/journeyEmulation" color={'danger'}> In Emulation</Badge>)
    }
    return status;
}

render() {
    const {startEmulation, buttonDisabled} = this.props;
    const items = this.props.results.map(result => {
        const serviceIdList = [];
        const deviceRequests = [];

        result.linkedCustomers.map(x => { const emulationData = {"imei": x.imei, "serviceId": x.serviceId, "deviceType": "CALAMP"}
            deviceRequests.push(emulationData)
        });

         result.linkedCustomers.map(x => {serviceIdList.push(x.serviceId);});

        return (
            <tr key={result.linkedCustomerId}>
                <th scope="row">{result.customerName}</th>
                <td>{result.linkedCustomerId}</td>
                <td>{result.numDevices}</td>
                <td>{result.startDate}</td>
                <td>{result.endDate}</td>
                <td>{result.lat}</td>
                <td>{result.lng}</td>
                <td> {this.getStatus(result)}</td>

                <td>
                    <div style={{width:"100%"}}>
                        <Button style={{width: "50%"}} color="primary" onClick={() => this.props.generateJourney(result.linkedCustomerId, result.startDate, result.endDate, serviceIdList, result.lat, result.lng)} disabled={buttonDisabled}>Generate Journey</Button>
                        {' '}
                        <Button style={{width: "50%", marginTop: "4%"}} color="danger" onClick={() => startEmulation(result.linkedCustomerId, result.customerName, result.startDate, result.endDate, deviceRequests)}>Emulate Journey</Button>
                    </div>
                </td>
            </tr>
        )
    })

    return (
        <div className="tableDesign" style={{marginTop: "2%"}}>
        <Table hover>
            <thead>
            <tr>
                <th>Customer Name</th>
                <th>Kinesis Customer Id</th>
                <th>Number of Devices</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Home Lat</th>
                <th>Home Long</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            {items}
            </tbody>
        </Table>
        </div>
    )
}
传递给RouteTable的道具

 {
                !this.state.loading && this.state.results.length > 0 ? (
                    <RouteTable generatingId ={this.state.generatingId} results={this.state.results} generateJourney={this.generateJourney} startEmulation={this.getJourneyEmulations}/>
                ) : null
            }
{
!this.state.loading&&this.state.results.length>0(
):null
}
然后在RouteTable中:

                            <Button style={{width: "50%"}} color="primary" onClick={() => this.props.generateJourney(result.linkedCustomerId, result.startDate, result.endDate, serviceIdList, result.lat, result.lng)} disabled={this.props.generatingId === result.linkedCustomerId}>Generate Journey</Button>
this.props.generateJourney(result.linkedCustomerId、result.startDate、result.endDate、serviceList、result.lat、result.lng)}禁用={this.props.generatingId==result.linkedCustomerId}>生成旅程
一种方法是:

  • 在使用
    RouteTable
    的组件上,创建一个名为
    generatingId
    的新状态
  • 为名为
    generatingId
    创建一个新道具,并将
    this.state.generatingId
    作为其值
  • generateJourney
    函数中,在AJAX调用之前执行
    this.setState({generatingId:customerId})
    。然后在.Then和.catch中,创建一个
    this.setState({generatingId:null})
  • 现在,在
    RouteTable
    组件中,将行的“生成旅程”按钮更新如下:
  • this.props.generateJourney(result.linkedCustomerId、result.startDate、result.endDate、serviceList、result.lat、result.lng)}禁用={this.props.generatingId==result.linkedCustomerId}>生成旅程
    

    当您单击“生成旅程”按钮时,generateJourney功能会将客户ID设置为正在生成的ID。这个新值将被传递到RouteTable,然后呈现您的表,因为行的按钮将检查
    generatingId
    prop是否等于该行的
    customerId
    ,如果该语句为true,它将禁用该行上的按钮。

    如果将
    buttonDisabled
    prop设置为true,它会禁用按钮吗?只是想确保您尝试禁用的按钮组件没有问题。是的,但是对于表中的每个按钮,而不是特定的行!嗨,我已经编辑了我的答案。它似乎不起作用。它会禁用,然后很快恢复为可再次按下OK,这样就可以工作了。但是,如果我转到一个新页面,然后返回表格页面,则该按钮可以再次单击。我怎样才能避开这件事?我需要传遍全州吗?
    <Button style={{width: "50%"}} color="primary" onClick={() => this.props.generateJourney(result.linkedCustomerId, result.startDate, result.endDate, serviceIdList, result.lat, result.lng)} disabled={this.props.generatingId === result.linkedCustomerId}>Generate Journey</Button>