【发布时间】:2020-02-01 09:11:40
【问题描述】:
我正在开发一个前端为 React 和后端为 Spring Boot 的网络应用程序。我正在使用带有服务器端分页的数据表(弹簧控制器和所有这些)。它可以查看表格和不同的页面。但是,现在我想单击该行以选择其 ID。我尝试从数据表中遵循不同的示例,但它总是返回空。我还通过 npm 安装了 datatables.net-select-bs4,也许我可以以某种方式使用它?对不起,我是数据表的新手。 我现在拥有的(正面):
import React, { Component } from 'react'
import '../dataTables.min.css';
const $ = require('jquery');
$.DataTable = require('datatables.net-bs4');
export default class Table extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
var selected = [];
var table = $('#paginatedTable').DataTable( {
"dom": '<"data-table-wrapper"t>',
"processing": true,
"serverSide": true,
"pageLength": 5,
"ajax": {
"url": "/map-runner/api/calculations",
"data": function (data) {
}},
"columns": this.props.columns,
"rowCallback": function(row, data) {
if ( $.inArray(data.DT_RowId, selected) !== -1 ) {
$(row).addClass('selected');
}
}
});
$('#paginatedTable tbody').on('click', 'tr', function () {
var id = this.id;
var index = $.inArray(id, selected);
if (index === -1) {
selected.push(id);
} else {
selected.splice(index, 1);
}
$(this).toggleClass('selected');
console.log(table.rows( { selected: true } ));
} );
}
componentWillUnmount() {
$('.data-table-wrapper').find('table').DataTable().destroy(true);
}
render() {
return (
<div class="container">
<div class="starter-template">
<div class="table-responsive">
<table id="paginatedTable" class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Date Create</th>
<th>Date Update</th>
<th>User ID</th>
<th>ID Type</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
)
}
}
【问题讨论】:
标签: javascript jquery reactjs spring datatables