luckDraw.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <div class="luckDraw">
  3. <div class="luckDraw_title">
  4. <span class="text">中奖名单</span>
  5. </div>
  6. <div class="luckDraw_select">
  7. <el-input v-model="form.text1" placeholder="请输入中奖姓名" clearable></el-input>
  8. <el-button type="primary" @click="selectClick" :loading="selectLoading">
  9. <i class="el-icon-search"></i> 查询
  10. </el-button>
  11. <el-button type="primary" @click="exportData()" :loading="downloadLoading">
  12. <i class="el-icon-document"></i> 导出(Excel)
  13. </el-button>
  14. </div>
  15. <div class="luckDraw_table">
  16. <el-table
  17. ref="tab"
  18. :data="tableData"
  19. style="width: 100%;"
  20. border
  21. :height="height"
  22. :stripe="true"
  23. :highlight-current-row="false"
  24. :row-style="tableRowStyle"
  25. :header-cell-style="tableHeaderColor"
  26. :loading="tableLoading"
  27. element-tableLoading-text="玩命加载中"
  28. element-tableLoading-spinner="el-icon-tableLoading"
  29. >
  30. <el-table-column
  31. type="index"
  32. label="序号"
  33. fixed="left"
  34. width="50"
  35. >
  36. </el-table-column>
  37. <el-table-column
  38. sortable
  39. v-for="(item,i) in tablehead"
  40. :key="i"
  41. :prop="item.prop"
  42. :label="item.label"
  43. :min-width="item.width || width">
  44. </el-table-column>
  45. </el-table>
  46. <div class="fy">
  47. <el-pagination
  48. @size-change="handleSizeChange"
  49. @current-change="handleCurrentChange"
  50. :current-page="currentPage"
  51. :page-sizes="pageSizes"
  52. :page-size="pageSize"
  53. layout="total, sizes, prev, pager, next, jumper"
  54. :total="total"
  55. :hide-on-single-page="true">
  56. </el-pagination>
  57. </div>
  58. </div>
  59. </div>
  60. </template>
  61. <script>
  62. import luckDrawTableHead from './luckDrawTableHead'
  63. export default {
  64. data(){
  65. return{
  66. //查询按钮的状态
  67. selectLoading:false,
  68. //导出按钮的状态
  69. downloadLoading:false,
  70. //导出excel文件的名字
  71. tableTitle:'中奖名单',
  72. //表格的高度
  73. height:null,
  74. //表格的每一列的宽度,若没有再表头设置则采用
  75. width:150,
  76. //总条目数
  77. total:null,
  78. //每页显示条目个数
  79. pageSize: 100,
  80. //每一页面的显示的条目个数数组
  81. pageSizes: [50, 100, 200, 300],
  82. //当前页数
  83. currentPage: 1,
  84. //表格的加载状态
  85. tableLoading: false,
  86. //查询条件
  87. form:{
  88. text1:null,
  89. },
  90. //后端给的数据
  91. tableData1:[],
  92. //表格渲染的数据
  93. tableData:[],
  94. //表格的表头数据
  95. tablehead:[],
  96. }
  97. },
  98. created(){
  99. this.tablehead = luckDrawTableHead.luckDrawTableHead;
  100. //动态监听页面的高度,如果改变则改变
  101. window.addEventListener("resize", this.getHeight);
  102. this.getHeight();
  103. this.initialization();
  104. this.changeList();
  105. },
  106. methods:{
  107. getHeight(){
  108. this.height = window.innerHeight - 200
  109. },
  110. initialization(){
  111. let arr;
  112. for(var i = 0 ;i<1000;i++){
  113. let num = i + 1;
  114. arr = {
  115. prop1:'测试' + num,
  116. }
  117. this.tableData1.push(arr)
  118. }
  119. this.total = this.tableData1.length;
  120. },
  121. //查询事件
  122. selectClick(){
  123. this.selectLoading = true;
  124. console.log(this.form)
  125. this.selectLoading = false;
  126. },
  127. //行间样式
  128. tableRowStyle(){
  129. return 'background-color:#CCF1FF;color:#000;text-align:center;height:10px'
  130. },
  131. //表头样式
  132. tableHeaderColor(){
  133. return 'background-color:#0C2278;color:#fff;text-align:center;height:20px'
  134. },
  135. changeList(){
  136. if(this.tableData1.length){
  137. this.tableData = [];
  138. var x = (this.currentPage - 1) * this.pageSize;
  139. var y = this.currentPage * this.pageSize;
  140. if(x >= this.total){
  141. x = this.total;
  142. }
  143. if(y >= this.total){
  144. y = this.total
  145. }
  146. for(x;x<y;x++){
  147. this.tableData.push(this.tableData1[x])
  148. }
  149. }
  150. this.tableLoading = false;
  151. },
  152. handleSizeChange(val) {
  153. this.tableLoading = true;
  154. this.pageSize = val
  155. this.changeList();
  156. },
  157. handleCurrentChange(val) {
  158. this.tableLoading = true;
  159. this.currentPage = val
  160. this.changeList();
  161. },
  162. exportData(){
  163. if(this.tableData.length){
  164. let tHeader = [];
  165. let filterVal = [];
  166. this.tablehead.filter( (item,i) =>{
  167. tHeader.push(item.label);
  168. filterVal.push(item.prop);
  169. } )
  170. this.export2Excel(tHeader,filterVal,this.tableData);
  171. }else{
  172. this.$alert('没有查询到数据,不能使用导出功能', '提示', {
  173. confirmButtonText: '确定',
  174. callback: action => {
  175. }
  176. });
  177. }
  178. },
  179. export2Excel(tHeader,filterVal,dataTabel) {
  180. var that = this;
  181. this.downloadLoading = true
  182. require.ensure([], () => {
  183. const { export_json_to_excel } = require("@/assets/excel/Export2Excel.js"); //这里必须使用绝对路径,使用@/+存放export2Excel的路径
  184. let list = dataTabel;
  185. let data = that.formatJson(filterVal, list);
  186. export_json_to_excel(tHeader, data, that.tableTitle); // 导出的表格名称
  187. });
  188. this.downloadLoading = false;
  189. },
  190. //3.格式转换
  191. formatJson(filterVal, jsonData) {
  192. return jsonData.map((v) => filterVal.map((j) => v[j]));
  193. },
  194. }
  195. }
  196. </script>
  197. <style lang="scss">
  198. .luckDraw{
  199. .luckDraw_title{
  200. width: 100%;height: 100px;
  201. display: flex;
  202. justify-content: center;
  203. align-items: center;
  204. font-size: 46px;
  205. font-weight: 900;
  206. color: red;
  207. }
  208. .luckDraw_select{
  209. display: flex;
  210. align-items: center;
  211. width: 100%;height: 60px;
  212. padding-left: 50px;
  213. .el-input{
  214. width: 200px;
  215. margin-right: 20px;
  216. }
  217. }
  218. .fy{
  219. display: flex;
  220. align-items: center;
  221. height: 40px;
  222. width: 100%;
  223. }
  224. }
  225. </style>