【问题标题】:Datatables.net Server-Side pagination with Spring: How to select single row's ID?使用 Spring 的 Datatables.net 服务器端分页:如何选择单行的 ID?
【发布时间】: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


    【解决方案1】:

    我不确定 100%,但我认为如果你想使用 this.id,你可以在数据表初始化中使用下一个属性:

    rowId: 'staffId'
    

    staffId 是您用于设置 id 的列的名称。

    文档:

    https://datatables.net/reference/option/rowId

    【讨论】:

    • 谢谢!!!有用!现在,如果我可能会问,剩下的唯一一件事是:突出显示的选择保留在我单击的前几行上,尽管 id 仅从按预期单击的最后一行注销。单击新行时如何从上一行中删除突出显示?
    • 也许吧。在单击功能中使用js或jquery,在设置选定行之前,删除所有选定行(或仅删除前一个,因为您已存储它)。试试看告诉我:-)
    • 谢谢!起作用的是``` if ( $(this).hasClass('selected') ) { $(this).removeClass('selected'); } else { table.$('tr.selected').removeClass('selected'); $(this).addClass('selected'); } ``` 而不是切换线!谢谢你的帮助!:)
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2017-10-29
    • 2018-09-20
    • 2019-03-07
    • 2016-06-22
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    相关资源
    最近更新 更多