1

我在 DataTables 的每个单元格的单个元素中获取 HTML 标记。我也想解析 HTML 标签,但直到现在,还不能这样做。

我已经尝试过 html-react-parser 但它正在将它们解析成一个数组。

import Parse from "html-react-parser";

<Table className={classes.table}>
        <TableHead>
          <TableRow>
            {/* <TableCell>Dessert (100g serving)</TableCell>
            <TableCell align="right">Calories</TableCell>
            <TableCell align="right">Fat (g)</TableCell>
            <TableCell align="right">Carbs (g)</TableCell>
            <TableCell align="right">Protein (g)</TableCell> */}
            {props.tableData.HeadingData.map(el=>{
              return <TableCell align="right">{el}</TableCell>
            })}
          </TableRow>
        </TableHead>
        <TableBody>
          {rows.map(row => (
            <TableRow>
              {/* <TableCell align="right">{row}</TableCell> */}
              {row.map(el=>{
                console.log(Parse(el));
                return <TableCell align="right">{Parse(el)}</TableCell>
              })}
            </TableRow>
          ))}
        </TableBody>
      </Table>
"content":{
                "Title": "List Title",
                "HeadingData" :["Name", "Company", "City", "State"],
                "RowData" :[
                    ["Joe James", "Test Corp<br/>TestCorp", "Yonkers", "NY"],
                    ["John Walsh", "Test Corp", "Hartford", 1989],
                    ["Bob Herm", "Test Corp", "Tampa", "FL"],
                    ["James Houston", "Test Corp", "Dallas", "TX"],
                ]
            },

上面是表格数据,如果您看到公司有 html 标签“Test Corp
TestCorp”。

我无法解析它,因为它返回一个数组。

4

1 回答 1

0

我找到了解决方案。通过 html-react-parser 解析之前将每个单元格元素转换为字符串来解决。

于 2019-04-11T06:44:11.043 回答