Javascript DataTableOnClick执行自定义函数
我有一个带有React、Spring和Datatables的应用程序(我的要求)。我可以根据单击的行提取信息。“我的表”是一个单独的组件,它是需要表的所有其他组件的子组件(它们只需在props中传递适当的列和数据,就可以正常工作)。现在我想要一个函数,当单击表行时更新父组件的状态。在我的表格组件中,我创建了如下内容:Javascript DataTableOnClick执行自定义函数,javascript,reactjs,function,datatable,datatables,Javascript,Reactjs,Function,Datatable,Datatables,我有一个带有React、Spring和Datatables的应用程序(我的要求)。我可以根据单击的行提取信息。“我的表”是一个单独的组件,它是需要表的所有其他组件的子组件(它们只需在props中传递适当的列和数据,就可以正常工作)。现在我想要一个函数,当单击表行时更新父组件的状态。在我的表格组件中,我创建了如下内容: updateIndex = (index) => { this.props.updateSelectedIndex(index); } 然后:
updateIndex = (index) => {
this.props.updateSelectedIndex(index);
}
然后:
componentDidMount() {
var table = $(this.refs.main).DataTable({
dom: '<"data-table-wrapper"t>',
data: this.props.data,
columns: this.props.columns,
ordering: false,
select: 'single'
});
$(this.refs.main).on('click', 'tr', function () {
var index = table.row(this).index();
item = table.row(this).data()
updateIndex(index);
console.log(index)
console.log(item)
});
}
您可以尝试创建“var that=this”,然后调用它。updateIndex()快速修复方法是将
function()
替换为()=>
,但是您不应该将jQuery与React一起使用。寻找合适的react数据表库。
$(this.refs.main).on('click', 'tr', function () {
}