print.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. class="el-table"
  186. :data="tableData"
  187. border
  188. style="margin: 40px auto; width: 100%"
  189. :row-class-name="tableRowClassName"
  190. >
  191. <el-table-column prop="date" label="Date" width="180" />
  192. <el-table-column prop="name" label="Name" width="180" />
  193. <el-table-column prop="address" label="Address" />
  194. </el-table>
  195. </el-col>
  196. <el-col
  197. :xs="22"
  198. :sm="22"
  199. :md="11"
  200. :lg="11"
  201. :xl="11"
  202. style="margin: 10px; border: 0.01rem solid var(--el-color-primary)"
  203. v-motion
  204. :initial="{
  205. opacity: 0,
  206. y: 100
  207. }"
  208. :enter="{
  209. opacity: 1,
  210. y: 0,
  211. transition: {
  212. delay: 200
  213. }
  214. }"
  215. >
  216. <p class="font-medium pt-1">Vxe Table</p>
  217. <vxe-table
  218. class="vxe-table"
  219. border
  220. style="margin: 40px auto"
  221. :header-cell-style="headerCellStyle"
  222. :row-style="rowStyle"
  223. :cell-style="cellStyle"
  224. :data="demo1.tableData"
  225. >
  226. <vxe-column type="seq" width="60" />
  227. <vxe-column field="name" title="Name" />
  228. <vxe-column field="sex" title="Sex" />
  229. <vxe-column field="age" title="Age" />
  230. <vxe-column field="attr1" title="Attr1" />
  231. <vxe-column field="address" title="Address" show-overflow />
  232. </vxe-table>
  233. </el-col>
  234. <el-col
  235. :xs="22"
  236. :sm="22"
  237. :md="11"
  238. :lg="11"
  239. :xl="11"
  240. style="
  241. width: 200px;
  242. height: 300px;
  243. margin: 10px;
  244. border: 0.01rem solid var(--el-color-primary);
  245. "
  246. v-motion
  247. :initial="{
  248. opacity: 0,
  249. y: 100
  250. }"
  251. :enter="{
  252. opacity: 1,
  253. y: 0,
  254. transition: {
  255. delay: 200
  256. }
  257. }"
  258. >
  259. <p class="font-medium pt-1">Echart</p>
  260. <Line class="echart" style="margin: 0 auto" />
  261. </el-col>
  262. <el-col
  263. :xs="22"
  264. :sm="22"
  265. :md="11"
  266. :lg="11"
  267. :xl="11"
  268. style="
  269. width: 200px;
  270. height: 300px;
  271. margin: 10px;
  272. border: 0.01rem solid var(--el-color-primary);
  273. "
  274. v-motion
  275. :initial="{
  276. opacity: 0,
  277. y: 100
  278. }"
  279. :enter="{
  280. opacity: 1,
  281. y: 0,
  282. transition: {
  283. delay: 200
  284. }
  285. }"
  286. >
  287. <p class="font-medium pt-1">Image</p>
  288. <img
  289. src="../../assets/avatars.jpg"
  290. alt="avatars"
  291. class="img"
  292. style="width: 200px; height: 200px; margin: 50px auto"
  293. />
  294. </el-col>
  295. </el-row>
  296. </el-card>
  297. </template>
  298. <style lang="scss" scoped>
  299. :deep(.el-table__row.warning-row) {
  300. --el-table-tr-bg-color: var(--el-color-warning-light-9);
  301. }
  302. :deep(.el-table__row.success-row) {
  303. --el-table-tr-bg-color: var(--el-color-success-light-9);
  304. }
  305. .card-header {
  306. display: flex;
  307. justify-content: space-between;
  308. align-items: center;
  309. }
  310. </style>