【发布时间】:2019-07-29 01:50:54
【问题描述】:
我正在尝试利用 create-react-app 创建一个DataTables 表,我想通过 CDN 加载依赖项。
我已经用简单的 html/javascript 成功创建了这个表(见下文,codepen here)
<html>
<head>
<link href='https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css' rel='stylesheet'>
<script src="https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<script src='https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js'>
</script>
</head>
<body>
<table class="display" id="example" style="width:100%"></table>
<script>
$("#example").DataTable({
data: [
{
first_name: "Tiger",
last_name: "Nixon",
position: "System Architect"
},
{
first_name: "Garrett",
last_name: "Winters",
position: "Accountant"
}
],
columns: [
{ data: "first_name", title: "first" },
{ data: "position", title: "secon" }
]
});
</script>
</body>
</html>
我正在尝试使用 create-react-app 产生相同的结果。
我尝试了以下方法,并在此 codesandbox here 中进行了复制
在 public/index.html 中添加以下依赖项
<link href='https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css' rel='stylesheet' />
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src='https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js'></script>
将三个依赖添加到codesandbox中的外部资源中
下面是要渲染 DataTables 的组件
class App extends Component {
componentDidMount() {
$("#example").DataTable({
data: [
{
first_name: "Tiger",
last_name: "Nixon",
position: "System Architect"
},
{
first_name: "Garrett",
last_name: "Winters",
position: "Accountant"
}
],
columns: [
{ data: "first_name", title: "first" },
{ data: "position", title: "secon" }
]
});
}
render() {
return (
<div>
<table id="example" />
</div>
);
}
}
【问题讨论】:
-
您没有找到任何其他基于 React 的组件,或者您必须使用 jquery?
-
我必须使用 JQuery,因为我也需要 JQuery 来处理其他事情
-
好吧,我只能建议不要这样做,你应该参考文档:reactjs.org/docs/integrating-with-other-libraries.html和stackoverflow.com/questions/38518278/…,祝你好运。
标签: reactjs datatables create-react-app cdn