I am using bootstrap in my mvc project. I have an issue with bootstrap popover widget. I have created a custom knockout binding for popover widget, here the code :
Check fiddle
ko.bindingHandlers.popover = {
init: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
var options = ko.utils.unwrapObservable(valuesAccessor() || {});
options.html = true;
options.content = '<small class="text-info">' + 'Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here. ' + '</small>';
$(element).popover(options);
},
update: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
var extraOptions = allBindingsAccessor().popoverOptions;
$(element).popover(extraOptions.observable() ? "show" : "hide");
$(element).siblings('.popover').css({ width: '150px' });
//IF YOU UN-COMMENT BELOW 2 LINES THAN EVERY THINGS WORKS FINE
//if(extraOptions.observable())
//$(element).popover('show');
}
};
function vm() {
var self = this;
self.isVisible = ko.observable(false);
self.open = function () {
self.isVisible(!self.isVisible());
};
}
ko.applyBindings(new vm());
I want to initialize popover on any element with variable text message and with variable size. Every thing goes ok but when i change the default width of the popover than on first time when it open its position is not correct (please check the behavior in fiddle).
There was some line of code in the fiddle which if you uncomment than this issue solve. But i feel it is a hacky approach, i want some better way to handle if there is any ?
To create a popover, you need to add the data-bs-toggle="popover" attribute to an element. Whereas, popover's title and its content that would display upon trigger or activation can be specified using the title and data-bs-content attribute respectively. Here is the standard markup for adding a popover to a button.
You can either change the className to style, and it should work or you're going to have to add a const variable and hook it into the withStyle HOC. Then add the height and width to that const, and then call the class you made in that to the className. All these examples are on the material-ui site.
To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.
Here's a sample initialization that I use.
$(".property-price")
.popover({
trigger: "hover",
placement: 'bottom',
toggle : "popover",
content : "The from price is calculated ba....the dates selected.",
title: "How is the from price calculated?",
container: 'body',
template: '<div class="popover popover-medium"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>',
});
As you can see, it uses a custom template. The template uses a custom popover-medium class.
I then have a CSS selector
.popover-medium {
max-width: 350px;
}
You could also just set a style on the template class if you wanted.
DEMO
Try this I have edited your code
ko.bindingHandlers.popover = {
init: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
var options = ko.utils.unwrapObservable(valuesAccessor() || {});
options.html = true;
options.content = '<small class="text-info">' + 'Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here.Variable text goes here. ' + '</small>';
$(element).popover(options);
},
update: function (element, valuesAccessor, allBindingsAccessor, viewModel, bindingContext) {
var extraOptions = allBindingsAccessor().popoverOptions;
$(element).popover(extraOptions.observable() ? "show" : "hide");
//IF YOU UN-COMMENT BELOW 2 LINES THAN EVERY THINGS WORKS FINE
//if(extraOptions.observable())
//$(element).popover('show');
}
};
function vm() {
var self = this;
self.isVisible = ko.observable(false);
self.open = function () {
self.isVisible(!self.isVisible());
};
}
ko.applyBindings(new vm());
just add this css
.popover {
max-width: 150px;
width: auto;
}
Hope this helps Thank you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With