js使用模板快速填入資料

來源:互聯網
上載者:User

標籤:eth   doctype   atime   end   --   att   dia   []   oct   

1.html

<!DOCTYPE html><html><head>  <title>模板標籤</title></head><body>  <table id="tableData">    <tr class="firstLine">      <th></th>      <th>圖片</th>      <th>圖片名稱</th>      <th>類型</th>      <th>大小</th>      <th>尺寸</th>      <th>上傳日期</th>      <th>操作</th>      <th></th>    </tr>  </table></body>

 2.js臨時模板

<script type="text/template">  <tr mgid="{mgid}" mid="{mid}">    <td>      <input type="checkbox" mid="{mid}">    </td>    <td>      <a href="{localfile}" data-fancybox-group="button" class="fancybox-buttons">        <img src="{localfile}" style="width:45px;height:45px;">      </a>    </td>    <td>      <input type="text" class="input-large valid" value="{medianame}" name="medianame" mid="{mid}" readonly="readonly">    </td>    <td>{mediatype}</td>    <!-- 各位看官,自訂的三個屬性在這裡哦~~ -->    <td>{fsize}</td>    <td>{asize}</td>    <td>{atime}</td>    <td>      <a href="javascript:void(0);">重新命名</a>      <a name="edit" localfile="{localfile}" href="javascript:void(0);">替換</a>      <a href="javascript:void(0);">刪除</a>      <a title="設定為分組【{groupname}】的封面" groupname="{groupname}" mid="{mid}" href="javascript:void(0);">設定封面</a>    </td>  </tr></script>

 3.核心代碼

<script>/*將模板寫入到html*/$.ajax({  url: ‘/html/datas‘,  type: ‘get‘,  cache: false,  dataType: "json",  success: function(dta) {    if (!dta || !dta.rows || dta.rows.length <= 0) {      return;    }    //擷取模板上的HTML      var html = $(‘script[type="text/template"]‘).html();    var arr = [];    //對資料進行遍曆      $.each(dta.rows, function(i, o) {      //自訂欄位      o.atime = getFormatDate(o.uploadtime ? o.uploadtime : o.createtime, ‘yyyy-MM-dd‘);      o.asize = (o.width && o.height) ? o.width + ‘ * ‘ + o.height : ‘-‘;      o.fsize = o.seizespace ? o.seizespace + ‘  KB‘ : ‘-‘;      arr.push(formatTemplate(o, html));    });    $(‘#tableData‘).append(arr.join(‘‘));  }});</script><script>/*日期格式*/function getFormatDate(xdate, format) {  try {    var format = format || ‘yyyy-MM-dd HH:mm:ss‘;    var date = (xdate instanceof Date) ? xdate : new Date(parseInt(xdate.replace(‘/Date(‘, ‘‘).replace(‘)/‘, ‘‘), 10));    var lang = {      ‘M+‘: date.getMonth() + 1,      ‘d+‘: date.getDate(),      ‘H+‘: date.getHours(),      ‘m+‘: date.getMinutes(),      ‘s+‘: date.getSeconds()    };    if (/(y+)/.test(format)) {      format = format.replace(RegExp.$1, (date.getFullYear() + ‘‘).substr(4 - RegExp.$1.length));    }    for (var key in lang) {      if (new RegExp(‘(‘ + key + ‘)‘).test(format)) {        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?          lang[key] : (‘00‘ + lang[key]).substr((‘‘ + lang[key]).length));      }    }    return format;  } catch (e) {    return ‘-‘;  }}</script>

  

js使用模板快速填入資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.