From 6efaa2ba16a302cba69622587dbab95f19795279 Mon Sep 17 00:00:00 2001
From: hdw <hdw@192.168.7.127>
Date: 星期三, 16 一月 2019 16:39:21 +0800
Subject: [PATCH] 手机端实时测试数据显示更新

---
 gx_tieta/WebRoot/mobil/js/tbl.js |  175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 174 insertions(+), 1 deletions(-)

diff --git a/gx_tieta/WebRoot/mobil/js/tbl.js b/gx_tieta/WebRoot/mobil/js/tbl.js
index 26d3922..fcd1b16 100644
--- a/gx_tieta/WebRoot/mobil/js/tbl.js
+++ b/gx_tieta/WebRoot/mobil/js/tbl.js
@@ -230,4 +230,177 @@
 		$('.tbl-container table tbody tr').removeClass('active');
 		$(this).addClass('active');
 	});
-});
\ No newline at end of file
+});
+
+//瀵筸obile绔暟鎹〃鏍艰繘琛屾搷浣�
+var MobileTbl = function(tbl, arrTh) {
+    this.tbl = tbl;
+    this.arrTh = arrTh;
+};
+
+// 瀹氫箟瀵硅薄鐨勫師鍨嬪嚱鏁�
+MobileTbl.prototype = {
+    setArrTh:function(arrTh) {      // 璁剧疆琛ㄦ牸鐨勫ご閮ㄦ暟鎹�
+        this.arrTh = arrTh;
+    }
+    ,updateCell: function(rows, cols, value) {          // 鏇存柊鎸囧畾鐨勫崟鍏冩牸
+        var tbody = this.tbl.find('tbody');             // 鑾峰彇tbody
+        var tr = tbody.find('tr').eq(rows);             // 鑾峰彇tr
+        var td = tr.find('td').eq(cols);                // 鑾峰彇td
+        var th = this.arrTh[cols];                      // 鍒楀ご閮ㄦ暟鎹俊鎭�
+        
+        // 鐢熸垚html鏍囪瀛楃涓�
+        var tdHtml = '<b class="ui-table-cell-label">'+th+'</b>';
+        tdHtml += value;
+
+        // 娣诲姞鍐呭鍒皌d
+        td.html(tdHtml);
+    }
+};
+
+//瀵逛簬鎵嬫満绔〃鏍艰繘琛岀殑浜屾寮�鍙�
+var MobileTable = function(opts) {
+    this.opts = {};     // 鐢熸垚琛ㄦ牸鐨勯厤缃」
+    this.el = '';
+    this.cols = [];     // 琛ㄦ牸鐨勫ご閮�
+    this.data = [];     // 琛ㄦ牸鐨勫唴瀹�
+    this.render(opts);
+}
+// 瀹氫箟瀵硅薄鐨勫師鍨嬪嚱鏁�
+MobileTable.prototype = {
+    _setEl: function(el) {          // 璁剧疆琛ㄦ牸鎵�鍦ㄧ殑瀹瑰櫒
+        if(typeof el == 'object') {
+            this.el = el;
+        }else {
+            this.el = $(el);
+        }
+    }
+    ,_setOpts: function(opts) {      // 璁剧疆閰嶇疆椤�
+        this.opts = opts;
+    }
+    ,_setCols: function(cols) {     //  璁剧疆琛ㄦ牸鐨勫ご閮�
+        this.cols = cols;
+    }
+    ,_setData: function(data) {
+        this.data = data;
+    }
+    ,_init: function(opts) {
+        let cols = opts.cols;
+        let data = opts.data;
+        let el = opts.el;
+        this._setOpts(opts);        // 鍒濆鍖栭厤缃」
+        this._setEl(el);            // 鍒濆鍖杄l
+        this._setCols(cols);        // 鍒濆鍖栧ご閮ㄦ暟鎹�
+        this._setData(data);        // 鍒濆鍖栬〃鏍兼暟鎹�
+    }
+    ,render: function(opts) {
+        this._init(opts);
+        var table = $('<table data-role="table" class="ui-responsive"></table>');
+        var thead = this._getThead();
+        var tbody = this._getTbody();
+        
+        // 鐢熸垚琛ㄦ牸
+        table.append(thead);
+        table.append(tbody);
+        // 娓呯┖瀹瑰櫒
+        this.el.text('');
+        this.el.append(table).trigger('create');
+    }
+    ,_getThead: function() {    // 鑾峰彇琛ㄦ牸鐨勫ご閮�
+        var cols = this.cols;
+        var thead = $('<thead></thead>');
+        var tr = $('<tr></tr>');
+        // 閬嶅巻cols鐨勫��,鏍规嵁鏁版嵁椤箃head娣诲姞鍐呭
+        for(let i=0; i<cols.length; i++) {
+            let _cols = cols[i];
+            var th = $('<th></th>');
+            th.html(_cols.title);
+            th.attr('data-field', _cols.field);
+            tr.append(th)
+            
+        }
+        thead.append(tr);
+        return thead;
+    }
+    ,_getTbody: function() {    // 鑾峰彇琛ㄦ牸鐨勫唴瀹�
+        var cols = this.cols;
+        var data = this.data;
+        var tbody = $('<tbody></tbody>');
+
+        // 閬嶅巻data鐨勫��
+        for(let i=0; i<data.length; i++) {
+            let _data = data[i];
+            let tr = $('<tr></tr>');
+            // 閬嶅巻cols鐨勫��
+            for(let k=0; k<cols.length; k++) {
+                let _cols = cols[k];
+                var field = _cols.field;
+                var title = _cols.title;
+                var td = $('<td></td>');
+                if(field in _data) {
+                    td = $('<td>'+_data[field]+'</td>');
+                }
+                // 璁剧疆鍊�
+                td.attr('data-field', field);
+                td.attr('data-title', title);
+                tr.append(td);
+            }
+            tr.append($('<td data-field="visibility" style="visibility: hidden"></td>'));
+            // 鐢熸垚tr
+            tbody.append(tr);
+        }
+
+        // 杩斿洖缁撴灉
+        return tbody;
+    }
+    ,update: function(field, fVal, data) {
+        let rowsInfo = this._getRows(field, fVal);
+        // 濡傛灉鑾峰彇鍒颁簡琛屽彿
+        if(rowsInfo.code==1) {
+            let rows = rowsInfo.data;       // 鏁版嵁鎵�鍦ㄧ殑琛屽彿
+            this.updateTr(rows, data);
+        }else {
+            console.log(rowsInfo.msg);
+        }
+    }
+    ,updateTr: function(rows, data) {
+        let tbody = this.el.find('tbody');
+        let tr = tbody.find('tr').eq(rows);
+        let td = tr.find('td');
+        // 閬嶅巻td鐨勫唴瀹�
+        td.each(function() {
+            let field = $(this).data('field');
+            let title = $(this).data('title');
+            // 鍒ゆ柇褰撳墠灞炴�ф槸鍚﹀湪data涓�
+            if(field in data) {
+                // 鐢熸垚html鏍囪瀛楃涓�
+                var tdHtml = '<b class="ui-table-cell-label">'+title+'</b>';
+                tdHtml += data[field];
+                $(this).html(tdHtml);
+            }
+        });
+
+    }
+    ,_getRows: function(field, fVal) {     // 鏍规嵁鏍囪瘑浣嶇殑鍊艰幏鍙栧綋鍓嶆暟鎹墍鍦ㄧ殑琛�
+        var rs = {
+            code: 0
+            ,data: -1
+            ,msg: '鏈幏鍙栧埌鏍囪瘑浣�'+field+':'+fVal+'鎵�鍦ㄧ殑琛�'
+        };
+        var data = this.data;
+        // 閬嶅巻data鐨勫��
+        for(let i=0; i<data.length; i++) {
+            let _data = data[i];
+            if(field in _data) {
+                let _fVal = _data[field];       // 鑾峰彇褰撳墠鏁版嵁鐨勬爣璇嗕綅鐨勫��
+                if(_fVal == fVal) {
+                    rs.code = 1;        // 鏍囪瘑鑾峰彇鍒颁簡涓嬫爣
+                    rs.data = i;        // 涓嬫爣鐨勫��
+                }
+            }
+        }
+
+        // 杩斿洖缁撴灉
+        return rs;
+    }
+};
\ No newline at end of file

--
Gitblit v1.9.1