tags:

views:

39

answers:

4

I would like the following script to move a table row to the top of a table. I can't figure out how to reference the id of the first row, however, since the ID is set dynamically, I tried row.insertAfter(row.first()); but it's making the row disappear instead of moving it. Any ideas how to reference the top row?

    $(".top").click(function(){
        var row = $(this).parents("tr:first");
        if ($(this).is(".top")) {
            row.inserAfter(row.first());
        } else {
            return false;
        }
    });


<tbody id="sortable">
  <tr id="row1">
    <td><a href="" class="top">Top</a></td>
  </tr>
  <tr id="row2">
    <td><a href="" class="top">Top</a></td>
  </tr>
  <tr id="row3">
    <td><a href="" class="top">Top</a></td>
  </tr>
</tbody>
+1  A: 
$(".top").click(function(){
            var tr=this.parentNode.parentNode;
    tr.parentNode.insertBefore(tr.parentNode.firstChild,tr);
    tr.parentNode.removeChild(tr);   
}
Bick
`this.parentNode.firstChild === this`
Josh Stodola
`this.parentNode.parentNode.firstChild === <td>`
Josh Stodola
Aargh) At first i missed that .class is used not to <tr> and now - not to <td>(
Bick
+3  A: 

This works...

$(".top").click(function() {
  var thisRow = $(this).parent().parent();
  var firstRow = thisRow.parent().find("tr:first").not(thisRow);
  thisRow.insertBefore(firstRow);
});
Josh Stodola
Josh, I tried that, but no go. It's not doing anything now. The row remains where it is.
Mel
I did an alert(firstRow), and as a result I get [object Object]
Mel
@Mel Whoops you are right, I had a logic error in my parent traversal. Check the update, I included a link to a live demo that works.
Josh Stodola
Josh, that's great! It works... Issue I'm having now is I have an ajax call in the link... and now that ajax call will not fire...
Mel
@Mel This should not interfere with any other click handlers (AJAX included), as it does not explicitly do anything to prevent the propogation/bubbling
Josh Stodola
Josh, awesome! It does work. The one question I have is, how can I figure this function from an onSuccess event in my ajax?
Mel
Figure this function? I don't know what that means. If you are trying to split this function out so you can call it elsewhere, you'll have to find a way to replace references to `this` with a parameter reference to the anchor.
Josh Stodola
A: 
 var row = $(this).find("tr").eq(0);
Diodeus
+1  A: 

Do you wanna do something like this?

$(".top").click(function(){
    var $this = $(this); // make a reference to the current element
    $.ajax({
       url: 'your/ajax/url.php',
       success: function(data) {
          // do what you want with data,
          // and then move the row on top
          // of the table, like so
          var row = $this.parents("tr");
          var first_row = row.parent().find('tr:first');
          if (row && first_row && row != first_row) {
             row.insertBefore(first_row);
          }
       }
    });
    return false; // stay on the page
});

Check http://api.jquery.com/jQuery.ajax/ for more info

ArtBIT
It works really well, only in my real scenario I have an ajax call in the link... and now that will not fire anymore :/
Mel
You can use `$(selector).live('click',function(){/*...*/})` to bind the method to a click event, both current and all the future elements that have been added to the page, and thus make it persistent. See http://api.jquery.com/live/
ArtBIT
Wait, do you have ajax call defined in `onclick`? If so, replace the `return false;` with: ` } else { return false; }`
ArtBIT
I do, and that works too. Many thanks. I'm unsure which answer to choose now :D - A final question, how is it possible to forgo the onClick on just call it after onSuccess in my ajax call?
Mel
I will edit the answer to show you.
ArtBIT
Thanks, I would appreciate that!
Mel
That's great, ArtBIT. Thank you. The one issue with the code is, however, that when the item is already on top, clicking the up top again again makes it disappear from the table. This has nothing to do with the modification for onSuccess that you added. I tried it with and without.
Mel
Okay, how about now?
ArtBIT