HTML5+中動態構建列表並填充資料,html5填充

來源:互聯網
上載者:User

HTML5+中動態構建列表並填充資料,html5填充

        部分代碼參考demo----《曆史上的今天》。

        感謝作者的分享,願好人一生平安,雖然只有兩個頁面,但是通過這個樣本讓我學會了5+中如何動態構建列表並填充資料,非常實用。


html部分:

<body>        <header class="mui-bar mui-bar-nav">            <a id="info" class="mui-icon mui-icon-info-filled mui-pull-right" style="color: #999;"></a>            <h1 class="mui-title">曆史上的今天</h1>        </header>        <div class="mui-content">            <ul class="mui-table-view">                <li class="mui-table-view-divider">                    <span class="fs14">今天:</span><span class="mui-badge mui-badge-blue" id="curdatestr"></span>                    <span style="margin-left: 50px;"></span>                    <span class="fs14">星期:</span><span class="mui-badge mui-badge-red" id="curdateweekstr"></span>                </li>                <ul class="mui-table-view" id="TIH_EventList_UL">                                    </ul>            </ul>        </div><pre name="code" class="html"></body>



js指令碼:

<script type="text/javascript">window.onload = function() {mui.init();mui.plusReady(function() {// for Android back eventmui.options.maintabid = mui.currentWebview.__uuid__;// fill headvar curDate = new Date();fillHeader(curDate);// righticon add eventmui.addEventListenerBySelector("#info", "tap", function() {plus.device.vibrate();});// ajax request datagetListData(curDate);});}function fillHeader(cdate) {mui.html('curdatestr', cdate.toString("yyyy年MM月dd日"));mui.html('curdateweekstr', cdate.week());}function getListData(cdate) {if (!mui.constMap.key_historyOnToday) {mui.toast("請在app.js中配置KEY!!! ");return;}plus.nativeUI.showWaiting("資料載入中,請稍候...");console.log(plus.storage.getLength());var existsCache = false;for (var i = 0; i < plus.storage.getLength(); i++) {var stVal = plus.storage.key(i);console.log(stVal.substring(5).length + "========" + cdate.toString().length);if (stVal.substring(5) == cdate.toString()) {// exists cache date in curdateexistsCache = true;break;}}if (!existsCache) {// clear all cacheplus.storage.clear();}var stKey = "main:" + cdate;var cdata = plus.storage.getItem(stKey);if (cdata) {console.log("read cache");// fill cached datacdata = mui.readJson(cdata);notifiULList(cdata);} else {console.log("read real data");var url = 'http://japi.juhe.cn/toh/toh?key=' + mui.constMap.key_historyOnToday + 'dtype=json&v=1.0&month=' + cdate.month() + '&day=' + cdate.day();mui.sendRequest(url, {}, function(result) {if (result.success) {var n = result.retval.result;notifiULList(n);plus.storage.setItem(stKey, mui.writeJson(n));} else {mui.toast(result.error);}}, 'json');}};function notifiULList(cdataList) {plus.nativeUI.closeWaiting();var doc = document,fragment = doc.createDocumentFragment(),container = doc.getElementById("TIH_EventList_UL");mui.each(cdataList, function(i, n) {console.info(n.id)notifiAndSetLiDetail(fragment, n);});container.appendChild(fragment);mui.addEventListenerBySelector("a.aEvent", "tap", showDetail);}function notifiAndSetLiDetail(fragment, n) {var doc = document,li = doc.createElement("li"),a = doc.createElement("a"),img = doc.createElement("img"),div = doc.createElement("div"),p = doc.createElement("p");li.setAttribute("class", "mui-table-view-cell mui-media");console.info(n._id)//明明是_id 介面上說id   坑我   a.setAttribute("eid", n._id);//a.addEventListener("click", showDetail(n.id)); //note:showDetail() has been called every times. i dont know why.a.setAttribute("class", "aEvent");img.setAttribute("class", "mui-media-object mui-pull-left");img.setAttribute("style", "width: 42px; height: 42px;");img.setAttribute("src", n.pic.isBlank() ? 'images/logo.png' : n.pic);div.setAttribute("class", "mui-media-body");div.innerText = n.title;p.innerHTML = n.year + "年" + n.month + "月" + n.day + "日" + ", " + n.lunar;li.appendChild(a);a.appendChild(img);a.appendChild(div);div.appendChild(p);fragment.appendChild(li);}function showDetail(o) {var a = o.target.offsetParent;if (a.tagName != 'A') {// fix the end of line onclick event, it's return li not a, i dont know why.return;}mui.openWindow({id: "detail.html",url: "detail.html",show: {autoShow: false},extras: {eid: a.getAttribute("eid")}});}</script>


        根據原作者的提示,我找到了彙總資料這個網站,註冊後擷取了免費試用介面100次的機會,為了讓這個app跑起來,真機測試很多次,後來在彙總網站上測試才發現返回的不是id,而是_id,話說我的心碎了,太不專業了吧,介面文檔明擺著,你卻返回不一致的東西,這不坑人嗎。


看來以後還要注意,發現錯誤, 要大膽質疑,管你是官方文檔還是大神的佳作,我只相信事實。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.