Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another approach to reorder columns with Jquery UI Sortable: <a href="http://jsfiddle.net/pg7wH/" rel="nofollow">http://jsfiddle.net/pg7wH/</a></p> <p>Needs</p> <ul> <li>jQuery (tested with 1.7.2 - 2.0)</li> <li>jQuery UI (tested with 1.8.18 - 1.10.2)</li> </ul> <p>HTML:</p> <pre><code>&lt;button id="getSorting"&gt;Get sorting&lt;/button&gt;&lt;input id="showSorting" /&gt; &lt;table id="table1"&gt; &lt;thead class="ui-state-default"&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>JS:</p> <pre><code>$(function() { var $table1 = $('#table1'); var $table1Thead = $table1.find('thead'); var $table1Tbody = $table1.find('tbody'); var startPos; var oldPos; // populate fake data var maxCols = 10; var maxRows = 50; for (var i = 1; i &lt;= maxCols; i++) { $table1Thead.append('&lt;th sort="' + i + '" id="column[' + i + ']"&gt;column ' + i + '&lt;/th&gt;'); } var rowHtml; for (var x = 1; x &lt;= maxRows; x++) { rowHtml = '&lt;tr&gt;'; for (var i = 1; i &lt;= maxCols; i++) { //rowHtml += '&lt;td&gt;' + i + ' - ' + x + '&lt;/td&gt;'; rowHtml += '&lt;td&gt;col ' + i + '&lt;/td&gt;'; } rowHtml += '&lt;/tr&gt;'; $table1Tbody.append(rowHtml); } // Show sorting $("button#getSorting").click(function() { $('#showSorting').val($table1Thead.sortable('toArray', { attribute: "sort" } )) console.log($table1Thead.sortable('toArray', { attribute: "sort" } )) }) // The sorting $table1Thead.sortable({ axis: "x" , items: 'th', containment: 'parent', cursor: 'move', helper: 'clone', distance: 5, opacity: 0.5, placeholder: 'ui-state-highlight', start: function(event, ui) { startPos = $table1Thead.find('th').index(ui.item); oldPos = startPos; }, change: function(event, ui) { // Get position of the placeholder var newPos = $table1Thead.find('th').index($table1Thead.find('th.ui-state-highlight')); // If the position is right of the original position, substract it by one in cause of the hidden th if(newPos&gt;startPos)newPos--; // move all the row elements //console.log('Move: 'oldPos+' -&gt; '+newPos); $table1Tbody.find('tr').find('td:eq(' + oldPos + ')').each(function() { var tdElement = $(this); var tdElementParent = tdElement.parent(); if(newPos&gt;oldPos)// Move it the right tdElementParent.find('td:eq(' + newPos + ')').after(tdElement); else// Move it the left tdElementParent.find('td:eq(' + newPos + ')').before(tdElement); }); oldPos = newPos; } }); }); </code></pre> <p>Thanks to Nate Pinchot for his example.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload