task-card.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="task-card">
  3. <view class="task-card-flex">
  4. <view
  5. v-for="(col, index) in _columns"
  6. class="task-col"
  7. :key="index"
  8. :style="getColStyle(col)"
  9. @click.stop="handleClickField(col.key)"
  10. >
  11. <view class="task-col-label" :style="getColLabelStyle(col)"
  12. >{{ col.title }}:</view
  13. >
  14. <view class="task-col-value" :style="getColValueStyle(col)">{{
  15. getFieldValue(col)
  16. }}</view>
  17. </view>
  18. </view>
  19. <view class="task-card-right-top" v-if="$slots.rightTop">
  20. <slot name="rightTop"></slot>
  21. </view>
  22. <u-divider></u-divider>
  23. <!-- 操作区域 -->
  24. <slot name="action"></slot>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. props: {
  30. /**
  31. * title: 标题
  32. * key: key值
  33. * formatter: 格式化函数
  34. * isShow: 显/隐函数
  35. * span: 列, 12|24
  36. */
  37. columns: {
  38. type: Array,
  39. default: () => [],
  40. },
  41. formData: {
  42. type: Object,
  43. default: () => {},
  44. },
  45. },
  46. computed: {
  47. _columns() {
  48. return this.columns.filter((col) => {
  49. if (col.isShow && col.isShow instanceof Function) {
  50. return col.isShow(col.key, { ...this.formData }, col);
  51. }
  52. return true;
  53. });
  54. },
  55. },
  56. data() {
  57. return {};
  58. },
  59. methods: {
  60. /**
  61. * 点击事件
  62. * @param {String} key 字段名
  63. */
  64. handleClickField(key) {
  65. this.$emit("handleClickField", key, { ...this.formData });
  66. },
  67. getColStyle(col) {
  68. if (col.span) {
  69. return { width: `${(col.span / 24) * 100}%` };
  70. }
  71. return { width: "100%" };
  72. },
  73. getColLabelStyle(col) {
  74. return col.titleStyle || {};
  75. },
  76. getColValueStyle(col) {
  77. return col.valueStyle || {};
  78. },
  79. getFieldValue(col) {
  80. const formData = { ...this.formData };
  81. if (col.formatter && col.formatter instanceof Function) {
  82. return col.formatter(col.key, formData, col);
  83. }
  84. return formData[col.key] || "-";
  85. },
  86. },
  87. };
  88. </script>
  89. <style lang="scss" scoped>
  90. .task-card {
  91. width: 100%;
  92. background-color: #fff;
  93. border-radius: 16rpx;
  94. padding: 24rpx 24rpx 0;
  95. margin-bottom: 24rpx;
  96. position: relative;
  97. .task-card-right-top {
  98. position: absolute;
  99. right: 24rpx;
  100. top: 36rpx;
  101. }
  102. .task-card-flex {
  103. width: 100%;
  104. display: flex;
  105. flex-wrap: wrap;
  106. }
  107. .task-col {
  108. display: flex;
  109. align-items: center;
  110. color: #666;
  111. font-family: "PingFang SC";
  112. font-size: 28rpx;
  113. font-weight: 400;
  114. margin-top: 16rpx;
  115. &-value {
  116. overflow: hidden;
  117. white-space: nowrap;
  118. text-overflow: ellipsis;
  119. }
  120. }
  121. }
  122. </style>