单击该行中的React组件链接时,如何防止行单击?

时间:2015-09-28 15:09:02

标签: javascript reactjs

我有一个网格,我希望能够单击该行进入“详细信息”视图,同时还具有单击的快捷链接,不会触发“详细信息”视图。这是我的代码(为简洁起见,删除了一些代码):

var ActionsColumn = React.createClass({

    onClickEdit: function(e) {
        e.preventDefault();
    },

    approveUser: function(organizationUserID) {
        OrganizationActions.approveUser(organizationUserID);
    },

    render: function () {
        var organizationUserID = this.props.rowData._id;

        return (
            <span className='hover-actions'>
                <a onClick={this.onClickEdit} className={(this.props.rowData.is_pending_approval) ? 'disabled' : ''}>edit</a>| 
                {(this.props.rowData.is_pending_approval) 
                    ? <a onClick={this.approveUser.bind(this, organizationUserID)}>approve</a>
                    : (this.props.rowData.is_suspended)
                    ? <a onClick={this.activateUser.bind(this, organizationUserID)}>activate</a>
                    : <a onClick={this.suspendUser.bind(this, organizationUserID)}>suspend</a>
                }|
                <a onClick={this.deleteUser.bind(this, organizationUserID)}><i className='fa fa-trash' /></a>
            </span>
        );
    }
});

问题是上面的代码在用户网格类之外,所以我不确定实现布尔值以防止行点击是多么容易。

var Users = React.createClass({

    mixins: [Reflux.connect(OrganizationStore)],

    columns: ['full_name', 'email', 'is_pending_approval', 'role_name', 'actionsColumn'],

    columnMetaData: [
        {
            'columnName': "full_name",
            'displayName': "Name",
            'order': 1,
            'visible': true
        },
        {
            'columnName': "email",
            'displayName': "Email",
            'order': 2,
            'visible': true,
            'cssClassName': 'email-column'
        },
        {
            'columnName': "is_pending_approval",
            'displayName': "Status",
            'order': 3,
            'visible': true,
            'customComponent': StatusColumn     
        },
        {
            'columnName': "role_name",
            'displayName': "Role",
            'order': 4,
            'visible': true,
            'cssClassName': 'role-column'
        },
        {
            'columnName': 'actionsColumn',
            'displayName': '',
            'order': 5,
            'customComponent': ActionsColumn,
            'visible': true,
            'cssClassName': 'actions-column'
        }
    ],

    onRowClick: function (e) {

        var user = e.props.data;

        // Cannot click a user that is pending approval.
        if (user.is_pending_approval) {
            return;
        } 

        this.setState({detailedUser: user});
    },

    render: function () {

        var organization = this.props.organization;

        return(
            <section className='organization-users-view'>
                <div className='list-header'>
                    <div className='menu-bar'>
                        <a onClick={this.onAllUsersDropdownClick}>
                            All Users<i className="fa fa-caret-down" />
                        </a>
                        <button className='btn btn-primary btn-xs menu-button' onClick={this.addNewUser}><i className="fa fa-user-plus" />Invite User</button>
                    </div>
                </div>
                {(this.state.detailedUser) 
                    ?
                        <UserView organization={organization} user={this.state.detailedUser} returnToList={this.returnToList} setUser={this.setUser}/>
                    :
                        <Griddle
                            results={organization.users}
                            columnMetadata={this.columnMetaData}
                            columns={this.columns}
                            resultsPerPage={10}
                            useGriddleStyles={false} 
                            noDataMessage={"This organization currently has no users."}
                            tableClassName={"table table-hover table-responsive"}
                            gridClassName={"grid-container"}
                            onRowClick={this.onRowClick} /> 
                }
            </section>
        );
    }
});

问题 - 虽然点击“批准”链接成功发出api调用以批准用户,但它也会触发“onRowClick”功能,然后用户处于“详细用户”视图中,我不这样做想要发生。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

e.preventDefault()不会阻止事件传播给父母,因为你需要e.stopPropagation()