使用ReactJS的Datatables.net在列中呈现ReactJS组件

时间:2017-05-19 19:10:50

标签: jquery reactjs datatables

我有以下数据表组件:

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>

问题是,对于数据表,我需要使用反应路由器呈现链接,使用anchorlink()不是解决方案,因为它将重新呈现整个页面。所以我需要使用指定的链接在列中呈现自定义组件。该链接使用ID构建。

2 个答案:

答案 0 :(得分:11)

我会为其他开发者回答我自己的问题。我做的是以下几点:

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

createdCell方法接收实际的dom元素,因此您可以直接在那里呈现react组件,唯一的问题是您无法呈现Links,这是因为路由器需要上下文并且上下文丢失。因此,最好的方法是使用方法转到特定路由,在这种情况下,goto从父项传递this.props.router.push

答案 1 :(得分:1)

也许您可以使用ReactDOM.render。该函数接收组件和容器,因此您可以将链接初始化为空节点,并将props作为数据类型。然后在componentDidMount中,您可以遍历并调用如下所示的函数: 编辑:链接组件需要反应上下文隐式导航,我不认为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);
}

看起来你要用这样的东西指定一个特定的dom节点:

$('#example').dataTable( {
  "columnDefs": [ {
    "targets": 0,
    "data": "download_link",
    "render": function ( data, type, full, meta ) {
      return `<div class="link-div" data-linkURL=${data}></div>`;
    }
  } ]
} );

让我知道它是怎么回事!