fairy
fairy
  • 发布:2016-07-14 17:52
  • 更新:2016-07-16 14:44
  • 阅读:1023

组件【选项卡切换+上拉加载】并不能初始化,是为什么呀?

分类:MUI

组件【选项卡切换+上拉加载】并不能初始化,是为什么呀?

2016-07-14 17:52 负责人:无 分享
已邀请:
赵梦欢

赵梦欢 - 专注前端,乐于分享!

最终的问题的错误的使用了预加载,同这个问题:http://ask.dcloud.net.cn/question/20897

k***@126.com

k***@126.com

这是main页面
//创建子页面,首个1. 选项卡页面显示,其它均隐藏;
$.plusReady(function() {
var self = plus.webview.currentWebview();

                for (var i = 0; i < 1; i++) {  
                    var temp = {};  
                    var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);  
                    if (i > 0) {  
                        sub.hide();  
                    } else {  
                        temp[subpages[i]] = "true";  
                        $.extend(aniShow, temp);  
                    }  
                    self.append(sub);  
                }  
            });  

子页面中
if ($.os.plus) {
$.plusReady(function() {
setTimeout(function() {
$('#pullrefresh').pullRefresh().pullupLoading();
}, 1000);
});
} else {
$.ready(function() {
$('#pullrefresh').pullRefresh().pullupLoading();
});
}

子页面list页面就是上啦加载,main页面有选项卡,list为首选项卡,可是第一次进来的时候数据不能初始化,不论写在哪都不行。

  • 赵梦欢

    参考tab-webview-main.html和tab-webview-subpage-chat.html页面

    2016-07-16 12:23

  • k***@126.com

    回复 赵梦欢:我就是这么写的 关键是我的数据是服务端获取的 初始化的时候是脚本动态加载的,现在就是初次加载父页面,子页面数据根本加载不出来

    2016-07-16 12:29

  • 赵梦欢

    回复 k***@126.com:首先排查ajax能够取到数据吗?建议打个测试工程。

    2016-07-16 12:35

  • 赵梦欢

    回复 k***@126.com:另外是不是经过了预加载,建议看看这个问题:http://ask.dcloud.net.cn/question/20897

    2016-07-16 14:11

  • k***@126.com

    回复 赵梦欢:嗯就是预加载问题,看了这篇文章帮助很大。谢谢!!

    2016-07-16 14:18

赵梦欢

赵梦欢 - 专注前端,乐于分享!

tab-webview-main.html主页面:

<!DOCTYPE html>  
<html>  

    <head>  
        <meta charset="utf-8">  
        <title>Hello MUI</title>  
        <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">  
        <meta name="apple-mobile-web-app-capable" content="yes">  
        <meta name="apple-mobile-web-app-status-bar-style" content="black">  

        <link rel="stylesheet" href="../css/mui.min.css">  
        <style>  
            html,  
            body {  
                background-color: #efeff4;  
            }  
        </style>  
    </head>  

    <body>  
        <header class="mui-bar mui-bar-nav">  
            <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>  
            <h1 id="title" class="mui-title">首页</h1>  
        </header>  
        <nav class="mui-bar mui-bar-tab">  
            <a id="defaultTab" class="mui-tab-item mui-active" href="tab-webview-subpage-about.html">  
                <span class="mui-icon mui-icon-home"></span>  
                <span class="mui-tab-label">首页</span>  
            </a>  
            <a class="mui-tab-item" href="tab-webview-subpage-chat.html">  
                <span class="mui-icon mui-icon-email"><span class="mui-badge">9</span></span>  
                <span class="mui-tab-label">消息</span>  
            </a>  
            <a class="mui-tab-item" href="tab-webview-subpage-contact.html">  
                <span class="mui-icon mui-icon-contact"></span>  
                <span class="mui-tab-label">通讯录</span>  
            </a>  
            <a class="mui-tab-item" href="tab-webview-subpage-setting.html">  
                <span class="mui-icon mui-icon-gear"></span>  
                <span class="mui-tab-label">设置</span>  
            </a>  
        </nav>  
        <script src="../js/mui.min.js"></script>  
        <script type="text/javascript" charset="utf-8">  
             //mui初始化  
            mui.init();  
            var subpages = ['tab-webview-subpage-about.html', 'tab-webview-subpage-chat.html', 'tab-webview-subpage-contact.html', 'tab-webview-subpage-setting.html'];  
            var subpage_style = {  
                top: '45px',  
                bottom: '51px'  
            };  

            var aniShow = {};  

             //创建子页面,首个选项卡页面显示,其它均隐藏;  
            mui.plusReady(function() {  
                var self = plus.webview.currentWebview();  
                for (var i = 0; i < 4; i++) {  
                    var temp = {};  
                    var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);  
                    if (i > 0) {  
                        sub.hide();  
                    }else{  
                        temp[subpages[i]] = "true";  
                        mui.extend(aniShow,temp);  
                    }  
                    self.append(sub);  
                }  
            });  
             //当前激活选项  
            var activeTab = subpages[0];  
            var title = document.getElementById("title");  
            //选项卡点击事件  
            mui('.mui-bar-tab').on('tap', 'a', function(e) {  
                var targetTab = this.getAttribute('href');  
                if (targetTab == activeTab) {  
                    return;  
                }  
                //更换标题  
                title.innerHTML = this.querySelector('.mui-tab-label').innerHTML;  
                //显示目标选项卡  
                //若为iOS平台或非首次显示,则直接显示  
                if(mui.os.ios||aniShow[targetTab]){  
                    plus.webview.show(targetTab);  
                }else{  
                    //否则,使用fade-in动画,且保存变量  
                    var temp = {};  
                    temp[targetTab] = "true";  
                    mui.extend(aniShow,temp);  
                    plus.webview.show(targetTab,"fade-in",300);  
                }  
                //隐藏当前;  
                plus.webview.hide(activeTab);  
                //更改当前活跃的选项卡  
                activeTab = targetTab;  
            });  
             //自定义事件,模拟点击“首页选项卡”  
            document.addEventListener('gohome', function() {  
                var defaultTab = document.getElementById("defaultTab");  
                //模拟首页点击  
                mui.trigger(defaultTab, 'tap');  
                //切换选项卡高亮  
                var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");  
                if (defaultTab !== current) {  
                    current.classList.remove('mui-active');  
                    defaultTab.classList.add('mui-active');  
                }  
            });  
        </script>  
    </body>  
</html>

tab-webview-subpage-chat.html子页面:

<!DOCTYPE html>  
<html>  

    <head>  
        <meta charset="utf-8">  
        <title>Hello MUI</title>  
        <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">  
        <meta name="apple-mobile-web-app-capable" content="yes">  
        <meta name="apple-mobile-web-app-status-bar-style" content="black">  
        <link rel="stylesheet" href="../css/mui.min.css">  
        <style>  
            html,  
            body {  
                background-color: #efeff4;  
            }  
            .title {  
                padding: 20px 15px 10px;  
                color: #6d6d72;  
                font-size: 15px;  
                background-color: #fff;  
            }  
        </style>  
    </head>  

    <body>  
        <div id="pullrefresh" class="mui-content mui-scroll-wrapper">  
            <div class="mui-scroll">  
                <div class="title">  
                    这是webview模式选项卡中的第2个子页面,该页面展示一个支持下拉刷新、上拉加载的消息列表  
                </div>  
                <ul class="mui-table-view mui-table-view-chevron">  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 1</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 2</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 3</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 4</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 5</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 6</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 7</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 8</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 9</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 10</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 11</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 12</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 13</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 14</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 15</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 16</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 17</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 18</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 19</a>  
                    </li>  
                    <li class="mui-table-view-cell"><a href="" class="mui-navigate-right">Item 20</a>  
                    </li>  
                </ul>  
            </div>  
        </div>  
        <script src="../js/mui.min.js"></script>  
        <script>  
            mui.init({  
                swipeBack: false,  
                pullRefresh: {  
                    container: '#pullrefresh',  
                    down: {  
                        callback: pulldownRefresh  
                    },  
                    up: {  
                        contentrefresh: '正在加载...',  
                        callback: pullupRefresh  
                    }  
                }  
            });  
            /**  
             * 下拉刷新具体业务实现  
             */  
            function pulldownRefresh() {  
                setTimeout(function() {  
                    var table = document.body.querySelector('.mui-table-view');  
                    var cells = document.body.querySelectorAll('.mui-table-view-cell');  
                    for (var i = cells.length, len = i + 3; i < len; i++) {  
                        var li = document.createElement('li');  
                        li.className = 'mui-table-view-cell';  
                        li.innerHTML = '<a class="mui-navigate-right">Item ' + (i + 1) + '</a>';  
                        //下拉刷新,新纪录插到最前面;  
                        table.insertBefore(li, table.firstChild);  
                    }  
                    mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //refresh completed  
                }, 1000);  
            }  
            var count = 0;  
            /**  
             * 上拉加载具体业务实现  
             */  
            function pullupRefresh() {  
                setTimeout(function() {  
                    mui('#pullrefresh').pullRefresh().endPullupToRefresh((++count > 2)); //参数为true代表没有更多数据了。  
                    var table = document.body.querySelector('.mui-table-view');  
                    var cells = document.body.querySelectorAll('.mui-table-view-cell');  
                    for (var i = cells.length, len = i + 20; i < len; i++) {  
                        var li = document.createElement('li');  
                        li.className = 'mui-table-view-cell';  
                        li.innerHTML = '<a class="mui-navigate-right">Item ' + (i + 1) + '</a>';  
                        table.appendChild(li);  
                    }  
                }, 1000);  
            }  
        </script>  
    </body>  
</html>

我觉得直接按这样写应该没有问题吧

  • fairy (作者)

    谢谢啊,对我很有帮助。

    2016-09-03 09:04

该问题目前已经被锁定, 无法添加新回复