Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

虚拟列表 #7

Open
maya1900 opened this issue May 4, 2023 · 0 comments
Open

虚拟列表 #7

maya1900 opened this issue May 4, 2023 · 0 comments
Assignees
Labels

Comments

@maya1900
Copy link
Owner

maya1900 commented May 4, 2023

<template>
  <el-dialog v-model="isShow" width="500px">
    <el-scrollbar wrap-class="list-wrapper" @scroll="handleScroll($event)">
      <div class="virtual-list" ref="container" :style="{height: containerHeight}">
        <div :style="{height: totalHeight + 'px', paddingTop: paddingTop + 'px'}" ref="container1">
          <div v-for="(item,index) in data.items" :key="`list-${index}`" class="virtual-list-item" :style="{height: `${itemHeight}px`}">
            <el-radio v-model="data.buyIns" :label="item.id">{{item.name}}</el-radio>
          </div>
        </div>
      </div>
    </el-scrollbar>
  </el-dialog>
</template>

<script setup lang="jsx">
import {computed, onMounted, reactive, ref, watchEffect} from 'vue'

const props = defineProps({
  items: {
    type: Array,
    default: () => []
  }
})
const data = reactive({
  buyIns: '',
  items: []
})

const container = ref(null)
const paddingTop = ref(0)
const itemHeight = ref(30)
const visibleCount = ref(10)
const scrollTop = ref(0)
watchEffect(() => {
  if (container.value) {
    updateVisibleDate()
  }
})
const totalHeight = computed(() => props.items.length * itemHeight.value)
const containerHeight = computed(() => visibleCount.value * itemHeight.value)
function updateVisibleDate() {
  const start = Math.floor(scrollTop.value / itemHeight.value)
  const end = start + visibleCount.value
  data.items = props.items.slice(start, end)
  paddingTop.value = start * itemHeight.value
}
const handleScroll = (event) => {
  scrollTop.value = event.scrollTop
  // updateVisibleDate()
}

const isShow = ref(false)
const open = () => {
  isShow.value = true
}
defineExpose({
  open
})
</script>
<style lang="scss" scoped>
:deep(.list-wrapper) {
  height: 300px;
  width: 100%;
}
.virtual-list {
  overflow-y: auto;
  &-item {
    height: 30px;
  }
}

</style>
@maya1900 maya1900 added the vue label May 4, 2023
@maya1900 maya1900 self-assigned this May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant