Show one popover and hide other popovers

前端 未结 13 1908
死守一世寂寞
死守一世寂寞 2020-12-24 04:45

i have several buttons and i need a popover for each.
i want it like this:
when my user click on one of them, i want others to be hidden. so only one popover is show

13条回答
  •  忘掉有多难
    2020-12-24 05:17

    I went for a combinations of approaches I've seen both on this thread and others. My requirements specify that:

    • Only one popover should be visible at a time
    • A click anywhere outside the popover should dismiss the popover
    • The popover should contain a control
    • Should not require rebinding the popover event

          function bindEvents() {
      
               setupPopupBinding();
      
               setupPopupDismissal();
          };
      
          function setupPopupBinding() {
                 $('.addSectionItem').popover({
                        html: true,
                        content: function () {
                              return createDropdowns($(this).data('sectionid'))[0].outerHTML;
                        },
                        placement: "right",
                        trigger: "click focus"
                 }).on("inserted.bs.popover", function (e) {
                      //initialize dropdown
                      setupSelect2();
                 }).on("hide.bs.popover", function (e) {
                       $('.select2-container--open').remove();
                 });
          }
      
          function setupPopupDismissal() {
                 $('body:not(.addSectionItem)').on('click', function (e) {
                 $('.addSectionItem').each(function () {
                 //the 'is' for buttons that trigger popups
                 //the 'has' for icons within a button that triggers a popup
                 if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
                 $(this).popover('hide');
                 $('.popover').has(e.target).remove();
              }
          });
          });
          }
      
          function createDropdowns(sectionId: number) {
                 var dropdown = $('
      ') .attr('Id', 'sectionPopupWrapper' + sectionId) .addClass('popupWrapper') .append($('
      提交评论

提交回复
热议问题