common-list.vue 2.9 KB

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