print.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <script setup lang="ts">
  2. import Print from "@/utils/print";
  3. import { reactive, ref } from "vue";
  4. import { VxeTablePropTypes } from "vxe-table";
  5. import Line from "../welcome/components/Line.vue";
  6. defineOptions({
  7. name: "Print"
  8. });
  9. interface User {
  10. date: string;
  11. name: string;
  12. address: string;
  13. }
  14. const demo1 = reactive({
  15. tableData: [
  16. {
  17. id: 10001,
  18. name: "Test1",
  19. role: "Develop",
  20. sex: "Man",
  21. age: 28,
  22. address: "test abc"
  23. },
  24. {
  25. id: 10002,
  26. name: "Test2",
  27. role: "Test",
  28. sex: "Women",
  29. age: 22,
  30. address: "Guangzhou"
  31. },
  32. {
  33. id: 10003,
  34. name: "Test3",
  35. role: "PM",
  36. sex: "Man",
  37. age: 32,
  38. address: "Shanghai"
  39. },
  40. {
  41. id: 10004,
  42. name: "Test4",
  43. role: "Designer",
  44. sex: "Women",
  45. age: 24,
  46. address: "Shanghai"
  47. }
  48. ]
  49. });
  50. const value = ref("1");
  51. const options = [
  52. {
  53. value: "1",
  54. el: ".el-table",
  55. label: "Element-Plus Table"
  56. },
  57. {
  58. value: "2",
  59. el: ".vxe-table",
  60. label: "Vxe Table"
  61. },
  62. {
  63. value: "3",
  64. el: ".echart",
  65. label: "Echart"
  66. },
  67. {
  68. value: "4",
  69. el: ".img",
  70. label: "Image"
  71. }
  72. ];
  73. function onPrint() {
  74. let el = options.filter(v => v.value === value.value)[0]?.el;
  75. Print(el).toPrint;
  76. }
  77. const headerCellStyle: VxeTablePropTypes.HeaderCellStyle = ({ column }) => {
  78. if (column.property === "name") {
  79. return {
  80. backgroundColor: "#f60",
  81. color: "#ffffff"
  82. };
  83. }
  84. };
  85. const rowStyle: VxeTablePropTypes.RowStyle = ({ rowIndex }) => {
  86. if ([2, 3, 5].includes(rowIndex)) {
  87. return {
  88. backgroundColor: "red",
  89. color: "#ffffff"
  90. };
  91. }
  92. };
  93. const cellStyle: VxeTablePropTypes.CellStyle = ({ row, column }) => {
  94. if (column.property === "sex") {
  95. if (row.sex >= "1") {
  96. return {
  97. backgroundColor: "#187"
  98. };
  99. } else if (row.age === 26) {
  100. return {
  101. backgroundColor: "#2db7f5"
  102. };
  103. }
  104. }
  105. };
  106. const tableRowClassName = ({ rowIndex }: { row: User; rowIndex: number }) => {
  107. if (rowIndex === 1) {
  108. return "warning-row";
  109. } else if (rowIndex === 3) {
  110. return "success-row";
  111. }
  112. return "";
  113. };
  114. const tableData: User[] = [
  115. {
  116. date: "2016-05-03",
  117. name: "Tom",
  118. address: "No. 189, Grove St, Los Angeles"
  119. },
  120. {
  121. date: "2016-05-02",
  122. name: "Tom",
  123. address: "No. 189, Grove St, Los Angeles"
  124. },
  125. {
  126. date: "2016-05-04",
  127. name: "Tom",
  128. address: "No. 189, Grove St, Los Angeles"
  129. },
  130. {
  131. date: "2016-05-01",
  132. name: "Tom",
  133. address: "No. 189, Grove St, Los Angeles"
  134. }
  135. ];
  136. </script>
  137. <template>
  138. <el-card>
  139. <template #header>
  140. <div class="card-header">
  141. <span class="font-medium">打印功能(报表、图表、图片)</span>
  142. <div>
  143. <el-select
  144. v-model="value"
  145. class="m-2"
  146. placeholder="Select"
  147. size="small"
  148. >
  149. <el-option
  150. v-for="item in options"
  151. :key="item.value"
  152. :label="item.label"
  153. :value="item.value"
  154. />
  155. </el-select>
  156. <el-button size="small" type="primary" @click="onPrint"
  157. >打印</el-button
  158. >
  159. </div>
  160. </div>
  161. </template>
  162. <el-row :gutter="24">
  163. <el-col
  164. :xs="22"
  165. :sm="22"
  166. :md="11"
  167. :lg="11"
  168. :xl="11"
  169. style="margin: 10px; border: 0.01rem solid var(--el-color-primary)"
  170. v-motion
  171. :initial="{
  172. opacity: 0,
  173. y: 100
  174. }"
  175. :enter="{
  176. opacity: 1,
  177. y: 0,
  178. transition: {
  179. delay: 200
  180. }
  181. }"
  182. >
  183. <p class="font-medium pt-1">Element-Plus Table</p>
  184. <el-table
  185. border
  186. :data="tableData"
  187. :row-class-name="tableRowClassName"
  188. class="el-table w-full mt-[40px] mr-[40px]"
  189. >
  190. <el-table-column prop="date" label="Date" width="180" />
  191. <el-table-column prop="name" label="Name" width="180" />
  192. <el-table-column prop="address" label="Address" />
  193. </el-table>
  194. </el-col>
  195. <el-col
  196. :xs="22"
  197. :sm="22"
  198. :md="11"
  199. :lg="11"
  200. :xl="11"
  201. style="margin: 10px; border: 0.01rem solid var(--el-color-primary)"
  202. v-motion
  203. :initial="{
  204. opacity: 0,
  205. y: 100
  206. }"
  207. :enter="{
  208. opacity: 1,
  209. y: 0,
  210. transition: {
  211. delay: 200
  212. }
  213. }"
  214. >
  215. <p class="font-medium pt-1">Vxe Table</p>
  216. <vxe-table
  217. class="vxe-table"
  218. border
  219. style="margin: 40px auto"
  220. :header-cell-style="headerCellStyle"
  221. :row-style="rowStyle"
  222. :cell-style="cellStyle"
  223. :data="demo1.tableData"
  224. >
  225. <vxe-column type="seq" width="60" />
  226. <vxe-column field="name" title="Name" />
  227. <vxe-column field="sex" title="Sex" />
  228. <vxe-column field="age" title="Age" />
  229. <vxe-column field="attr1" title="Attr1" />
  230. <vxe-column field="address" title="Address" show-overflow />
  231. </vxe-table>
  232. </el-col>
  233. <el-col
  234. :xs="22"
  235. :sm="22"
  236. :md="11"
  237. :lg="11"
  238. :xl="11"
  239. style="
  240. width: 200px;
  241. height: 300px;
  242. margin: 10px;
  243. border: 0.01rem solid var(--el-color-primary);
  244. "
  245. v-motion
  246. :initial="{
  247. opacity: 0,
  248. y: 100
  249. }"
  250. :enter="{
  251. opacity: 1,
  252. y: 0,
  253. transition: {
  254. delay: 200
  255. }
  256. }"
  257. >
  258. <p class="font-medium pt-1">Echart</p>
  259. <Line class="echart" style="margin: 0 auto" />
  260. </el-col>
  261. <el-col
  262. :xs="22"
  263. :sm="22"
  264. :md="11"
  265. :lg="11"
  266. :xl="11"
  267. style="
  268. width: 200px;
  269. height: 300px;
  270. margin: 10px;
  271. border: 0.01rem solid var(--el-color-primary);
  272. "
  273. v-motion
  274. :initial="{
  275. opacity: 0,
  276. y: 100
  277. }"
  278. :enter="{
  279. opacity: 1,
  280. y: 0,
  281. transition: {
  282. delay: 200
  283. }
  284. }"
  285. >
  286. <p class="font-medium pt-1">Image</p>
  287. <img
  288. src="../../assets/avatars.jpg"
  289. alt="avatars"
  290. class="img"
  291. style="width: 200px; height: 200px; margin: 50px auto"
  292. />
  293. </el-col>
  294. </el-row>
  295. </el-card>
  296. </template>
  297. <style lang="scss" scoped>
  298. :deep(.el-table__row.warning-row) {
  299. --el-table-tr-bg-color: var(--el-color-warning-light-9);
  300. }
  301. :deep(.el-table__row.success-row) {
  302. --el-table-tr-bg-color: var(--el-color-success-light-9);
  303. }
  304. .card-header {
  305. display: flex;
  306. justify-content: space-between;
  307. align-items: center;
  308. }
  309. </style>