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

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

Qt TableWidget 固定表头 实例(qtablewidget自定义表头)  第1张

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

使用说明:

  1. vartableWidget=newTableWidget("TableID","DestID","100%","300px");
  2. tableWidget.change();

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

  1. /*
  2. *函数名称:Widget
  3. *作者:yithcn
  4. *功能说明:固定表格头,表体可以滚动
  5. *创建日期:2010.10.13
  6. */
  7. functionTableWidget(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. varthat=this;
  19. vardiv=document.createElement("div");
  20. div.style.cssText="background-color:white;width:"+that.width;
  21. that.dest.appendChild(div);
  22. //title
  23. vartitlediv=document.createElement("div");
  24. titlediv.style.cssText="width:100%;";
  25. div.appendChild(titlediv);
  26. //body
  27. varbodydiv=document.createElement("div");
  28. bodydiv.style.cssText="overflow:auto;height:"+that.height+";";
  29. bodydiv.appendChild(that.table);
  30. div.appendChild(bodydiv);
  31. varnewtable=that.table.cloneNode(true);
  32. varlen=newtable.rows.length;
  33. for(vari=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. varthat=this;
  43. window.onload=function(){
  44. if(table&&typeoftable=="string")
  45. table=document.getElementById(table);
  46. if(dest&&typeofdest=="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. varthat=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 固定表头 实例的内容介绍完了, 希望本文对你有所帮助!

转载请说明出处
知优网 » Qt TableWidget 固定表头 实例(qtablewidget自定义表头)

发表评论

您需要后才能发表评论