开发者

jQuery UI sortable and live() click problem -- need to click twice after sorting for click to register

I have a table I'm using jQuery UI's "sortable" on. In the table, I have rows consisting of a "drag handle" to grab and reorder the table, and cells with clickable items, like this:

<table id="test-table">
    <tbody>
    <tr>
        <td class="handle"><div class="ui-icon ui-icon-arrowthick-2-n-s" /></td>
        <td class="clickycell"><a href="#">Testing 1</a></td>
    </tr>
    <tr>
        <td class="handle"><div class="ui-icon ui-icon-arr开发者_JAVA百科owthick-2-n-s" /></td>
        <td class="clickycell"><a href="#">Testing 2</a></td></td>
    <tr>
        <td class="handle"><div class="ui-icon ui-icon-arrowthick-2-n-s" /></td>
        <td class="clickycell"><a href="#">Testing 3</a></td></td>
    </tr>
    </tbody>
</table>

In my code, I make the table sortable, and also use jQuery's live() to bind a click event to the clickable items, like this:

$(function() {
    /*
       Using live() because in my real code table rows are dynamically added.
       However, if I use click() instead, as in the commented-out code, it works
       fine, without any need to click twice.

    */
    // $(".clickycell a").click(function() {
    $(".clickycell a").live('click', function() {
        alert("Successful click");
        return false;
    });
    $("#test-table tbody").sortable({
        handle: "td.handle", /* Use the draggy handle to move, not the whole row */
        cursor: "move"
    });
});

I'm using live() because rows can be dynamically added to the table in the real code.

My problem is this: if I click on any of the clickable items before sorting, they work fine. After the user drags the rows to reorder them, though, I have to click twice for a click to register. After that second click, the clickable items go back to "normal", needing only one click, until the next time rows are dragged around.

If I use click() rather than live() -- as in the commented-out code -- then a single-click works fine at all times, but I'd rather use live(), as I said. And I'm curious as to why it's not working.

There's a live jsFiddle example here. Try dragging a row into a different position, then clicking on any of the "Testing..." links. In Firefox, at least, I need to click twice to get the "Successful click" alert.

Any ideas?


I was having this same issue with "live" and sortable when I found your post (delegate has the same problem as well). To follow up on the observation @Stephen made about clicking on the drag handle once before an item's click handler bound with live works without having to click twice... I've successfully used this workaround. For a hack, I don't find it too offensive.

$('table.demo tbody').sortable({
    handle: 'td.drag',
    stop: function(e,ui){
        $('td.drag', ui.item).click();
    }
});

I'm just issuing a click call to the drag handler as soon as the drag is complete, and now other items (in my row) that I have bound with live do not require clicking on twice.


It's unfortunate to end with the answer "It is a bug," but I guess sometimes that happens. Maybe I'll dust off my GitHub login and fork it to see if I can track it down and discover a fix.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜