欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Jquery 模板数据绑定插件的使用方法详解

程序员文章站 2022-10-22 23:44:42
1 绑定后台数据到指定模板(无嵌套,内容均为后台数据原始信息,前台绑定) 以通话记录页为例: 首先指定目标容器: . 代码如下:  

1 绑定后台数据到指定模板(无嵌套,内容均为后台数据原始信息,前台绑定)
以通话记录页为例:
首先指定目标容器:

. 代码如下:


 <dl class="box_pannel_content_filled_border none" id="gvreccalls">
   <dd class="bg_blue">
    <p class="width_level_half_2 fl nopitch"></p>
    <p class="width_level_half_5 bold fl">被叫号码</p>
    <p class="width_level_half_5 bold fl">主叫号码</p>
    <p class="width_level_half_6 bold fl">起始时间</p>
    <p class="width_level_half_4 bold fl" id="pamount">金额($)</p>
    <p class="width_level_half_4 bold fl">时长</p>
    <p class="width_level_half_10 bold fl">区域</p>
    <p class="width_level_half_4 bold fl nopitch" name="pcdrheader">cdr id</p>
    <p class="width_level_half_2 fl none" name="pselectcbheader"></p>
    <p class="width_level_half_2 fr none" name="pdeletecbheader"></p>
   </dd>
</dl>


其次指定模板数据:

. 代码如下:


<dl id="reccallstemplate" >
<dd >
<p class="center width_level_half_2 fl nopitch">
<b class="${callmode == 1 ? 'icon_call_in' : 'icon_call_out'}" ></b>
</p>
<span class="width_level_half_5 fl"><span style="color:#ff0000;">${calledstationid}</span></span>
<span class="width_level_half_5 fl">${callingstationid.substr(callingstationid.indexof("*") + 1)}</span>
<span class="width_level_half_6 fl"  id="start_time" >${starttime}</span>
<span class="width_level_half_4 fl">${csscommonjs.changedecimal(revenue,3)}</span>
<span class="width_level_half_4 fl"><span style="color:#ff0000;">${csscommonjs.gettimeformatstring(revenuetime)}</span></span>
<span class="width_level_half_10 fl">${location} </span>
<span class="width_level_half_4 fl nopitch" name="pcdrheader" >${cdrid}</span>
<p class="right width_level_half_2 fr none" name="pselectcbheader">
<input  type="checkbox" name="cbselect" class="label" /></p>
<span class="fl none" name="pdeletecbheader">
<button class="crm_btn norm" id="btdelete"><b>删除</b></button>
</span>
</dd>

</dl>


后台进行绑定:
 <script src="scripts/jquery.tmpl.js" type="text/javascript"></script>
function rendertemplatefunction(container, template, data) {
    $(template).tmpl(data).appendto(container); //原始方法
};
 csscommonjs.rendertemplatefunction($(t.panelid).find("#gvreccalls"), $(t.panelid).find("#reccallstemplate"), result.cdrdata);
2 指定模板中无通配符,后台填充数据(许愿墙实现)
前台:

. 代码如下:


 <p id="content">
                    <!-- 模板数据-->
                    <p id="itemlist">
                    </p>
                    <!-- 模板数据end-->
                </p>
 <p id="itemtemplate" style="display: none;">
        <dd>
            <p class="items">
                <p class="bg">
                    <p class="info">
                        <a href="#" id="btntitle"><span id="item_title"></span></a>
                    </p>
                    <p class="k">
                    </p>
                    <p class="person" id="item_person">
                    </p>
                    <p class="date" id="item_date">
                    </p>
                </p>
            </p>
        </dd>
    </p>


后台进行取数据绑定,绑事件等。

. 代码如下:


    //获取许愿墙数据
    $.get("control/controler.ashx?t=" + new date(), { type: 'heartwall', date: new date() }, function (data) {
        var jsondata = eval("(" + data + ")");
        //alert(jsondata.table[1].title);
        rendertemplatefunction($("#itemlist"), $("#itemtemplate"), jsondata.table);
        $("#itemlist").children("dd").each(function (index) {
            var ttr = this;
            var selecteditem = $.tmplitem(this);
            var tmp_title = $(ttr).find("#item_title");
            var tmp_person = $(ttr).find("#item_person");
            var tmp_date = $(ttr).find("#item_date");
            var btntitle = $(ttr).find("#btntitle");
            var bgnumber = "it" + math.floor(math.random() * 10 + 9) + ".jpg"; //1-10的随机数
            var bg = $(ttr).find(".bg");
            bg.css('background-image', "url('img/bg/" + bgnumber + "')");
            var getrandomcolor = function () {
                return (function (m, s, c) {
                    return (c ? arguments.callee(m, s, c - 1) : '#') +
                        s[m.floor(m.random() * 16)]
                })(math, '0123456789abcdef', 5)
            }
            var color = getrandomcolor();
            $(ttr).find("#item_title").css('color', color.tostring());
            //绑定数据
            tmp_title.html(selecteditem.data.title);
            tmp_person.html(selecteditem.data.pubname);
            tmp_date.html(selecteditem.data.adddate.tostring().split(' ')[0].replaceall('/', '-').tostring());
            btntitle.click(function () {
                var heart_date = "";
                if (selecteditem.data.begindate.tostring() == selecteditem.data.enddate.tostring()) {
                    heart_date = selecteditem.data.begindate.tostring().split(' ')[0].replaceall('/', '-');
                }
                else {
                    heart_date = selecteditem.data.begindate.tostring().split(' ')[0].replaceall('/', '-') + " 至 " +
selecteditem.data.enddate.tostring().split(' ')[0].replaceall('/', '-');
                }
                $("#heart_title").html(selecteditem.data.title);
                $("#heart_content").html(selecteditem.data.content);
                $("#heart_date").html(heart_date);
                $("#heart_person").html(selecteditem.data.participator);
                $("#heart_contact").html(selecteditem.data.contact);
                $("#heatr_puber").html(selecteditem.data.pubname);
                //showbox
                this.href = "#heartinfo_content";
                $(this).addclass("heartinfo_inline");
                $("#heartinfo_content").show();
                showdialog();
            });
        });
    });


3 嵌套绑定 (目标数据源中含有多个数组,分别绑定到对应的子模板)
账单页面为例:
前台:

. 代码如下:


目标容器
<span class="width_level_0 fl nopitch" id="productbilling">

                                    </span>
外层模板
<p id="productbillingtemplate" class="none">
<dl class="box_pannel_content_filled_special">
                                         <dd class="border_none_special_top">
                                             <p class="width_level_half_3 fl"></p>
                                                <span class="width_level_9 fl"><b class="bold" id="bcomboname"></b> <b id="btel"></b></span>
                                            </dd>
                                         {{tmpl(billtransactions) "#billingdetaildatetemplate"}}
                                         <span style="color:#ff0000;">   {{tmpl(rebateinstances) "#billingdetaildatetemplate"}}</span>  固定写法,第一个参数为数据源中的第二个数组,第二个为使用的子模板
                                            {{tmpl(topupdetails) "#billingdetaildatetemplate"}}
                                        </dl>
</p>
子模板
<p id="billingdetaildatetemplate" class="none">
<dd class="border_none_special">
    <p class="width_level_half_3 fl"></p>
    <p class="width_level_half_12 fl">${(<span style="color:#ff0000;">typeof(name) == "undefined" ? type : name</span>) + ":"}</p> <span style="color:#ff0000;">
子模板是三个数据源的公共模板,可能属性的名称会有不同,需要判断</span>
    <span class="width_level_1 fl" id="spamount" title = "{{= createdate }}">${csscommonjs.changedecimal((typeof(initialamount) == "undefined" ?
 amount : initialamount), 2)}</span>
    <span class="width_level_5 fl" id="spdescription">${description}</span>
</dd>
</p>


后台绑定

. 代码如下:


csscommonjs.rendertemplatefunction($(t.panelid).find("#productbilling"), $(t.panelid).find("#productbillingtemplate"), billingdetail);
    //
    $(t.panelid).find("#productbilling").children("dl").each(function (index) {
        var ttr = this;
        var selecteditem = $.tmplitem(this);
        var bcomboname = $(ttr).find("#bcomboname");
        var btel = $(ttr).find("#btel");
        var n = selecteditem.data;
        var curacct = csscommonjs.getcurrentuser(t.masteruser, n.accountid); // n.businessaccountid);
        var curpstn = "";
        if (curacct.accounttype == cssaccounttype.bb) {
            if (curacct.did) {
                if (curacct.countrycode == "1") {
                    curpstn = "(tel:" + csscommonjs.formatuscaphone(curacct.did) + ")";
                }
                else {
                    curpstn = "(tel:" + curacct.did + ")";
                }
            }
            else if (curacct.bbnumber) {
                curpstn = "(" + curacct.bbnumber + ")";
            }
        }
        else if (curacct.accounttype == cssaccounttype.hy) {
            curpstn = "(" + curacct.hynumber + ")";
        }
        else if (curacct.accounttype == cssaccounttype.dsl) {
            curpstn = "(" + curacct.dslnumber + ")";
        }
        bcomboname.html(curacct.comboname);
        btel.html(curpstn);
        if ((n.billtransactions.length + n.rebateinstances.length + n.topupdetails.length) == 0) {
            $(ttr).hide();
        }
        $(ttr).find(".border_none_special").each(function (spindex) {
            var tdd = this;
            var selecteditem = $.tmplitem(this);
            var spamount = $(tdd).find("#spamount");
            var spdescription = $(tdd).find("#spdescription");
            if (t.currentadmin.valid) {
                spamount.attr("title", spamount.attr("title").formatdate(t.masteruser, ""));
            }
            else {
                spdescription.hide();
            }
        });
  });


嵌套绑定是模板自动完成的。