DataTables/TableTools - 单击某行时是否可以获得所选行的准确列表?

DataTables/TableTools - Is it possible to get an accurate list of the selected row(s) when a row is clicked?

我正在开发一个使用 DataTables 和 TableTools 的管理应用程序,并且希望能够在 DataTable 中的行被 selected 时实时 enable/disable 元素(例如按钮)。目的是根据用户 select 的项目以及其他因素确定向用户显示的管理选项,并让应用响应该确定。

我的问题是行的 on 'click' 回调在 selected 之前被调用,因此尝试在回调中调用 TableTools fnGetSelected 函数会导致数据不正确。这里有一个 jsfiddle 说明了我的意思:

HTML:

<div>
    <table id="my-table" class="table table-striped table-hover table-bordered" width="100%">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
        </thead>
        <tbody id="my-table-body"></tbody>
    </table>
</div>

Javascript:

var dtColumns = [
    [ "title", "Column Number 1" ], 
    [ "title", "Column Number 2" ]
]

var dtData = [
    ["Some Data", "More Data"],
    ["Some Data", "More Data"],
    ["Some Data", "More Data"],
    ["Some Data", "More Data"],
    ["Some Data", "More Data"]
]

$(document).ready(function() {
    $("#my-table").dataTable( {
        dom: 'T<"clear">lRrtip',
        tableTools: {
            "sRowSelect": "os",
            "aButtons": [{"sExtends": "select_none", "sButtonText": "Clear     Selection"}]
        },
        "data": dtData,
        "columns": dtColumns,
        fnRowCallback : function(row, data, index, indexFull) {
            $(row).on('click', function(e) {
                var ttInstance = TableTools.fnGetInstance("my-table")
                alert(ttInstance.fnGetSelected().length + " items selected")
            })
        }
    })
})

请注意,当单击一行时,在 table 之前打开警报表示该行已 selected,"selected" 行的数量表示行 select 在点击之前编辑,而不是在点击之后 select 编辑的行。

我考虑过根据点击的项目、点击方式(即 ctrl-click、shift-click)以及是否已经 select 在单击时编辑。对于简单的用例,一开始这似乎很容易,但当我开始深入研究时,我意识到它比我想处理的要复杂得多。

有没有什么方法可以在不重新实现 TableTools selection 功能的情况下完成我想做的事情?如果不是,我可能会放弃使用行 selection 的 "os" 样式的愿望,并做一些可以使手动确定 selected 行更容易的事情。

也许是这样的?

$(document).ready(function() {
    $("#my-table").dataTable( {
        dom: 'T<"clear">lRrtip',
        tableTools: {
            "sRowSelect": "os",
            "aButtons": [{"sExtends": "select_none", "sButtonText": "Clear Selection"}],
            fnRowSelected: function(nodes) {
                var ttInstance = TableTools.fnGetInstance("my-table");
                alert(ttInstance.fnGetSelected().length + " items selected");
            }
        },
        "data": dtData,
        "columns": dtColumns
    })
});

使用fnRowSelected

TableTools Initialisation