[Bug] updateFilterRules之后,checkbox选中到另一行
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 3.7k
- Forks
- 486
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 16
Description
Version
1.10.0
Link to Minimal Reproduction
https://visactor.io/vtable/demo/list-table-data-analysis/list-table-data-filter
Steps to Reproduce
`var tableInstance;
VTable.register.icon('filter', {
name: 'filter',
type: 'svg',
width: 20,
height: 20,
marginRight: 6,
positionType: VTable.TYPES.IconPosition.right,
// interactive: true,
svg: ''
});
VTable.register.icon('filtered', {
name: 'filtered',
type: 'svg',
width: 20,
height: 20,
marginRight: 6,
positionType: VTable.TYPES.IconPosition.right,
// interactive: true,
svg: ''
});
fetch('https://lf9-dp-fe-cms-tos.byteorg.com/obj/bit-cloud/VTable/olympic-winners.json')
.then(res => res.json())
.then(data => {
const columns = [
{
field: 'athlete',
title: 'athlete',
width:120,
aggregation: {
aggregationType: VTable.TYPES.AggregationType.NONE,
formatFun(value) {
return 'Total:';
}
}
},
{
field: 'age',
title: 'age',
cellType:"checkbox",
aggregation: {
aggregationType: VTable.TYPES.AggregationType.AVG,
formatFun(value) {
return Math.round(value) + '(Avg)';
}
}
},
{
field: 'country',
title: 'country',
width:240,
headerIcon: 'filter',
aggregation: {
aggregationType: VTable.TYPES.AggregationType.CUSTOM,
aggregationFun(values, records) {
// 使用 reduce() 方法统计金牌数
const goldMedalCountByCountry = records.reduce((acc, data) => {
const country = data.country;
const gold = data.gold;
if (acc[country]) {
acc[country] += gold;
} else {
acc[country] = gold;
}
return acc;
}, {});
// 找出金牌数最多的国家
let maxGoldMedals = 0;
let countryWithMaxGoldMedals = '';
for (const country in goldMedalCountByCountry) {
if (goldMedalCountByCountry[country] > maxGoldMedals) {
maxGoldMedals = goldMedalCountByCountry[country];
countryWithMaxGoldMedals = country;
}
}
return {
country: countryWithMaxGoldMedals,
gold: maxGoldMedals
};
},
formatFun(value) {
return `Top country in gold medals: ${value.country},\nwith ${value.gold} gold medals`;
}
}
},
{ field: 'year', title: 'year', headerIcon: 'filter' },
{ field: 'sport', title: 'sport', headerIcon: 'filter' },
{
field: 'gold',
title: 'gold',
aggregation: {
aggregationType: VTable.TYPES.AggregationType.SUM,
formatFun(value) {
return Math.round(value) + '(Sum)';
}
}
},
{
field: 'silver',
title: 'silver',
aggregation: {
aggregationType: VTable.TYPES.AggregationType.SUM,
formatFun(value) {
return Math.round(value) + '(Sum)';
}
}
},
{
field: 'bronze',
title: 'bronze',
aggregation: {
aggregationType: VTable.TYPES.AggregationType.SUM,
formatFun(value) {
return Math.round(value) + '(Sum)';
}
}
},
{
field: 'total',
title: 'total',
aggregation: {
aggregationType: VTable.TYPES.AggregationType.SUM,
formatFun(value) {
return Math.round(value) + '(Sum)';
}
}
}
];
const option = {
columns,
records: data,
autoWrapText: true,
heightMode: 'autoHeight',
widthMode: 'autoWidth',
bottomFrozenRowCount: 1,
theme: VTable.themes.ARCO.extends({
bottomFrozenStyle: {
fontFamily: 'PingFang SC',
fontWeight: 500
}
})
};
const t0 = window.performance.now();
tableInstance = new VTable.ListTable(document.getElementById(CONTAINER_ID), option);
window.tableInstance = tableInstance;
const filterListValues = {
country: ['all', 'China', 'United States', 'Australia'],
year: ['all', '2004', '2008', '2012', '2016', '2020'],
sport: ['all', 'Swimming', 'Cycling', 'Biathlon', 'Short-Track Speed Skating', 'Nordic Combined']
};
let filterListSelectedValues = '';
let lastFilterField;
tableInstance.on('icon_click', args => {
const { col, row, name } = args;
if (name === 'filter') {
const field = tableInstance.getHeaderField(col, row);
if (select && lastFilterField === field) {
removeFilterElement();
lastFilterField = null;
} else if (!select || lastFilterField !== field) {
const rect = tableInstance.getCellRelativeRect(col, row);
createFilterElement(filterListValues[field], filterListSelectedValues, field, rect);
lastFilterField = field;
}
}
});
let filterContainer = tableInstance.getElement();
let select;
function createFilterElement(values, curValue, field, positonRect) {
// create select tag
select = document.createElement('select');
select.setAttribute('type', 'text');
select.style.position = 'absolute';
select.style.padding = '4px';
select.style.width = '100%';
select.style.boxSizing = 'border-box';
// create option tags
let opsStr = '';
values.forEach(item => {
opsStr +=
item === curValue
? `<option value="${item}" selected>${item}</option>`
: `<option value="${item}" >${item}</option>`;
});
select.innerHTML = opsStr;
filterContainer.appendChild(select);
select.style.top = positonRect.top + positonRect.height + 'px';
select.style.left = positonRect.left + 'px';
select.style.width = positonRect.width + 'px';
select.style.height = positonRect.height + 'px';
select.addEventListener('change', () => {
filterListSelectedValues = select.value;
tableInstance.updateFilterRules([
{
filterKey: field,
filteredValues: select.value
}
]);
removeFilterElement();
//更新列头icon
columns.forEach(col => {
if (col.field === field) {
col.headerIcon = 'filtered';
}
});
tableInstance.updateColumns(columns);
});
}
function removeFilterElement() {
filterContainer.removeChild(select);
select.removeEventListener('change', () => {
// this.successCallback();
});
select = null;
}
});`
1、勾选第一行,
2、筛选选择China。
选择的人对不齐
Current Behavior
勾选状态到了另一行上
Expected Behavior
正确勾选
Environment
- OS:
- Browser:
- Framework:
Any additional comments?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked minimal reproduction and trace the tableInstance.updateFilterRules call after selecting China. Verify the checkbox state before and after filtering; done means the checked item remains aligned with its original record rather than moving to another row.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100