简体   繁体   English

Semantic-ui popup动态内容

[英]Semantic-ui popup Dynamic content

Semantic-ui ver. Semantic-ui ver。 2.0.8. 2.0.8。 I currently use the following method to load dynamic content in a pop-up 我目前使用以下方法在弹出窗口中加载动态内容

JAVASCRIPT JAVASCRIPT

var popupContent = null;
var popupLoading = '<i class="notched circle loading icon green"></i> wait...';

$('.vt').popup({
    inline: true,
    on: 'hover',
    exclusive: true,
    hoverable: true,
    html: popupLoading,
    variation: 'wide',
    delay: {
        show: 400,
        hide: 400
    },
    onShow: function(el) { // load data (it could be called in an external function.)
        var then = function(r) {
            if (r.status) {
                popupContent = r.data; // html string
            } else {
                // error
            }
        };
        var data = {
            id: el.dataset.id
        };
        ajax.data('http://example.site', data, then); // my custom $.ajax call
    },
    onVisible: function(el) { // replace popup content
        this.html(popupUserVoteContent);
    },
    onHide: function(el) { // replace content with loading
        this.html(popupLoading);
    }
});

HTML HTML

<h2 data-id="123" class="vt">10</h2>
<div class="ui popup" data-id="123"></div>

There 'sa way to simplify the whole process? 有简化整个过程的方法吗? For example with a element.popup ('refresh') after loading the new content? 例如,在加载新内容后使用element.popup('refresh')? I tried: 我试过了:

JAVASCRIPT JAVASCRIPT

...
if (r.status) {
   $('.ui.popup[data-id="123"]').html(r.data);
} 
...

but it does not work. 但它不起作用。 I also tried using (replace) data-content into h2.vt but nothing. 我也尝试使用(替换)数据内容到h2.vt但没有。

The only improvement that comes to mind is to make the code a little cleaner (you only really need the onShow event, which fires before the popup shows) and avoid using a global variable (popupContent). 想到的唯一改进是使代码更清晰(你只需要在弹出窗口显示之前触发的onShow事件)并避免使用全局变量(popupContent)。

That said, the main idea is mostly the same - when the popup is supposed to show, you replace its content with some fake content (the loading animation), then trigger $.ajax and update the popup content as soon as the request completes. 也就是说,主要思想大致相同 - 当弹出窗口应显示时,用一些假内容(加载动画)替换其内容,然后在请求完成后立即触发$.ajax并更新弹出内容。

var popupLoading = '<i class="notched circle loading icon green"></i> wait...';
$('.vt').popup({
    inline: true,
    on: 'hover',
    exclusive: true,
    hoverable: true,
    html: popupLoading,
    variation: 'wide',
    delay: {
        show: 400,
        hide: 400
    },
    onShow: function (el) { // load data (it could be called in an external function.)
        var popup = this;
        popup.html(popupLoading);
        $.ajax({
            url: 'http://www.example.com/'
        }).done(function(result) {
            popup.html(result);
        }).fail(function() {
            popup.html('error');
        });
    }
});

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

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