この質問と似ているが同じではないクエリがあります。
違いは、列ではなく行のデータ属性でフィルタリングしようとしていることです。要素ではtr
なく、td
要素。これには主キーが含まれており、重複を削除しようとしています。重複行は実際の列データが異なる場合があるため、そのようにすることはできません。キーは、アンダースコアで区切られた 3 つの数値フィールドの組み合わせです。
<tr data-primarykey="12345678_12_34"><td>...
次に、javascript
ページの読み込み時に呼び出される関数内で...
var rows = [];
$.fn.DataTable.ext.search.push(
function( settings, data, dataIndex, rowObj, counter ) {
if(counter === 0) rows = [];
// I want rowkey to be the data-primarykey attribute of the tr element
var rowkey = table.row(dataIndex).data('primarykey');
// If we've seen the row before (already in the array), filter it out.
// Otherwise, return true and add it to the array
if ($.inArray(rowkey,rows) > -1) return false;
else {
rows.push(rowkey);
return true;
}
}
);
問題の一部は、table
オブジェクトがどこにも定義されていないことだと思います。このフィルターは複数のテーブルに適用されるため、可能であれば、現在フィルター処理されているテーブルである必要があります。各テーブルには固有の がありますが、属性id
を共有しています。class
2016/12/12更新
実際のデータ属性を使用するバージョンで、ある程度の進歩を遂げました。このバージョンには動作するtable
DataTable オブジェクトがあり、行を正しく取得して jquery に変換すると思います。ただし、data-attribute を取得しようとすると、undefined
.
$.fn.DataTable.ext.search.push(
function( settings, data, dataIndex, rowObj, counter ) {
if(counter === 0) rowkeys = [];
// I want rowkey to be the data-primarykey attribute of the tr element
var tableID = $(settings.nTable).attr("id");
var table = $('#'.tableID).DataTable(); // Correct DataTable
//var row = table.rows(dataIndex).nodes().to$(); // Correct row?
var rowkey = table.rows(dataIndex).nodes().to$().data("primarykey");
console.log('key: '+rowkey); // Shows 'key: undefined' in console.
// If we've seen the row before (already in the array), filter it out.
// Otherwise, return true and add it to the array
if ($.inArray(rowkey,rowkeys) > -1) return false;
else {
rowkeys.push(rowkey);
return true;
}
}
);