鍍金池/ 問(wèn)答/HTML/ 如何將事件和觸發(fā)的函數(shù)當(dāng)作參數(shù)傳遞,可以使用jsx語(yǔ)法

如何將事件和觸發(fā)的函數(shù)當(dāng)作參數(shù)傳遞,可以使用jsx語(yǔ)法

page1

columns = [{
    title: '操作', dataIndex: 'action',
    fixed: 'right',
    width: 76,
    render: [
        { title: '編輯', icon: 'edit', span: 12, onClick: 'this.showEdit.bind(this, record)', },
        { title: '刪除', icon: 'delete', span: 12, onClick: 'this.delectItem.bind(this, record)', },
    ],
},,,,]

page2

handleColumns = (columns) => {
    columns.forEach((item, index) => {
        if (item.dataIndex == 'action') {
            item['render'] = (text, record) => (
                <Row type="flex" justify="space-between" align="middle" gutter={8} style={{ width: item.width }}>
                    {item.other.map((item, index) => {
                        return <Col key={index} span={item.span}><Tooltip title={item.title}><Button shape="circle" icon={item.icon} /></Tooltip></Col>
                    })}
                </Row>
            )
        }
    })
    return columns
}

<Table
    columns={handleColumns(columns)}
/>

圖片描述

如果換成jsx語(yǔ)法,因?yàn)椴辉谝粋€(gè)頁(yè)面,函數(shù)會(huì)在page1執(zhí)行,因?yàn)閠his指向的是page1

回答
編輯回答
艷骨

不要用 bind 或箭頭函數(shù)不就好了。onClick = {this.showEdit}

還有,你最上面 page1里的,onClick: 'this.showEdit.bind(this, record)' 是錯(cuò)的,首先onclick 的值是個(gè)字符串,根本不是方法,其次,bind 用法也錯(cuò)誤。

2018年1月30日 09:06
編輯回答
祈歡

你數(shù)據(jù)里屬性值不能直接寫(xiě)成函數(shù)么

2017年3月2日 05:47
編輯回答
吢涼
onClick={() => eval(`self.${item.click}(record)`)}

用這種方式解決了問(wèn)題

2018年2月14日 23:57