JS quickly populate data with templates

Source: Internet
Author: User

1.html

<! DOCTYPE html>

2.js Temp Template

<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-butto NS ">  </a> </td> <td> <input type= "text" class= "input-large valid" value= "{medianame}" Name= "MediaName" mid= "{mid}" readonly= "readonly" > </td> <td>{mediatype}</td> <!--everyone crossing, since Three properties defined here Oh ~--<td>{fsize}</td> <td>{asize}</td> <td>{atime}</td> &L t;td> <a href= "javascript:void (0);" > Rename </a> <a name= "edit" localfile= "{localfile}" href= "javascript:void (0);" > Replace </a> <a href= "javascript:void (0);" > Delete </a> <a title= "set as cover for group {groupname}" Groupname= "{groupname}" mid= "{mid}" href= "javascript:void (0);" > Set SealNoodles </a> </td> </tr></script> 

3. Core code

<script>/* writes the template to Html*/$.ajax ({url: '/html/datas ', type: ' Get ', Cache:false, DataType: ' JSON ', success:functi    On (DTA) {if (!dta | |!dta.rows | | dta.rows.length <= 0) {return;    }//Gets the HTML var html = $ (' script[type= "Text/template") on the template. html ();    var arr = []; Traverse the data $.each (dta.rows, function (i, O) {//custom field 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>/* date format */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 (') ', ') ' (') ' (')/', '), 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]: (' xx ' + Lang[key]). substr ((' + lang[key]). length);  }} return format;  } catch (e) {return '-'; }}</script>

  

JS quickly populate data with templates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.