简体   繁体   English

从弹出窗口访问表格行

[英]Accessing Table Row From Popover

I currently have a bootstrap popover holding a button.我目前有一个带有按钮的引导弹出窗口。 The popover shows only when the mouse is over a table's tr .仅当鼠标悬停在表格的tr时才会显示弹出窗口。

What I want to do is to be able to access the elements for that row, is this possible.我想要做的是能够访问该行的元素,这可能吗。

Popover code:弹出代码:

$('.popup').popover(
    {
      placement: 'top',
      trigger: 'manual',
      delay: { show: 350, hide: 100 },
      html: true,
      content: $('#shortcuts').html(),
      title: "Quick Tasks"
    }
  ).parent().delegate('#quickDeleteBtn', 'click', function() {
      alert($(this).closest('tr').children('td').text()); // ???
});
    var timer,
        popover_parent;
    function hidePopover(elem) {
        $(elem).popover('hide');
    }
    $('.popup').hover(
        function() {
          var self = this;
          clearTimeout(timer);
          $('.popover').hide(); //Hide any open popovers on other elements.
          popover_parent = self
          //$('.popup').attr("data-content","WOOHOOOO!");
          $(self).popover('show');            
        }, 
        function() {
          var self = this;
          timer = setTimeout(function(){hidePopover(self)},250);                 
    });
    $(document).on({
      mouseenter: function() {
        clearTimeout(timer);
      },
      mouseleave: function() {
        var self = this;
        timer = setTimeout(function(){hidePopover(popover_parent)},250); 
      }
    }, '.popover');

HTML: HTML:

<div class="hide" id="shortcuts">
    <a href="javascript:void(0);" id="quickDeleteBtn" class="btn btn-danger">Delete</a>
    </div>

javascript that implements popover on row:在行上实现 popover 的 javascript:

rows += '<tr class="popup datarow" rel="popover">';

Does anyone know what I'm doing wrong here and how I am supposed to access the child elements of the tr I'm hovering over?有谁知道我在这里做错了什么以及我应该如何访问我悬停的tr的子元素?

JSFiddle: http://jsfiddle.net/C5BjY/8/ JSFiddle: http : //jsfiddle.net/C5BjY/8/

For some reason I couldn't get closest() to work as it should.出于某种原因,我无法让closest()正常工作。 Using parent().parent() to get to the containing .popover divider, then using prev() to get the previous tr element seems to do the trick however.使用parent().parent()获取包含.popover分隔符,然后使用prev()获取前一个tr元素似乎可以解决问题。

Just change:只是改变:

alert($(this).closest('tr').children('td').text());

To:到:

alert($(this).parent().parent().prev('tr').children('td').text());

JSFiddle example . JSFiddle 示例


As a side note, as your Fiddle uses jQuery 1.10.1 you should change delegate() to on() :作为旁注,当您的 Fiddle 使用 jQuery 1.10.1 时,您应该将delegate()更改为on()

on('click', '#quickDeleteBtn', function(index) { ... });

Here I have fixed it.在这里,我已经修复了它。 You just have to pass the container option in which the popover element is added for the popover您只需要传递为 popover 添加 popover 元素的 container 选项

$('.popup').each(function (index) {
    console.log(index + ": " + $(this).text());
    $(this).popover({
        placement: 'top',
        trigger: 'manual',
        delay: {
            show: 350,
            hide: 100
        },
        html: true,
        content: $('#shortcuts').html(),
        title: "Quick Tasks",
        container: '#' + this.id
    });
});

In your button click alert, $(this) refers to the button itself.在您的按钮单击警报中, $(this) 指的是按钮本身。 In the DOM hierarchy, the popover html is nowhere near your hovered tr.在 DOM 层次结构中,popover html 远不及您悬停的 tr。

Add a handler to the list item to store itself in a global variable and access that from the click event.向列表项添加处理程序以将其自身存储在全局变量中并从单击事件访问该变量。 See the forked fiddle here .这里看到分叉的小提琴。

First we declare a global (at the very top):首先我们声明一个全局(在最顶部):

var hovered;

Then we add a mouseover handler to the list item.然后我们向列表项添加一个鼠标悬停处理程序。 Note that using 'on' means every newly generated list item will also receive this handler:请注意,使用 'on' 意味着每个新生成的列表项也将收到此处理程序:

$('body').on('mouseover', '.popup', function() {
      hovered = $(this);   
 });

Then we can alert the needed data from within the button click event:然后我们可以从按钮单击事件中提醒所需的数据:

alert(hovered.text());

See here JS Fiddle看这里JS Fiddle

by removing the delegate and using the id to find the button and attaching it to a click handler by making the popover makes it easier to track it通过删除委托并使用 id 查找按钮并将其附加到单击处理程序,使弹出窗口更容易跟踪它

$(self).popover('show');
$('#quickDeleteBtn').click(function(){
    alert($(self).text());
});

also note还要注意

$('#shortcuts').remove();

because you were using the button in the popover with the same ID in the #shortcuts we couldn't select it first, now we remove it we can因为您在#shortcuts中使用了弹出窗口中具有相同 ID 的按钮, #shortcuts我们无法先选择它,现在我们可以删除它

You already have the correct element in your code.您的代码中已经包含正确的元素。 Just reuse the popover_parent variable and you are all set :) FIDDLE只需重用popover_parent变量就可以了:) FIDDLE

alert($(popover_parent).text());

Or you could do something around like this :或者你可以做这样的事情:

$('.popup').hover(

    function () {
        var self = this;
        clearTimeout(timer);
        $('.popover').hide(); //Hide any open popovers on other elements.
        $('#quickDeleteBtn').data('target', '');
        popover_parent = self;

        //$('.popup').attr("data-content","WOOHOOOO!");
        $('#quickDeleteBtn').data('target', $(self));
        $(self).popover('show');

    },

    function () {
        var self = this;
        timer = setTimeout(function () {
            $('#quickDeleteBtn').data('target', '');
            hidePopover(self)
        }, 250);
    });
    $(document).on({
        mouseenter: function () {
            clearTimeout(timer);
        },
        mouseleave: function () {
            var self = this;
            timer = setTimeout(function () {
                $('#quickDeleteBtn').data('target', '');
                hidePopover(popover_parent)
            }, 250);
        }
    }, '.popover');

I just store the element clicked in your #quickDeleteBtn then use the link.我只是存储在 #quickDeleteBtn 中单击的元素,然后使用链接。 FIDDLE HERE在这里小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM