Nic*_*ico 16 jquery datatables reactjs
我有以下组件与数据表:
import React, { Component } from 'react'
import { Link } from 'react-router'
import { PanelContainer, Panel, PanelBody, Grid, Row, Col } from '@sketchpixy/rubix'
import $ from 'jquery'
import DataTable from 'datatables.net'
$.DataTable = DataTable
const columns = [{
title: '<input type="checkbox" />',
data: 'check',
}, {
title: 'Foto',
data: 'avatar',
}, {
title: 'Nombre',
data: 'name',
}, {
title: 'Dirección',
data: 'address',
}, {
title: 'Clasificación',
data: 'clasification',
}, {
title: 'Editar',
data: 'editLink',
render: x => `<a href="${x}"><i class="icon-fontello-edit"></i></a>`, // <-- this line i'm interested!
}]
class Table extends Component {
transform(content) {
return content.map(x => ({
...x,
check: '<input type="checkbox" />',
avatar: '<img src="/public/imgs/app/nico.jpg" width="40" height="40" style="border-radius: 100%;">',
clasification: `<i class="${x.clasification.icon}"></i> ${x.clasification.name}`,
}))
}
componentDidMount(nextProps, nextState) {
this.table = $(this.refs.main).DataTable({
dom: '<"data-table-wrapper"tip>',
data: [],
columns,
language: {
info: 'Mostrando _START_-_END_ de _TOTAL_ puntos',
infoEmpty: 'No hay puntos',
paginate: {
next: 'Siguiente',
previous: 'Anterior',
},
},
})
}
componentWillUpdate() {
this.table.clear()
this.table.rows.add(this.transform(this.props.data))
this.table.draw()
}
componentWillUnmount() {
$('.data-table-wrapper')
.find('table')
.DataTable()
.destroy(true)
}
render() {
return (
<table
className="table table-striped hover"
cellSpacing="0"
width="100%"
ref="main"
/>
)
}
}
export default p =>
<PanelContainer>
<Panel>
<PanelBody>
<Grid>
<Row>
<Col xs={12}>
<Table data={p.data} />
</Col>
</Row>
</Grid>
</PanelBody>
</Panel>
</PanelContainer>
Run Code Online (Sandbox Code Playgroud)
问题是,对于数据表,我需要使用反应路由器呈现链接,使用anchorlink()不是解决方案,因为它将重新呈现整个页面.所以我需要使用指定的链接在列中呈现自定义组件.该链接使用ID构建.
Nic*_*ico 14
我将为其他开发人员回答我自己的问题.我做的是以下内容:
columnDefs: [{
targets: 5,
createdCell: (td, cellData, rowData, row, col) =>
ReactDOM.render(
<a style={{ cursor: 'pointer' }}
onClick={() => this.props.goto(cellData) }>
<i className="icon-fontello-edit"></i>
</a>, td),
} // ... the rest of the code
Run Code Online (Sandbox Code Playgroud)
createdCell方法接收实际的dom元素,因此您可以直接在那里呈现react组件,唯一的问题是您无法呈现Links,这是因为路由器需要上下文并且上下文丢失.所以最好的方法是使用一种方法去具体的路线,在这种情况下goto是this.props.router.push从父通过.
也许你可以利用 ReactDOM.render。该函数接受一个组件和一个容器,因此您可以将链接初始化为空节点,并将 props 作为数据类型。然后在 componentDidMount 中,您可以循环并调用一个如下所示的函数:编辑:Link 组件需要反应上下文来隐式导航,我不认为reactDOM.render 会将您的上下文对象与其创建的对象相协调。最好的选择是创建一个自定义链接组件以在此处使用,该组件使用 browserHistory(react-router v3) 或仅历史库进行导航。
componentDidMount() {
function populateLinks(node) {
const linkURL = node.dataset.linkURL;
reactDOM.render(<Link to={linkURL}>Hello</Link>, node);
}
$('.link-div').get().forEach(populateLinks);
}
Run Code Online (Sandbox Code Playgroud)
看起来你会指定一个特定的 dom 节点来渲染,如下所示:
$('#example').dataTable( {
"columnDefs": [ {
"targets": 0,
"data": "download_link",
"render": function ( data, type, full, meta ) {
return `<div class="link-div" data-linkURL=${data}></div>`;
}
} ]
} );
Run Code Online (Sandbox Code Playgroud)
让我知道事情的后续!
| 归档时间: |
|
| 查看次数: |
6607 次 |
| 最近记录: |