VOOZH about

URL: https://dzone.com/articles/fixed-width-sortable-tables

⇱ Fixed Width Sortable Tables Row with jQueryUI


Related

  1. DZone
  2. Data Engineering
  3. Databases
  4. Fixed Width Sortable Tables Row with jQueryUI

Fixed Width Sortable Tables Row with jQueryUI

By Apr. 27, 15 · Interview
Likes
Comment
Save
19.5K Views

Join the DZone community and get the full member experience.

Join For Free

When you use jQuery UI sortable function on a table I've noticed that it will collapse the width of the row you're dragging which can lead to a strange user experience. In this tutorial we are going to see how you can use a helper function to change the width of dragging rows back to the original width.

Have a look at the demo to see the difference.

Demo

jQuery Sortable is part of the jQuery UI library which can be found below.

jQuery Sortable

To define a table to have sortable rows all you have to do is apply the sortable method to the parent element of the row, which normal would be the table itself or ideally the table body.

<table>
<thead>
<tr>
<th>Film</th>
<th>Date</th>
<th>Rating</th>
</tr>
</thead>

<tbody>
<tr>
<td>The Shawshank Redemption</td>
<td>1994</td>
<td>9.2</td>
</tr>
</tbody>
</table>

Then you can make the table body rows sortable by using the following jQuery code.

$('table tbody').sortable();

One of the options you can use on the sortable method is helper property where you can define a function to run when dragging the display. Therefore we simply need to create a function that will reset the width of the table row by simply using the function below.

$('table tbody').sortable({
 helper: fixWidthHelper
}).disableSelection();

function fixWidthHelper(e, ui) {
 ui.children().each(function() {
 $(this).width($(this).width());
 });
 return ui;
}

Demo


Database

Published at DZone with permission of Paul Underwood. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Connect Existing Data to AI Retrieval: How to Build Production-Ready Search Without Rebuilding Core Systems
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: