最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Twitter Bootstrap Popover and AJAX - Stack Overflow

programmeradmin3浏览0评论

I been browsing SO for solutions on how to load an ajax content on bootstrap popover but can't find any decent solutions.

Here's what I have so far:

$(".btnCharge").click(function () {
    $("#boxPayment").fadeIn();
})
.popover({
    title: 'Advantages',
    html: 'true',
    content: function () {
        $.ajax({
            type: "POST",
            url: "Index.aspx/FindAdvantagesByCCID",
            data: '{"id": "' + 1 + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var json = jQuery.parseJSON(data.d);
                var html = '';
                $.each(json, function (i, item) {
                    html = html + '<a href="#"><i class="icon-ok"></i>' + item.Advantage + '</a><br />';
                });
            }
        });
    },
    placement: 'bottom',
    trigger: 'hover'
});

How can i add the ajax response to popover content? I tried "return" and doesnt work.

Any clean solutions?

I been browsing SO for solutions on how to load an ajax content on bootstrap popover but can't find any decent solutions.

Here's what I have so far:

$(".btnCharge").click(function () {
    $("#boxPayment").fadeIn();
})
.popover({
    title: 'Advantages',
    html: 'true',
    content: function () {
        $.ajax({
            type: "POST",
            url: "Index.aspx/FindAdvantagesByCCID",
            data: '{"id": "' + 1 + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var json = jQuery.parseJSON(data.d);
                var html = '';
                $.each(json, function (i, item) {
                    html = html + '<a href="#"><i class="icon-ok"></i>' + item.Advantage + '</a><br />';
                });
            }
        });
    },
    placement: 'bottom',
    trigger: 'hover'
});

How can i add the ajax response to popover content? I tried "return" and doesnt work.

Any clean solutions?

Share Improve this question edited Sep 19, 2018 at 18:21 Praveen Kumar Purushothaman 167k27 gold badges213 silver badges259 bronze badges asked Feb 6, 2013 at 18:54 AlexandreAlexandre 4153 gold badges6 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

Yes. it is possible. And it has been answered already.

Using data- attributes you can provide the URL, like here:

<a href="#" title="blabla" data-ajaxload="/test.php">blabla</a>

Now the handler:

$('*[data-ajaxload]').bind('hover',function(){
  var e=$(this);
  e.unbind('hover');
  $.get(e.data('ajaxload'),function(d){
      e.popover({content: d}).popover('show');
  });
});

unbind('hover') prevents loading data more than once and popover() binds a new hover event. If you want the data to be refreshed at every hover event, you should modify this a bit.

You can directly access to popover option data like this:

popoverData = $('.myPopover').data('popover')

So, you can do this to change popover content since it cant be changed once it is set.

if (popoverData = $('.myPopover').data('popover'))
{
    popoverData.options.content = newContent;
}

$('.myPopover').popover({ content: newContent, html:true, trigger:'hover' }).popover("show");

Your Html like this...

Id is present in your loop. You will use this value to retrieve additional information (ajax).

 <tbody data-bind="foreach:persons">
                <tr>
                    <td data-bind="text: id"></td>
                    <td data-bind="text: name"></td>
                    <td ><span data-bind="popOver: {content : $root.contentData, id: id}"</td>
                </tr>
            </tbody>

In your viewModel you have a variable - this variable is initially empty.

 var contentData = ko.observable();

Add a custom binding handler like:

> ko.bindingHandlers.popOver = {
>     init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
>         var value = ko.utils.unwrapObservable(valueAccessor());
> 
>         var options = {
>             placement: 'top',
>             title: "Title",
>             html: true,
>             trigger: 'manual',
>             content: value.content
>         };
> 
>         $(element).popover(options);
> 
> 
> 
> 
>         $(element).click(function () {
>             var id = value.id();
>             var response = myApp.GetAdditionalData(id);
>             value.content(response.content);
> 
>             $(this).popover('toggle');
>         });
>     } };

Outside your viewModel you will have a function making the ajax call:

 var GetAdditionalDataFromAjax = function (id) {
      return { "content": "some content returned by id"};
发布评论

评论列表(0)

  1. 暂无评论