table-page.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <div class="table-page">
  3. <div class="toolbar">
  4. <div class="title" v-if="grid.title.show">
  5. <i :class="grid.title.icon"></i>
  6. <span>{{grid.title.text}}</span>
  7. </div>
  8. <div class="page-bar">
  9. <span class="btn" @click="firstPage()">首页</span>
  10. <span class="btn" @click="prevPage()">上一页</span>
  11. <span v-for="(bar, index) of pageBar" :key="index" @click="gotoPage(bar)"
  12. :class="bar.type + ' ' + (bar.type != 'dot' && page == bar.text ? 'active' : '')">{{bar.text}}</span>
  13. <span class="btn" @click="nextPage()">下一页</span>
  14. <span class="btn" @click="lastPage()">尾页</span>
  15. </div>
  16. <div class="page-num">
  17. <span>共</span>
  18. <span>{{pages}}</span>
  19. <span>页</span>
  20. <span class="total">{{grid.data.total}}</span>
  21. <span>条数据</span>
  22. </div>
  23. <div class="return" v-if="showReturn" @click="onReturn">
  24. 返回
  25. </div>
  26. </div>
  27. <div class="sub-title" v-if="grid.subTitle.show">
  28. <div class="text">{{grid.subTitle.text}}</div>
  29. <div class="sub-text">{{grid.subTitle.subText}}</div>
  30. </div>
  31. <div class="ct">
  32. <table class="com-table">
  33. <thead>
  34. <tr>
  35. <th v-for="(col, index) of grid.column" :key="index" :class="{ 'light': col.is_light }">{{col.name}}
  36. </th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <tr v-for="(row, index) of data" :key="index">
  41. <td v-for="(col, i) of grid.column" :key="i" :class="{ 'light': col.is_light || row.is_light, 'num': col.is_num }">
  42. {{row[col.field]}}</td>
  43. </tr>
  44. </tbody>
  45. </table>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. export default {
  51. // 名称
  52. name: "ComTable",
  53. // 使用组件
  54. components: {},
  55. // 传入参数
  56. props: {
  57. grid: Object,
  58. showReturn: Boolean
  59. },
  60. // 自定义事件
  61. emits: {
  62. onReturn: null,
  63. },
  64. // 数据
  65. data() {
  66. return {
  67. pages: 0,
  68. page: 1,
  69. pageBar: [],
  70. data: []
  71. }
  72. },
  73. // 函数
  74. methods: {
  75. createPageBar: function() {
  76. let pb = [];
  77. const p = this.page;
  78. const ps = this.pages;
  79. if (p >= 4) {
  80. pb.push({
  81. type: "dot",
  82. text: "...",
  83. })
  84. }
  85. for (let i = p - 2; i <= p + 2 && i <= ps; i++) {
  86. if (i <= 0) {
  87. continue;
  88. }
  89. pb.push({
  90. type: "btn num",
  91. text: i,
  92. })
  93. }
  94. if (ps > p + 2) {
  95. pb.push({
  96. type: "dot",
  97. text: "...",
  98. })
  99. }
  100. this.pageBar = pb;
  101. },
  102. createData: function () {
  103. let d = [];
  104. let p = this.page;
  105. let l = this.grid.limit;
  106. let t = this.grid.data.list.length;
  107. for (let i = l * (p - 1); i < l * (p - 1) + l && i < t; i++) {
  108. d.push(this.grid.data.list[i]);
  109. }
  110. this.data = d;
  111. },
  112. gotoPage: function(item) {
  113. if (item.type != 'dot') {
  114. this.page = item.text;
  115. this.createPageBar();
  116. this.createData();
  117. }
  118. },
  119. prevPage: function() {
  120. let p = this.page - 1;
  121. if (p > 0) {
  122. this.page = p;
  123. this.createPageBar();
  124. this.createData();
  125. }
  126. },
  127. nextPage: function() {
  128. let p = this.page + 1;
  129. if (p <= this.pages) {
  130. this.page = p;
  131. this.createPageBar();
  132. this.createData();
  133. }
  134. },
  135. firstPage: function() {
  136. this.page = 1;
  137. this.createPageBar();
  138. this.createData();
  139. },
  140. lastPage: function() {
  141. this.page = this.pages;
  142. this.createPageBar();
  143. this.createData();
  144. },
  145. onReturn: function() {
  146. this.$emit('onReturn');
  147. }
  148. },
  149. // 生命周期钩子
  150. beforeCreate() {
  151. // 创建前
  152. },
  153. created() {
  154. // 创建后
  155. this.pages = Math.ceil(this.grid.data.list.length / this.grid.limit);
  156. this.createPageBar();
  157. this.createData();
  158. },
  159. beforeMount() {
  160. // 渲染前
  161. },
  162. mounted() {
  163. // 渲染后
  164. },
  165. beforeUpdate() {
  166. // 数据更新前
  167. },
  168. updated() {
  169. // 数据更新后
  170. },
  171. }
  172. </script>
  173. <style lang="less">
  174. @titleGray: #9CA5A8;
  175. @rowGray: #606769;
  176. @darkBack: #536268;
  177. .table-page {
  178. display: flex;
  179. flex-direction: column;
  180. .toolbar {
  181. background-color: fade(@darkgray, 40%);
  182. display: flex;
  183. .title {
  184. padding: 0.741vh 0;
  185. color: @green;
  186. font-size: @fontsize;
  187. i {
  188. margin: 0 0.556vh 0 1.481vh;
  189. }
  190. }
  191. .page-bar {
  192. margin-left: auto;
  193. font-size: @fontsize;
  194. color: @gray;
  195. display: flex;
  196. align-items: center;
  197. span {
  198. border-radius: 0.370vh;
  199. padding: 0.185vh 0.278vh;
  200. margin-right: 0.556vh;
  201. &.btn {
  202. cursor: pointer;
  203. &:hover {
  204. color: @green;
  205. border-color: @green;
  206. }
  207. }
  208. }
  209. .num {
  210. font-weight: 600;
  211. width: 2.315vh;
  212. text-align: center;
  213. border: 0.093vh solid @darkgray;
  214. &.active {
  215. color: @green;
  216. border-color: @green;
  217. }
  218. }
  219. }
  220. .page-num {
  221. margin-left: 1.481vh;
  222. margin-right: 1.481vh;
  223. font-size: @fontsize;
  224. color: @gray;
  225. display: flex;
  226. align-items: center;
  227. .total {
  228. margin-left: 0.741vh;
  229. }
  230. }
  231. .return {
  232. margin-right: 1.481vh;
  233. font-size: @fontsize;
  234. color: @gray;
  235. display: flex;
  236. align-items: center;
  237. cursor: pointer;
  238. &:hover {
  239. color: @green;
  240. }
  241. }
  242. }
  243. .sub-title {
  244. background-color: fade(@darkgray, 40%);
  245. display: flex;
  246. padding: 0.741vh 2.222vh;
  247. font-size: @fontsize;
  248. margin-top: 1.481vh;
  249. .text {
  250. color: @write;
  251. }
  252. .sub-text {
  253. margin-left: auto;
  254. color: @write;
  255. }
  256. }
  257. .ct {
  258. flex-grow: 1;
  259. overflow: auto;
  260. }
  261. .com-table {
  262. width: 100%;
  263. border-collapse: collapse;
  264. thead {
  265. tr {
  266. th {
  267. background-color: fade(@darkBack, 60%);
  268. padding: 0.556vh 0;
  269. color: @titleGray;
  270. font-weight: 400;
  271. font-size: @fontsize-s;
  272. position: sticky;
  273. top: 0;
  274. &.light {
  275. color: @green;
  276. }
  277. }
  278. }
  279. }
  280. tbody {
  281. tr {
  282. &:nth-child(2n) {
  283. background-color: fade(@rowGray, 20%);
  284. }
  285. td {
  286. padding: 0.556vh 0;
  287. color: @rowGray;
  288. text-align: center;
  289. font-size: @fontsize-s;
  290. &.light {
  291. color: @green;
  292. }
  293. &.num {
  294. font-family: "Bicubik";
  295. font-weight: 400;
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. </style>