Skip to content

Commit 541668f

Browse files
committed
fix: itemHeight is invalid, delete virtualLines at the same time, use height and itemHeight to calculate. #177
1 parent f4a66e2 commit 541668f

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

example/VirtualList.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<h3>Options:</h3>
88
<div class="options">
99
<div>
10-
<label>virtualLines</label>
11-
<input v-model="state.virtualLines" type="number" />
10+
<label>itemHeight</label>
11+
<input v-model="state.itemHeight" type="number" />
1212
</div>
1313
<div>
1414
<label>showLine</label>
@@ -31,9 +31,8 @@
3131
<div class="block">
3232
<h3>vue-json-pretty(1000+ items):</h3>
3333
<vue-json-pretty
34-
style="height: 400px"
3534
:virtual="true"
36-
:virtualLines="+state.virtualLines"
35+
:item-height="+state.itemHeight"
3736
:data="state.data"
3837
:deep="state.deep"
3938
:show-line="state.showLine"
@@ -53,7 +52,7 @@ const defaultData = {
5352
data: [],
5453
};
5554
56-
for (let i = 0; i < 100000; i++) {
55+
for (let i = 0; i < 1000; i++) {
5756
defaultData.data.push({
5857
news_id: i,
5958
title: 'iPhone X Review: Innovative future with real black technology',
@@ -73,7 +72,7 @@ export default defineComponent({
7372
showLine: true,
7473
collapsedOnClickBrackets: true,
7574
deep: 3,
76-
virtualLines: 20,
75+
itemHeight: 20,
7776
});
7877
7978
watch(

src/components/Tree/index.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ export default defineComponent({
3838
type: Boolean,
3939
default: false,
4040
},
41-
//When using virtual scroll, set the number of items there can be
42-
virtualLines: {
41+
// When using virtual scroll, set the height of tree.
42+
height: {
4343
type: Number,
44-
default: 10,
44+
default: 400,
4545
},
4646
// When using virtual scroll, define the height of each row.
4747
itemHeight: {
@@ -134,7 +134,7 @@ export default defineComponent({
134134
const updateVisibleData = (flatDataValue: FlatDataType) => {
135135
if (props.virtual) {
136136
const treeRefValue = tree.value;
137-
const visibleCount = props.virtualLines;
137+
const visibleCount = props.height / props.itemHeight;
138138
const scrollTop = (treeRefValue && treeRefValue.scrollTop) || 0;
139139
const scrollCount = Math.floor(scrollTop / props.itemHeight);
140140
let start =
@@ -234,6 +234,7 @@ export default defineComponent({
234234
render() {
235235
const {
236236
virtual,
237+
height,
237238
itemHeight,
238239
customValueFormatter,
239240
showDoubleQuotes,
@@ -281,6 +282,7 @@ export default defineComponent({
281282
onBracketsClick={onBracketsClick}
282283
onSelectedChange={onSelectedChange}
283284
onValueChange={onValueChange}
285+
style={itemHeight && itemHeight !== 20 ? { lineHeight: `${itemHeight}px` } : {}}
284286
/>
285287
));
286288

@@ -294,8 +296,10 @@ export default defineComponent({
294296
onScroll={onTreeScroll}
295297
>
296298
{virtual ? (
297-
<div style={{ height: `${flatData.length * itemHeight}px` }}>
298-
<div style={{ transform: `translateY(${state.translateY}px)` }}>{nodeContent}</div>
299+
<div style={{ height: `${height}px` }}>
300+
<div style={{ height: `${flatData.length * itemHeight}px` }}>
301+
<div style={{ transform: `translateY(${state.translateY}px)` }}>{nodeContent}</div>
302+
</div>
299303
</div>
300304
) : (
301305
nodeContent

src/components/TreeNode/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, reactive, computed, PropType } from 'vue';
1+
import { defineComponent, reactive, computed, PropType, CSSProperties } from 'vue';
22
import Brackets from 'src/components/Brackets';
33
import CheckController from 'src/components/CheckController';
44
import Carets from 'src/components/Carets';
@@ -90,6 +90,7 @@ export default defineComponent({
9090
collapsed: Boolean,
9191
// Whether the current node is checked(When using the selection function).
9292
checked: Boolean,
93+
style: Object as PropType<CSSProperties>,
9394
onTreeNodeClick: {
9495
type: Function as PropType<(node: NodeDataType) => void>,
9596
},

src/components/TreeNode/styles.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.@{css-prefix}-tree__node {
88
display: flex;
99
position: relative;
10+
line-height: 20px;
1011

1112
&.has-selector {
1213
padding-left: @selector-span;

0 commit comments

Comments
 (0)