React数据组件表不呈现html

时间:2017-04-28 17:11:48

标签: javascript html reactjs

https://github.com/carlosrocha/react-data-components包不允许将html发送到td单元格。参见:

enter image description here

enter image description here

我的目标是该产品的超链接。

我的用途是:

import React from 'react';
var DataTable = require('react-data-components').DataTable;
import PlainTable from './PlainTable'


class ReduxDataTable extends React.Component {
  constructor(props) {
    super(props);
  }

    processHeaders(){
      var columns = [];
      for (let i = 0; i < this.props.data.headers.length; i++){
          var header = this.props.data.headers[i];
          var item = {title: header, prop: header};
          columns.push(item);
      }
      return columns;
    }

    render() {
        var dataList = this.props.data.data;
        console.log("datalist is", dataList);
        console.log("datalist length is", dataList.length);
        var headerList = this.processHeaders();

        if(dataList.length > 2) {
          return (
              <DataTable
                keys="name"
                columns={headerList}
                initialData={dataList}
                initialPageLength={20}
                initialSortBy={{ prop: headerList[0].title, order: 'descending' }}
                pageLengthOptions={[ 20, 60, 120 ]}
              />
          );
        }
        else {
          return (
              <PlainTable
                headers={headerList}
                rows={dataList}
              />
          );
        }
    }
}

export { ReduxDataTable as default };

然后只是

return (
  <div className="card">
    <h2 className="style-1">Detailed Report</h2>
    <br/>
      <h2 className="style-1:after">Data about products </h2>
      <ReduxDataTable data={data}/>
 </div>

普通表格是<table>,以防少数产品。

套餐没有显示任何&#34; htmlTrue&#34;选项,搜索&#34; html&#34;没有任何用处。我对任何html都有同样的问题:

enter image description here

我不反对分叉,但是有一种简单的方法可以使用这个包并在这里声明html吗?谢谢

1 个答案:

答案 0 :(得分:3)

我没有使用该组件,但查看代码,似乎您可以使用render函数来执行您需要的操作。见这里:https://github.com/carlosrocha/react-data-components/blob/3d092bd375da0df9428ef02f18a64d056a2ea5d0/src/Table.js#L13

请参阅此处的示例https://github.com/carlosrocha/react-data-components/blob/master/example/table/main.js#L17

相关代码段:

const renderMapUrl =
  (val, row) =>
    <a href={`https://www.google.com/maps?q=${row['lat']},${row['long']}`}>
      Google Maps
    </a>;

const tableColumns = [
  { title: 'Name', prop: 'name' },
  { title: 'City', prop: 'city' },
  { title: 'Street address', prop: 'street' },
  { title: 'Phone', prop: 'phone', defaultContent: '<no phone>' },
  { title: 'Map', render: renderMapUrl, className: 'text-center' },
];

return (
  <DataTable
    className="container"
    keys="id"
    columns={tableColumns}
    initialData={data}
    initialPageLength={5}
    initialSortBy={{ prop: 'city', order: 'descending' }}
    pageLengthOptions={[ 5, 20, 50 ]}
  />
);

尝试将render属性添加到dataList。也许是这样的

var dataList = this.props.data.data;
for (let i=0; i<dataList.length; i++)
    dataList[i].render = function(val, row) {return (
        <a href={row.href}>row.title</a>
    )}