如何使用MVC在React JS中创建动态表?

时间:2018-10-19 11:41:41

标签: c# reactjs model-view-controller

我正在使用react来创建新组件,并且我想为每个控件创建动态组件,并且我从表开始,在其中可以添加编辑,删除,添加更新的选项。

1 个答案:

答案 0 :(得分:0)

You should bind the Method name in constructor Level Like this 

constructor(props) {
    super(props);
    this.state = {
        items: [],
        isLoaded: false,
    }

    this.doAction = this.doAction.bind(this);
    this.barrow =this.barrow.bind(this);
    this.CRUDService = new CRUDService();
}

方法应为

doAction(actionName,recordId) {
    if(actionName === 'delete'){
        this.CRUDService.delete('books',recordId).then((result) => {
            let responseJson = result;
            console.log(result)
            debugger;
            if(responseJson===1){
                this.loadDataTable();
            }
        });
    } else {
        this.props.history.push('/book/' + actionName + '/' + recordId);
    }
}

barrow(id,currcount,totalcount){
    if(currcount<1){
        window.ShowPopup('Book is not available');
    }else{

    }
}

“ HTML绑定”部分应为

 <table className="table">
                                    <thead>
                                    <tr>
                                        <th>IsbnNo</th>
                                        <th>Title</th>
                                        <th>Book Current Count</th>
                                        <th>Book Total Count</th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    {items.map((item, i) =>
                                        <tr key={i}>
                                            <td>{item.isbn_no}</td>
                                            <td>{item.title}</td>
                                            <td>{item.book_current_count}</td>
                                            <td>{item.book_total_count}</td>

                                            <td>
                                            <button type="button" className="btn btn-info btn-sm btn-space"
                                                        onClick={() => this.doAction('view',item.id)}>
                                                    View</button>

                                                <button type="button" className="btn btn-primary btn-sm btn-space"
                                                        onClick={() => this.doAction('edit',item.id)}>
                                                    Edit</button>
                                                <button type="button" className="btn btn-danger btn-sm btn-space"
                                                        onClick={() => this.doAction('delete',item.id)}>
                                                    Delete</button>
                                                    <button type="button" className="btn btn-secondary btn-sm btn-space"
                                                        onClick={() => this.barrow(item.id,item.book_current_count,item.book_total_count)} disabled>
                                                      Barrow   </button>

                                            </td>
                                        </tr>
                                    )}
                                </tbody>

                                </table>