3

I have an HTML table (#view-page-table) with close to the following HTML:

<table id="view-page-table">
    <thead>
        <th>Header1</th>
        <th>Header2</th>
    </thead>
    <tbody>
        <tr><td>Group 1</td><td>Group 1</td></tr>
        <tr><td>Group 1</td><td>Group 1</td></tr>

        <tr><td>Group 2</td><td>Group 2</td></tr>
        <tr><td>Group 2</td><td>Group 2</td></tr>

        <tr><td>Group 3</td><td>Group 3</td></tr>
        <tr><td>Group 3</td><td>Group 4</td></tr>
    </tbody>
</table>

My jQuery is simple right now:

function reOrder() {
    $("#view-page-table tbody").sortable({
        helper: function(e, ui) {
             ui.children().each(function() {
                 $(this).width($(this).width());
             });
             return ui;
        }
    }).disableSelection();
}

It seems to work fine (the helper function is to maintain cell width while moving). The only problem is, I need to move two <tr>'s at a time. In other words, the user needs to drag all of group one, two, or three without being able to drag just one row. I tried to add a div in the table but I guess that's a no-no.

3 Answers 3

6

I think I have solution, but not optimum. try to make tabel inside

<tbody>
        <tr><td>Group 1</td><td>Group 1</td></tr>
        <tr><td>Group 1</td><td>Group 1</td></tr>
</tbody>
<tbody>
        <tr><td>Group 2</td><td>Group 2</td></tr>
        <tr><td>Group 2</td><td>Group 2</td></tr>
</tbody>
<tbody>
        <tr><td>Group 3</td><td>Group 3</td></tr>
        <tr><td>Group 3</td><td>Group 4</td></tr>
</tbody>

And replace your script with this one

function reOrder() {
    $("#view-page-table").sortable({
        helper: function(e, ui) {
             ui.children().each(function() {
                 $(this).width($(this).width());
             });
             return ui;
        },
        handle: "tbody"
    }).disableSelection();
}

Best of Luck

Sign up to request clarification or add additional context in comments.

2 Comments

The above code didn't work for me, I had to replace handle: "tobdy" by items: "tbody". Otherwise, very nice solution!
Thanks for this answer. Using something similar, I achieved technically correct results, but with extreme formatting issues. I eventually abandoned my effort and relooked at how I approached the issue altogether. Thanks.
2

Try wrapping the related rows in their own tbody tag. You can use multiple tbody tags in a single table. This is an ideal use case.

1 Comment

Awesome answer! Had no idea that was possible!
1

The Ahmed Assaf solution need to be modified as below

HTML Code

<table id="sort" border="1" width="500">  
    <thead>  
    <tr><th>Col 1</th><th>Col 2</th><th>Col 3</th></tr>  
    </thead>  
    <tbody class="red">
            <tr><td>Group 1</td><td>Group 1</td><td>Group 1</td></tr>
            <tr><td>Group 1</td><td>Group 1</td><td>Group 1</td></tr>
    </tbody>                                    
    <tbody>                                     
            <tr><td>Group 2</td><td>Group 2</td><td>Group 2</td></tr>
            <tr><td>Group 2</td><td colspan="2">Group 2 and Group 2</td></tr>
    </tbody>                                    
    <tbody class="blue">                                     
            <tr><td>Group 3</td><td>Group 3</td><td>Group 3</td></tr>
            <tr><td>Group 3</td><td>Group 4</td><td>Group 4</td></tr>
    </tbody>
</table> 

Script Code

// Return a helper with preserved width of cells  
var fixHelper = function(e, ui) {  
  ui.children().children().each(function() { 
    $(this).width($(this).width()); 
  });  
  return ui;  
};

$("#sort").sortable({
        helper: fixHelper,
        items: "tbody"
    }).disableSelection();

In the fixHelper function the code line

ui.children().each(function()
need to be replace with
ui.children().children().each(function()
to catch the TD width value.

And the line

handle: "tbody"
need to be replace with
items: "tbody"
has indicated by rlanvin in his comment

Demo on jsfiddle

Best Regard NexusFred

2 Comments

which jquery-ui version is required to set helper with parameter? for me param 'ui' is giving error
Sorry for the delay. In this demo I use the JQuery UI 1.11.4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.