index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <script setup lang="ts">
  2. import { ref } from "vue";
  3. import Print from "@/utils/print";
  4. import pieChart from "./pieChart.vue";
  5. defineOptions({
  6. name: "Print"
  7. });
  8. interface User {
  9. date: string;
  10. name: string;
  11. age: number;
  12. address: string;
  13. }
  14. const value = ref("1");
  15. const options = [
  16. {
  17. value: "1",
  18. el: ".el-table",
  19. label: "Table"
  20. },
  21. {
  22. value: "2",
  23. el: ".echart",
  24. label: "Echart"
  25. },
  26. {
  27. value: "3",
  28. el: ".img",
  29. label: "Image"
  30. }
  31. ];
  32. function onPrint() {
  33. const el = options.filter(v => v.value === value.value)[0]?.el;
  34. Print(el).toPrint;
  35. }
  36. const tableRowClassName = ({ rowIndex }: { row: User; rowIndex: number }) => {
  37. if (rowIndex === 1) {
  38. return "warning-row";
  39. } else if (rowIndex === 3) {
  40. return "success-row";
  41. }
  42. return "";
  43. };
  44. const tableData: User[] = [
  45. {
  46. date: "2016-05-03",
  47. name: "Tom",
  48. age: 18,
  49. address: "No. 189, Grove St, Los Angeles"
  50. },
  51. {
  52. date: "2016-05-02",
  53. name: "Tom",
  54. age: 18,
  55. address: "No. 189, Grove St, Los Angeles"
  56. },
  57. {
  58. date: "2016-05-04",
  59. name: "Tom",
  60. age: 18,
  61. address: "No. 189, Grove St, Los Angeles"
  62. },
  63. {
  64. date: "2016-05-01",
  65. name: "Tom",
  66. age: 18,
  67. address: "No. 189, Grove St, Los Angeles"
  68. }
  69. ];
  70. </script>
  71. <template>
  72. <el-card shadow="never">
  73. <template #header>
  74. <div class="card-header">
  75. <span class="font-medium">打印功能(报表、图表、图片)</span>
  76. <el-select
  77. v-model="value"
  78. class="w-[100px]! mr-2"
  79. placeholder="Select"
  80. size="small"
  81. >
  82. <el-option
  83. v-for="item in options"
  84. :key="item.value"
  85. :label="item.label"
  86. :value="item.value"
  87. />
  88. </el-select>
  89. <el-button size="small" type="primary" @click="onPrint">
  90. 打印
  91. </el-button>
  92. </div>
  93. <el-link
  94. class="mt-2"
  95. href="https://github.com/pure-admin/vue-pure-admin/blob/main/src/views/able/print"
  96. target="_blank"
  97. >
  98. 代码位置 src/views/able/print
  99. </el-link>
  100. </template>
  101. <el-row :gutter="24">
  102. <el-col
  103. v-motion
  104. :xs="24"
  105. :sm="24"
  106. :md="24"
  107. :lg="24"
  108. :xl="24"
  109. :initial="{
  110. opacity: 0,
  111. y: 100
  112. }"
  113. :enter="{
  114. opacity: 1,
  115. y: 0,
  116. transition: {
  117. delay: 200
  118. }
  119. }"
  120. >
  121. <p class="font-medium text-lg text-center">Table</p>
  122. <el-table
  123. border
  124. :data="tableData"
  125. :row-class-name="tableRowClassName"
  126. class="el-table w-full mt-[10px]"
  127. >
  128. <el-table-column prop="date" label="Date" />
  129. <el-table-column prop="name" label="Name" />
  130. <el-table-column prop="age" label="age" />
  131. <el-table-column prop="address" label="Address" />
  132. </el-table>
  133. </el-col>
  134. <el-col
  135. v-motion
  136. :xs="11"
  137. :sm="11"
  138. :md="11"
  139. :lg="11"
  140. :xl="11"
  141. :initial="{
  142. opacity: 0,
  143. y: 100
  144. }"
  145. :enter="{
  146. opacity: 1,
  147. y: 0,
  148. transition: {
  149. delay: 200
  150. }
  151. }"
  152. >
  153. <p class="font-medium text-lg text-center">Echart</p>
  154. <pieChart class="echart mt-[10px]" />
  155. </el-col>
  156. <el-col
  157. v-motion
  158. :xs="11"
  159. :sm="11"
  160. :md="11"
  161. :lg="11"
  162. :xl="11"
  163. :initial="{
  164. opacity: 0,
  165. y: 100
  166. }"
  167. :enter="{
  168. opacity: 1,
  169. y: 0,
  170. transition: {
  171. delay: 200
  172. }
  173. }"
  174. >
  175. <p class="font-medium text-lg text-center">Image</p>
  176. <img
  177. src="https://pure-admin-utils.netlify.app/logo.png"
  178. alt="avatars"
  179. class="img mt-[10px] m-auto"
  180. />
  181. </el-col>
  182. </el-row>
  183. </el-card>
  184. </template>
  185. <style lang="scss" scoped>
  186. :deep(.el-table__row.warning-row) {
  187. --el-table-tr-bg-color: var(--el-color-warning-light-9);
  188. }
  189. :deep(.el-table__row.success-row) {
  190. --el-table-tr-bg-color: var(--el-color-success-light-9);
  191. }
  192. </style>