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