Qt TableWidget 固定表头 实例

移动开发
本文介绍的是Qt TableWidget 固定表头 实例,很多时候我们都在用列表,不多说,先哎看本文内容。

Qt TableWidget 固定表头 实例是本文要介绍的内容,使TableWidget  固定表头一个js插件的实例,先来看内容。

公司项目里面很多地方都需要用到,出列表的时候固定表头,滚动表体,思路就是动态创建一个div,然后里面创建2个div,一个title,一个body,然后用clone的方法,分别处理2个div的内容

使用说明:

  1. var tableWidget = new TableWidget("TableID", "DestID", "100%", "300px");  
  2. tableWidget.change(); 

表格需要固定宽度,table 需要加 style="table-layout: fixed;"

  1. /*  
  2. * 函数名称: Widget  
  3. * 作    者: yithcn  
  4. * 功能说明: 固定表格头,表体可以滚动  
  5. * 创建日期: 2010.10.13  
  6. */  
  7. function TableWidget(table, dest, width, height) {  
  8.     this.construct(table, dest, width, height);  
  9. };  
  10. TableWidget.prototype = {  
  11.     table: null,  
  12.     dest: null,  
  13.     widht: null,  
  14.     height: null,  
  15.     tdiv: null,  
  16.     bdiv: null,  
  17.     create: function() {  
  18.         var that = this;  
  19.         var div = document.createElement("div");  
  20.         div.style.cssText = "background-color:white;width:" + that.width;  
  21.         that.dest.appendChild(div);  
  22.         //title  
  23.         var titlediv = document.createElement("div");  
  24.         titlediv.style.cssText = "width:100%;";  
  25.         div.appendChild(titlediv);  
  26.         //body  
  27.         var bodydiv = document.createElement("div");  
  28.         bodydiv.style.cssText = "overflow:auto;height:" + that.height + ";";  
  29.         bodydiv.appendChild(that.table);  
  30.         div.appendChild(bodydiv);  
  31.         var newtable = that.table.cloneNode(true);  
  32.         var len = newtable.rows.length;  
  33.         for (var i = len - 1; i > 0; i--) {  
  34.             newtable.deleteRow(i);  
  35.         }  
  36.         titlediv.appendChild(newtable);  
  37.         that.table.deleteRow(0);  
  38.         that.tdiv = titlediv;  
  39.         that.bdiv = bodydiv;  
  40.     },  
  41.     construct: function(table, dest, width, height) {  
  42.         var that = this;  
  43.         window.onload = function() {  
  44.             if (table && typeof table == "string")  
  45.                 table = document.getElementById(table);  
  46.             if (dest && typeof dest == "string")  
  47.                 dest = document.getElementById(dest);  
  48.             else  
  49.                 dest = document.body;  
  50.             widthwidth = width || "100%";  
  51.             heightheight = height || "300px";  
  52.             height = parseInt(height) - table.rows[0].offsetHeight;  
  53.             that.table = table;  
  54.             that.dest = dest;  
  55.             that.width = width;  
  56.             that.height = height;  
  57.             that.create();  
  58.             that.change();  
  59.         }  
  60.     },  
  61.     change: function() {  
  62.         var that = this;  
  63.         if (that.table.offsetHeight > parseInt(that.height)) {  
  64.             that.tdiv.style.width = parseInt(that.bdiv.offsetWidth) - 16;  
  65.         }  
  66.         else {  
  67.             that.tdiv.style.width = parseInt(that.bdiv.offsetWidth);  
  68.         }  
  69.     }  
  70. }; 

之所以会有一个change方法,是因为在项目当中需要动态改变列表,要计算表头和表体滚动条。

小结:Qt TableWidget 固定表头 实例的内容介绍完了, 希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-06-30 14:34:17

QT Tablewidge QTableWidg

2011-06-30 16:53:18

QT Creator TableWidge

2009-06-23 11:23:13

DataTableJSF动态生成

2011-07-05 14:46:34

2011-06-14 16:45:57

Qt 图标

2011-06-24 14:34:17

Qt 小票 打印

2011-06-13 16:51:19

Qt Socket

2011-06-27 16:07:49

Qt Designer

2011-06-21 09:33:49

Qt 启动 界面

2011-06-30 18:15:36

Qt 线程 同步

2011-07-05 15:16:00

QT 进度条

2011-06-16 17:54:30

Qt Mplayer

2011-06-30 11:07:02

Qt QTextEdit

2014-08-26 11:46:46

QtAndroid实例教程

2011-06-27 16:37:08

Qt Designer

2011-06-21 15:11:04

QT 数据库

2011-06-30 16:38:07

Qt QTableWidg

2011-06-14 10:52:10

QT QTreeView

2011-06-30 17:31:32

Qt 多线程 信号

2011-06-24 16:09:24

Qt 动画 状态机
点赞
收藏

51CTO技术栈公众号