简体   繁体   English

Twitter Bootstrap Popover和AJAX

[英]Twitter Bootstrap Popover and AJAX

I been browsing SO for solutions on how to load an ajax content on bootstrap popover but can't find any decent solutions. 我一直在浏览如何在bootstrap popover上加载ajax内容的解决方案,但找不到任何合适的解决方案。

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? 如何在弹出窗口内容中添加ajax响应? I tried "return" and doesnt work. 我试过“返回”并且不起作用。

Any clean solutions? 有清洁的解决方案

Yes. 是。 it is possible. 有可能的。 And it has been answered already. 它已经得到了回答

Using data- attributes you can provide the URL, like here: 使用data-属性,您可以提供URL,如下所示:

<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. unbind('hover')阻止多次加载数据,popover()绑定一个新的悬停事件。 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: 您可以直接访问popover选项数据,如下所示:

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... 你的Html喜欢这样......

Id is present in your loop. Id存在于您的循环中。 You will use this value to retrieve additional information (ajax). 您将使用此值来检索其他信息(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. 在你的viewModel中你有一个变量 - 这个变量最初是空的。

 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: 在viewModel之外,你将有一个函数进行ajax调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM