Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we use drag and drop to do row ranking ? #636

Closed
Waynezhi opened this issue Jun 2, 2014 · 12 comments
Closed

Can we use drag and drop to do row ranking ? #636

Waynezhi opened this issue Jun 2, 2014 · 12 comments

Comments

@Waynezhi
Copy link

Waynezhi commented Jun 2, 2014

i use pager, math, filter widgets, and edit / append new line
and if i can use sub-rows for a parent row (do the same thing like drag and drop, filtering, edit etc.) ?

thx

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2014

Hi @Waynezhi!

I think it might be difficult to add drag & drop functionality to the table rows because ideally the script would need to use delegated events and I don't know of any existing scripts that do that.

If a script does exist, or if I have time to find & modify one, then after the drag & drop of child rows for a parent row, an "update" method would need to be used to retain the order.

If parent rows were allowed to be drag and dropped, then what would be expected to happen if a column were sorted?

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2014

I stand corrected. It looks like jQuery UI drag & drop does use delegated events. I'll try to work on a demo for you later today.

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2014

Ok here is what I put together (demo):

The only problem is that child rows can be moved from one parent to another... so it is possible to break the parent/child relationship and/or form new relationships (which may be a bad thing)

$(function() {

    var $table = $('.tablesorter');

    // hide child rows & make draggable
    $table.find('.tablesorter-childRow')
        .find('td')
        .droppable({
            drop: function(event, ui) {
                ui.draggable
                    .css({ left : 0, top : 0 })
                    .parent().after( $(this).parent() );
                $table.trigger('update');
            }
        })
        .draggable()
        .hide();

    $table
        .tablesorter({
            theme : 'blue',
            // this is the default setting
            cssChildRow: "tablesorter-childRow"
        })
        .tablesorterPager({
            container: $("#pager"),
            positionFixed: false
        })
        .delegate('.toggle', 'click' ,function(){
            $(this)
                .closest('tr')
                .nextUntil('tr.tablesorter-hasChildRow')
                .find('td').toggle();
            return false;
        });

});

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2014

Yeah, that demo is not pretty... it really breaks the table is a lot of cases. At least we know it's possible to drag & drop though LOL.

@TheSin-
Copy link
Collaborator

TheSin- commented Jun 3, 2014

screen shot 2014-06-03 at 9 24 27 am

this is fun in safari, such odd behaviour

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2014

Try this demo... it makes sure that we are only moving child row cells

// hide child rows & make draggable
$table.find('.tablesorter-childRow')
    .find('td')
    .droppable({
        drop: function(event, ui) {
            ui.draggable
                .css({ left : 0, top : 0 })
                .closest('tr.tablesorter-hasChildRow').after( $(this).closest('tr.tablesorter-childRow') );
            $table.trigger('update');
        }
    })
    .draggable()
    .hide();

@Waynezhi
Copy link
Author

Waynezhi commented Jun 3, 2014

thx @Mottie, look better

@TheSin-
Copy link
Collaborator

TheSin- commented Jun 3, 2014

in safari it now does not work at all, they just unclip and never snap, at lest before if i got over a row it'd push it down etc etc. Just reporting ;)

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2014

Ok, I think I have most of the bugs worked out (demo). There are some limitations though:

  • Only child rows associated with a given parent row may be sorted, so in that demo look for a parent row with more than one child (e.g. SO71777 on page 1)
  • When dragging & dropping, only the child row below can be moved up. This is a limitation of the code, otherwise it would get even more complicated.
  • Firefox does not show a draggable element because Firefox does not allow the absolute or relative positioning of table cells.
$(function() {

    var $table = $('.tablesorter');

    // hide child rows & make draggable
    $table.find('.tablesorter-childRow')
        .find('td')
        .droppable({
            accept: '.draggingSiblings',
            drop: function(event, ui) {
                if ($(this).closest('tr').length){
                    $(this).closest('tr').before(
                        ui.draggable
                            .css({ left: 0, top: 0 })
                            .parent()
                            .removeClass('draggingRow')
                        );
                    $table
                        .find('.draggingSiblingsRow')
                        .removeClass('draggingSiblingsRow')
                        .find('.draggingSiblings')
                        .removeClass('draggingSiblings');
                    $table.trigger('update');
                } else {
                    return false;
                }
            }
        })
        .draggable({
            revert: "invalid",
            start: function( event, ui ) {
                $(this)
                    .parent()
                    .addClass('draggingRow')
                    .prevUntil('.tablesorter-hasChildRow')
                    .nextUntil('tr:not(.tablesorter-childRow)')
                    .addClass('draggingSiblingsRow')
                    .find('td')
                    .addClass('draggingSiblings');
            }
        })
        .hide();

    $table
        .tablesorter({
            theme : 'blue',
            // this is the default setting
            cssChildRow: "tablesorter-childRow"
        })
        .tablesorterPager({
            container: $("#pager"),
            positionFixed: false
        })
        .delegate('.toggle', 'click' ,function(){
            $(this)
                .closest('tr')
                .nextUntil('tr.tablesorter-hasChildRow')
                .find('td').toggle();
            return false;
        });

});
  • Thanks to @TheSin- for helping me test it!

@Mottie Mottie added the Demo label Jun 3, 2014
@Waynezhi
Copy link
Author

Waynezhi commented Jun 4, 2014

@Mottie its possible to have a demo of drag & drop parent lines ? only zebra widget, without other widgets, just want to save ordering after dropped.

@Mottie
Copy link
Owner

Mottie commented Jun 4, 2014

You could use jQuery UI sortable - see this Stackoverflow question & demo.

@Waynezhi
Copy link
Author

Waynezhi commented Jun 4, 2014

thx a lot @Mottie

@Waynezhi Waynezhi closed this as completed Jun 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants