Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The plugin <a href="http://www.trirand.com/blog/" rel="nofollow noreferrer">jqGrid</a> (latest versions at <a href="http://github.com/tonytomov/jqGrid/" rel="nofollow noreferrer">github repo</a>) has a feature to re-order columns - <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods&amp;s[]=column" rel="nofollow noreferrer">the documentation is here</a>. It does not appear to have drag/drop support for re-ordering columns however.</p> <p>Here is a custom working sample of draggable table columns using only <a href="http://jquery.com/" rel="nofollow noreferrer">jQuery</a> and <a href="http://jqueryui.com/" rel="nofollow noreferrer">jQuery UI</a> <a href="http://jqueryui.com/demos/sortable/" rel="nofollow noreferrer">sortable interaction</a>.</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;script src="jquery-1.4.2.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="jquery-ui-1.8.1.custom.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;link rel="stylesheet" href="jquery-ui-1.8.1.custom.css"&gt; &lt;style type="text/css"&gt; th { background-color: #e0e0e0; cursor: pointer; } .ui-state-highlight { height: 1.5em; line-height: 1.2em; } &lt;/style&gt; &lt;script type="text/javascript"&gt; $(function() { var $table1 = $('#table1'); var $table1Thead = $table1.find('thead'); var $table1Tbody = $table1.find('tbody'); var maxCols = 10; var maxRows = 50; // populate fake data for (var i = 1; i &lt;= maxCols; i++) { $table1Thead.append('&lt;th 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;row ' + x + ', column ' + i + '&lt;/td&gt;'; rowHtml += '&lt;td&gt;column ' + i + '&lt;/td&gt;'; } rowHtml += '&lt;/tr&gt;'; $table1Tbody.append(rowHtml); } // set an index helper on each th element $table1Thead.find('th').each(function() { var thElement = $(this); thElement.data('columnIndex', $table1Thead.find('th').index(thElement)); }); $table1Thead.sortable({ items: 'th', containment: 'parent', helper: 'clone', placeholder: 'ui-state-highlight', update: function(event, ui) { var prevPos = ui.item.data('columnIndex'); var newPos = $table1Thead.find('th').index(ui.item); // adjust all the row elements $table1Tbody.find('tr').find('td:eq(' + prevPos + ')').each(function() { var tdElement = $(this); var tdElementParent = tdElement.parent(); tdElement.remove(); tdElementParent.find('td:eq(' + newPos + ')').before(tdElement); }); // re-set an helper indexes $table1Thead.find('th').each(function() { var thElement = $(this); thElement.data('columnIndex', $table1Thead.find('th').index(thElement)); }); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;table id="table1"&gt; &lt;thead&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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