0

I am using the following binding on each cell in a large HTML Excel like grid. Right now I am binding to every cell, is there a way to do this with lazy loading it on mouse hover over a certain delay so each cell doesnt need to activate it? If the mouse is over the cell for say 2 seconds, the tooltip binding activates and is shown. This tooltip is the bootstrap tooltip.

 ko.bindingHandlers.tooltip = {
        init: function (element, valueAccessor) {
            $(element).tooltip();

            ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
                $(element).tooltip("destroy");
            });
        }
    };
admdrew
  • 3,790
  • 4
  • 27
  • 39
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • I don't have time for a full fledged answer right this second but you could capture the mouse enter event, fire a setTimeout, and if it is still hovering after x amount of milliseconds show the tool tip. – PW Kad Apr 25 '14 at 20:07

1 Answers1

0

Instead of adding one event handler per cell, you could add a single event handler on the cells container. Given the markup

<div class="cell-container" data-bind="tooltipContainer: true">
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
</diV>

you could use this binding:

ko.bindingHandlers.tooltipContainer = {
    init: function (element, valueAccessor) {
        $(element).on('hover', '.cell', function() {
          $(this).tooltip();
        });

        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).tooltip("destroy");
        });
    }
};

If you want to add a timeout on top, this

ko.bindingHandlers.tooltipContainer = {
    init: function (element, valueAccessor) {
        $(element).on('hover', '.cell', function() {
          var $this    = $(this),
              callback = function() { $this.tooltip(); };

          setTimeout(callback, 2000);
        });

        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).tooltip("destroy");
        });
    }
};

should work.

janfoeh
  • 10,243
  • 2
  • 31
  • 56