mustache.js實(shí)現(xiàn)首頁元件動態(tài)渲染的示例代碼
前言
在項(xiàng)目開發(fā)過程中,特別是OA類軟件,會針對郵件/待辦/公告等模塊在主頁面進(jìn)行快捷查看的元件展示要求,類似效果如下
針對框架層面,我們可以進(jìn)行后臺的可視化配置,使用mustache.js在主頁面進(jìn)行動態(tài)渲染,避免了對主頁面的繁瑣的硬編碼工作,同時(shí)針對每個(gè)信息展示的元件進(jìn)行內(nèi)部個(gè)性化處理
表結(jié)構(gòu)
包含了元件名稱,元件模板路徑,元件列表數(shù)據(jù)路由,查看更多路由,啟用/禁用等
可視化配置
模板定義
這里的模板直接使用的html文件,方便css與js的修改,簡單的使用了mustache.js進(jìn)行模板數(shù)據(jù)綁定,當(dāng)然也可以使用其他模板引擎
<div class="cellheadcontainer">
<span class="celltitletext">{{elementtitle}}</span>
<div class="celltitleop">
<a style="color:white" class="morebtn" onclick="OpenMore('{{elementmoreurl}}')">更多</a>
</div>
</div>
<div class="cellcontentcontainer">
{{#data}}
<div class="notciecell" onclick="OpenDetail('{{title}}')">
<div class="noticeleft">
<span class="noticetitle noticeindex">{{index}}</span>
<span class="noticetitle">{{title}}</span>
</div>
<div class="noticeright">
<span class="noticetip">{{publishuname}}</span>
<span class="noticetip">{{publishdate}}</span>
</div>
</div>
{{/data}}
</div>
<script>
var noticeid = "{{elementid}}";
function OpenMore(url) {
OpenTopDialog( '消息通知列表', url, 600, 800, BindNotcieList)
}
function BindNotcieList() {
CommonRefresh(noticeid);
}
function OpenDetail(title) {
OpenTopDialog('消息詳情', "frame/demo/notice/noticeDetail", 600, 800, BindNotcieList, title)
}
</script>
主頁面模板渲染
主要是針對當(dāng)前配置的模板進(jìn)行分組,渲染每行每列的元件,模板文字內(nèi)容在后端處理獲取完成,前端調(diào)用Mustache.render方法進(jìn)行數(shù)據(jù)的填充,同時(shí)需要注意針對每個(gè)元件定義局部刷新的方法,避免操作完畢后針對主頁面整體刷新
<body>
<div id="app">
<div id="maincontainer">
<div class="rowcontainer">
<div class="columncontainer">
</div>
</div>
</div>
</div>
<script>
loadcss(getRootPath() + "theme/" + GetSystemTheme() + "/main.css", true)
</script>
<script>
var pageData = {
ElementList: [],
groupCount: 0,
rowElementCount: 2
};
$(function () {
BindElement();
})
function BindElement() {
var param = {};
CloudPost(param, GetRootPath() + "frame/extend/element/findMainPageElementList", function (res) {
if (res.code == 0) {
pageData.ElementList = res.data;
var rowelementcount = pageData.rowElementCount
var groupcount = pageData.ElementList.length % rowelementcount == 0 ? pageData
.ElementList
.length / rowelementcount : (parseInt(pageData.ElementList.length /
rowelementcount)) +
1;
pageData.groupCount = groupcount;
var MainHtml = "";
for (var i = 0; i < groupcount; i++) {
var RowHtml = " <div class='rowcontainer'>";
for (var j = 0; j < rowelementcount && i * groupcount + j < pageData.ElementList.length; j++) {
var id = "row_" + (i + 1) + "column_" + (j + 1);
var ColumnHtml = "<div class='columncontainer' id='" + id + "'>"
ColumnHtml += "</div>"
RowHtml += ColumnHtml;
}
RowHtml += "</div>";
MainHtml += RowHtml;
}
$("#maincontainer").html(MainHtml);
for (var i = 0; i < groupcount; i++) {
for (var j = 0; j < rowelementcount && i * groupcount + j < pageData.ElementList.length; j++) {
var index = i * rowelementcount + j
var element = pageData.ElementList[index];
var id = "row_" + (i + 1) + "column_" + (j + 1);
pageData.ElementList[index]["ElementID"] = id;
var renderdata = {
elementtitle: element.ElementName,
elementmoreurl: element.ElementMoreUrl,
elementid: id
}
//刷新事件存儲
pageData.ElementList[index]["RefreshEvent"] = function () {
//請求數(shù)據(jù)
CloudPost({}, GetRootPath() + element.ElementDataUrl,
function (res) {
if (res.code == 0) {
//合并json
for (var attr in res) {
renderdata[attr] = res[attr];
}
var htmlcontent = Mustache.render(element.TemplatContent, renderdata);
$("#" + id).html(htmlcontent);
}
})
}
//請求數(shù)據(jù)
CommonRefresh(id);
}
}
} else {
OpenFail(res.data)
}
})
}
function CommonRefresh(id) {
$(pageData.ElementList).each(function () {
if (this.ElementID == id) {
this.RefreshEvent();
}
})
}
</script>
</body>
到此這篇關(guān)于mustache.js實(shí)現(xiàn)首頁元件動態(tài)渲染的文章就介紹到這了,更多相關(guān)mustache.js首頁元件動態(tài)渲染內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript function(函數(shù)類型)使用與注意事項(xiàng)小結(jié)
這篇文章主要介紹了javascript function(函數(shù)類型)使用與注意事項(xiàng),結(jié)合實(shí)例形式較為詳細(xì)的分析了Function(函數(shù))類型的基本聲明、屬性、方法相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-06-06
js 與 php 通過json數(shù)據(jù)進(jìn)行通訊示例
這篇文章主要介紹了js與php通過json數(shù)據(jù)進(jìn)行通訊的具體實(shí)現(xiàn),需要的朋友可以參考下2014-03-03
微信小程序頁面與組件之間信息傳遞與函數(shù)調(diào)用
不管是vue還是react中,都在強(qiáng)調(diào)組件思想,所以下面這篇文章主要給大家介紹了關(guān)于微信小程序頁面與組件之間信息傳遞與函數(shù)調(diào)用的相關(guān)資料,需要的朋友可以參考下2021-05-05
CKEditor擴(kuò)展插件:自動排版功能autoformat插件實(shí)現(xiàn)方法詳解
這篇文章主要介紹了CKEditor擴(kuò)展插件:自動排版功能autoformat插件實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了CKEditor擴(kuò)展插件實(shí)現(xiàn)自動排版功能的autoformat插件具體定義、配置與使用技巧,需要的朋友可以參考下2020-02-02

