Ich habe heute Morgen verbrachte mit diesem zu spielen, und das ist, was ich habe und läuft, ich bin wirklich nicht sicher, dass es die beste Lösung ist, aber es funktioniert:
$(function() {
var table = $("#example").DataTable({
"order": [
[0, 'asc']
],
"paging": true,
"columns": [{
"visible": false
}, {
"orderable": false
}, {
"orderable": false
}, {
"orderable": false
}, {
"render": function(d) {
return $("<i/>", {
"class": "fa fa-caret-down swapable swapDown"
}).prop("outerHTML");
},
"orderable": false
}, {
"render": function(d) {
return $("<i/>", {
"class": "fa fa-caret-up swapable swapUp"
}).prop("outerHTML");
},
"orderable": false
}]
});
$('#example tbody').on('click', 'td', function(event) {
// We're only interested in cells with a class of swapable
if ($(this).find(".swapable")) {
// Helper variable
var _this = $(this).find(".swapable");
// Total number of rows (including hidden rows (zero based index))
var tableRows = table.data().length - 1;
// Index of current row
var rowIndex = table.row(this).index();
// Data of current row
var rowData = table.row(this).data();
// Index value of row (artifical because it's ours) - also tempval
var artificalIndex = ~~rowData[0];
/*
* If we're on the bottom row of the table and the direction of travel is downwards or if we're
* on the top row and the direction of travel is upwards then we need to just swap the top and
* bottom rows
*/
if(
(_this.hasClass("swapDown") && artificalIndex === tableRows)
||
(_this.hasClass("swapUp") && artificalIndex === 0)
){
var topIndex, bottomIndex;
table.rows().every(function(rowIdx, tableLoop, rowLoop) {
var data = this.data();
if(~~data[0] === 0){
topIndex = rowIdx;
}
if(~~data[0] === tableRows){
bottomIndex = rowIdx;
}
});
table.cell(topIndex, 0).data(tableRows);
table.cell(bottomIndex, 0).data(0);
}else{
var movingIndex, tempData;
table.rows().every(function(rowIdx, tableLoop, rowLoop) {
var data = this.data();
// Moving down
if (_this.hasClass("swapDown") && ~~data[0] === artificalIndex + 1) {
movingIndex = rowIdx;
tempData = data[0];
}
// Moving up
if (_this.hasClass("swapUp") && ~~data[0] === artificalIndex - 1) {
movingIndex = rowIdx;
tempData = data[0];
}
});
table.cell(rowIndex, 0).data(tempData);
table.cell(movingIndex, 0).data(artificalIndex);
}
table.draw(false);
}
});
});
This is a working JSFiddle.
Ich hatte einige Probleme mit dem Index, da ich nicht 100% sicher bin, dass der interne Index nach einem draw()
aktualisiert wird, also entschied ich mich, einen versteckten Index zu verwenden und auf diesem zu bestellen. Dies ist die HTML verwendet, um die Tabelle zu erstellen:
<div class="container">
<table cellpadding="0" cellspacing="0" border="0" class="table" id="example">
<thead>
<tr>
<th>Index</th>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Swap Down</th>
<th>Swap Up</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Trident</td>
<td>Internet Explorer 4.0</td>
<td>Win 95+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>1</th>
<td>Trident</td>
<td>Internet Explorer 5.0</td>
<td>Win 95+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>2</th>
<td>Trident</td>
<td>Internet Explorer 5.5</td>
<td>Win 95+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>3</th>
<td>Trident</td>
<td>Internet Explorer 6</td>
<td>Win 98+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>4</th>
<td>Trident</td>
<td>Internet Explorer 7</td>
<td>Win XP SP2+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>5</th>
<td>Trident</td>
<td>AOL browser (AOL desktop)</td>
<td>Win XP</td>
<td></td>
<td></td>
</tr>
<tr>
<th>6</th>
<td>Gecko</td>
<td>Firefox 1.0</td>
<td>Win 98+/OSX.2+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>7</th>
<td>Gecko</td>
<td>Firefox 1.5</td>
<td>Win 98+/OSX.2+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>8</th>
<td>Gecko</td>
<td>Firefox 2.0</td>
<td>Win 98+/OSX.2+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>9</th>
<td>Gecko</td>
<td>Firefox 3.0</td>
<td>Win 2k+/OSX.3+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>10</th>
<td>Gecko</td>
<td>Camino 1.0</td>
<td>OSX.2+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>11</th>
<td>Gecko</td>
<td>Camino 1.5</td>
<td>OSX.3+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>12</th>
<td>Gecko</td>
<td>Netscape 7.2</td>
<td>Win 95+/Mac OS 8.6-9.2</td>
<td></td>
<td></td>
</tr>
<tr>
<th>13</th>
<td>Gecko</td>
<td>Netscape Browser 8</td>
<td>Win 98SE+</td>
<td></td>
<td></td>
</tr>
<tr>
<th>14</th>
<td>Misc</td>
<td>Lynx</td>
<td>Text only</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
Hoffnung, die hilft, und ich würde schätzen jedes Feedback aus der Community, wie es auf verbessert werden könnte, wie ich bin mir ziemlich sicher, dass ich etwas fehle. ..?
Sie verwenden 'this' eine schreckliche Menge und ich denke, dass Sie wahrscheinlich' $ (this) 'stattdessen verwenden möchten. Das macht deine Konsole: http://jsfiddle.net/annoyingmouse/zqzzyscy/ aber ich bin nicht in der Lage, Dinge zu bewegen. Ich habe mich gefragt, ob es daran lag, dass Sie den Browsernamen voreingestellt haben, aber ich bin mir nicht sicher, ob ich es entfernt habe und es immer noch nicht so funktioniert, wie Sie es erwartet haben. – annoyingmouse
Ich versuche, die Zeile zu tauschen, aber ich kann nicht http: // jsfiddle.net/zqzzyscy/11/' – Raavanan
Wenn Ihre Tabelle nicht hart codiert war und einen willkürlichen Index hatte, würden Sie sortiert werden, wie Sie den Index tauschen und die Reihenfolge in der ausgeblendeten Spalte. Aber weil Sie einen hart codierten Tisch haben, ist es problematischer - ertragen Sie mit und ich werde ein JSFiddle aufarbeiten. – annoyingmouse