FluTableView.qml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Layouts
  4. import FluentUI
  5. Item {
  6. property var columns : []
  7. property var dataSource : []
  8. property int pageCurrent: 1
  9. property int itemCount: 1000
  10. property int pageCount: 10
  11. property int itemHeight: 56
  12. property bool pageVisible: true
  13. signal requestPage(int page,int count)
  14. id:control
  15. implicitHeight: layout_table.height
  16. QtObject{
  17. id:d
  18. property int coumnsWidth: parent.width
  19. }
  20. MouseArea{
  21. anchors.fill: parent
  22. preventStealing: true
  23. }
  24. ListModel{
  25. id:model_coumns
  26. }
  27. ListModel{
  28. id:model_data_source
  29. }
  30. onColumnsChanged: {
  31. model_coumns.clear()
  32. model_coumns.append(columns)
  33. var w = 0
  34. for(var i=0;i<model_coumns.count;i++){
  35. var item = model_coumns.get(i)
  36. w=w+item.width
  37. }
  38. d.coumnsWidth = w
  39. }
  40. onDataSourceChanged: {
  41. model_data_source.clear()
  42. model_data_source.append(dataSource)
  43. }
  44. Flickable{
  45. id:layout_flickable
  46. height: layout_table.height
  47. anchors{
  48. top: parent.top
  49. left: parent.left
  50. right: parent.right
  51. }
  52. contentWidth: layout_table.width
  53. clip:true
  54. ScrollBar.horizontal: FluScrollBar {
  55. }
  56. Rectangle{
  57. anchors.fill: layout_table
  58. radius: 5
  59. color: FluTheme.dark ? Qt.rgba(39/255,39/255,39/255,1) : Qt.rgba(251/255,251/255,253/255,1)
  60. }
  61. ListView{
  62. id:layout_table
  63. height: contentHeight
  64. width: Math.max(layout_flickable.width,d.coumnsWidth)
  65. clip:true
  66. interactive: false
  67. header: FluRectangle{
  68. id:layout_coumns
  69. height: control.itemHeight
  70. width: parent.width
  71. color:FluTheme.dark ? Qt.rgba(50/255,50/255,50/255,1) : Qt.rgba(247/255,247/255,247/255,1)
  72. radius: [5,5,0,0]
  73. Row{
  74. id:list_coumns
  75. spacing: 0
  76. anchors.fill: parent
  77. Repeater{
  78. model: model_coumns
  79. delegate: Item{
  80. height: list_coumns.height
  81. width: model.width
  82. FluText{
  83. text:model.title
  84. wrapMode: Text.WordWrap
  85. anchors{
  86. verticalCenter: parent.verticalCenter
  87. left: parent.left
  88. leftMargin: 14
  89. }
  90. font: FluTextStyle.BodyStrong
  91. }
  92. FluDivider{
  93. width: 1
  94. height: 40
  95. anchors.right: parent.right
  96. anchors.verticalCenter: parent.verticalCenter
  97. visible: index !== model_coumns.count-1
  98. }
  99. }
  100. }
  101. }
  102. }
  103. footer: Item{
  104. height: pageVisible ? 50 : 0
  105. clip: true
  106. width: layout_table.width
  107. FluPagination{
  108. id:pagination
  109. height: 40
  110. pageCurrent: control.pageCurrent
  111. onPageCurrentChanged: control.pageCurrent = pageCurrent
  112. itemCount: control.itemCount
  113. pageCount: control.pageCount
  114. onRequestPage:
  115. (page,count)=> {
  116. control.requestPage(page,count)
  117. }
  118. anchors{
  119. top: parent.top
  120. right: parent.right
  121. }
  122. Connections{
  123. target: control
  124. function onPageCurrentChanged(){
  125. if (control.pageCurrent!==pagination.pageCurrent)
  126. {
  127. pagination.calcNewPage(control.pageCurrent)
  128. }
  129. }
  130. }
  131. }
  132. }
  133. model:model_data_source
  134. delegate: Item{
  135. height: table_row.maxHeight
  136. width: layout_table.width
  137. property var model_values : getObjectValues(index)
  138. property var itemObject: getObject(index)
  139. property var listModel: model
  140. Row{
  141. id: table_row
  142. spacing: 0
  143. anchors.fill: parent
  144. property int maxHeight: itemHeight
  145. Repeater{
  146. model: model_values
  147. delegate:Item{
  148. height: table_row.maxHeight
  149. width: modelData.width
  150. Loader{
  151. property var model : modelData
  152. property var dataModel : listModel
  153. property var dataObject : itemObject
  154. anchors.fill: parent
  155. sourceComponent: {
  156. if(model.itemData instanceof Component){
  157. return model.itemData
  158. }
  159. return com_text
  160. }
  161. onHeightChanged:
  162. {
  163. table_row.maxHeight = Math.max(table_row.maxHeight,height,itemHeight)
  164. parent.height = table_row.maxHeight
  165. table_row.parent.height = table_row.maxHeight
  166. }
  167. }
  168. }
  169. }
  170. }
  171. FluDivider{
  172. width: parent.width
  173. height: 1
  174. anchors.right: parent.right
  175. anchors.bottom: parent.bottom
  176. }
  177. }
  178. }
  179. }
  180. Component{
  181. id:com_text
  182. Item{
  183. FluCopyableText{
  184. id:table_value
  185. text:String(model.itemData)
  186. width: Math.min(parent.width - 14,implicitWidth)
  187. wrapMode: Text.WordWrap
  188. onImplicitHeightChanged: parent.parent.parent.height = Math.max(implicitHeight + 20,itemHeight)
  189. anchors{
  190. verticalCenter: parent.verticalCenter
  191. left: parent.left
  192. leftMargin: 14
  193. }
  194. MouseArea{
  195. id:item_mouse
  196. hoverEnabled: true
  197. anchors.fill: parent
  198. cursorShape: Qt.IBeamCursor
  199. acceptedButtons: Qt.NoButton
  200. }
  201. FluTooltip{
  202. visible: item_mouse.containsMouse
  203. text:parent.text
  204. delay: 1000
  205. }
  206. }
  207. }
  208. }
  209. function getObject(index){
  210. return model_data_source.get(index)
  211. }
  212. function getObjectValues(index) {
  213. var obj = model_data_source.get(index)
  214. if(!obj)
  215. return
  216. var data = []
  217. for(var i=0;i<model_coumns.count;i++){
  218. var item = model_coumns.get(i)
  219. data.push({itemData:obj[item.dataIndex],width:item.width})
  220. }
  221. return data;
  222. }
  223. }