123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <template>
- <div class="common-list">
- <ul>
- <li v-for="i in listData" :key="i.id">
- <div class="left-box" style="margin-right: 20px" v-if="isCheckbox">
- <van-checkbox
- v-model="i.checked"
- icon-size="15px"
- shape="square"
- @change="listCheck"
- :disabled="optionalValue ? optionalValue != i[optionalKey] : false"
- ></van-checkbox>
- </div>
- <p>
-
- </p>
- <div
- class="center-content"
- style="line-height: 24px"
- @click="listCk(i)"
- >
- <div v-for="j in config" :key="j.prop" style="display: flex">
- <span style="width:72px;">{{ j.label }}:</span>
- <span v-if="j.type && j.type === 'slot'" style="flex: 1;">
- <slot :name="j.slotName" :row="i">
- {{ j.slotName }}插槽占位符
- </slot>
- </span>
- <span v-else style="flex: 1;">
- {{ i[j.prop] || ''}}
- </span>
- </div>
- </div>
- <div class="more-box" v-if="showMore" @click="listCk(i)">
- <van-icon name="arrow" />
- </div>
- </li>
- </ul>
- </div>
- </template>
- <script setup>
- import { ref, getCurrentInstance, onMounted, defineProps, watch } from "vue";
- const proxy = getCurrentInstance().proxy;
- defineProps({
- data: {
- type: Array,
- default: [],
- },
- config: {
- type: Object,
- default: [],
- },
- showMore: {
- type: Boolean,
- default: true,
- },
- isCheckbox: {
- type: Boolean,
- default: false,
- },
- optionalKey: {
- type: String,
- default: null,
- },
- });
- let listData = ref([]);
- let checkList = [];
- watch(
- () => proxy.data,
- (newVal) => {
- listData.value = newVal.map((i) => {
- //判断是否有选中的
- if (checkList.length > 0) {
- checkList.map((j) => {
- if (i.id == j.id) {
- i.checked = true;
- }
- });
- }
- return i;
- });
- }
- );
- const optionalValue = ref(null);
- const listCheck = () => {
- checkList = [];
- listData.value.map((i) => {
- if (i.checked) {
- checkList.push(i);
- if (!optionalValue.value) {
- optionalValue.value = i[proxy.optionalKey];
- }
- }
- });
- if (checkList.length == 0) {
- optionalValue.value = null;
- }
- proxy.$emit("onCheck", checkList);
- };
- const listCk = (item) => {
- proxy.$emit("onClick", item);
- };
- </script>
- <style lang="scss">
- .common-list {
- ul {
- margin-top: 10px;
- li {
- list-style: none;
- position: relative;
- display: flex;
- box-sizing: border-box;
- align-items: center;
- justify-content: space-between;
- padding: 12px 16px;
- background: #fff;
- border-bottom: 1px solid #f1f1f1;
- .center-content {
- flex: 1;
- }
- .left-box {
- }
- }
- }
- }
- </style>
|