common-list.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div class="common-list">
  3. <ul>
  4. <li v-for="i in listData" :key="i.id">
  5. <div class="left-box" style="margin-right: 20px" v-if="isCheckbox">
  6. <van-checkbox
  7. v-model="i.checked"
  8. icon-size="15px"
  9. shape="square"
  10. @change="listCheck"
  11. :disabled="optionalValue ? optionalValue != i[optionalKey] : false"
  12. ></van-checkbox>
  13. </div>
  14. <p>
  15. </p>
  16. <div
  17. class="center-content"
  18. style="line-height: 24px"
  19. @click="listCk(i)"
  20. >
  21. <div v-for="j in config" :key="j.prop" style="display: flex">
  22. <span style="width:72px;">{{ j.label }}:</span>
  23. <span v-if="j.type && j.type === 'slot'" style="flex: 1;">
  24. <slot :name="j.slotName" :row="i">
  25. {{ j.slotName }}插槽占位符
  26. </slot>
  27. </span>
  28. <span v-else style="flex: 1;">
  29. {{ i[j.prop] || ''}}
  30. </span>
  31. </div>
  32. </div>
  33. <div class="more-box" v-if="showMore" @click="listCk(i)">
  34. <van-icon name="arrow" />
  35. </div>
  36. </li>
  37. </ul>
  38. </div>
  39. </template>
  40. <script setup>
  41. import { ref, getCurrentInstance, onMounted, defineProps, watch } from "vue";
  42. const proxy = getCurrentInstance().proxy;
  43. defineProps({
  44. data: {
  45. type: Array,
  46. default: [],
  47. },
  48. config: {
  49. type: Object,
  50. default: [],
  51. },
  52. showMore: {
  53. type: Boolean,
  54. default: true,
  55. },
  56. isCheckbox: {
  57. type: Boolean,
  58. default: false,
  59. },
  60. optionalKey: {
  61. type: String,
  62. default: null,
  63. },
  64. });
  65. let listData = ref([]);
  66. let checkList = [];
  67. watch(
  68. () => proxy.data,
  69. (newVal) => {
  70. listData.value = newVal.map((i) => {
  71. //判断是否有选中的
  72. if (checkList.length > 0) {
  73. checkList.map((j) => {
  74. if (i.id == j.id) {
  75. i.checked = true;
  76. }
  77. });
  78. }
  79. return i;
  80. });
  81. }
  82. );
  83. const optionalValue = ref(null);
  84. const listCheck = () => {
  85. checkList = [];
  86. listData.value.map((i) => {
  87. if (i.checked) {
  88. checkList.push(i);
  89. if (!optionalValue.value) {
  90. optionalValue.value = i[proxy.optionalKey];
  91. }
  92. }
  93. });
  94. if (checkList.length == 0) {
  95. optionalValue.value = null;
  96. }
  97. proxy.$emit("onCheck", checkList);
  98. };
  99. const listCk = (item) => {
  100. proxy.$emit("onClick", item);
  101. };
  102. </script>
  103. <style lang="scss">
  104. .common-list {
  105. ul {
  106. margin-top: 10px;
  107. li {
  108. list-style: none;
  109. position: relative;
  110. display: flex;
  111. box-sizing: border-box;
  112. align-items: center;
  113. justify-content: space-between;
  114. padding: 12px 16px;
  115. background: #fff;
  116. border-bottom: 1px solid #f1f1f1;
  117. .center-content {
  118. flex: 1;
  119. }
  120. .left-box {
  121. }
  122. }
  123. }
  124. }
  125. </style>