Question

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");
            });
        }
    };
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top