Hi All,
I have an array of data items, that I am displaying in HTML through a repeat.for, I am using a value converter to filter this data based on a search value. Each data item has a html element that is linked to the template via the use of a ref="". Everything appears to work correctly, however if I splice an item from my array, the refs appear to go out of sync, and point at the incorrect input. This behavior does not occur if I remove the value converter from the HTML. Hoping someone may be able to show me where I am going wrong here. I have provided a simple example below
My Data Item:
export interface DataItem {
displayName: string;
id: string;
rInputElement?: HTMLInputElement;
}
View Model:
export class App
{
dataItems: DataItem[];
bTagSearchValue = '';
public attached()
{
return this.getData();
}
getData()
{
// reset data
this.dataItems = [];
// create new data
for (let index = 0; index < 6; index++)
{
let dataItem: DataItem = { displayName: "data Item" + index, id: index.toString() }
this.dataItems.push(dataItem);
}
}
deleteDataItem(id: string)
{
// grab the index of out item
let itemIndex = this.dataItems.findIndex(item => item.id === id);
// remove the item from our array
this.dataItems.splice(itemIndex, 1);
}
updateDataItemName(id: string)
{
let itemIndex = this.dataItems.findIndex(item => item.id === id);
let dataItem = this.dataItems[itemIndex];
let itemDisplayName = dataItem.displayName;
let itemRef = dataItem.rInputElement;
let itemRefInputValue = dataItem.rInputElement.value;
// itemDisplayName will not equal itemRefInputValue once an item has been spliced from dataItems
}
}
My View :
<template>
<input value.bind="bTagSearchValue">
<div repeat.for="item of dataItems | filter: ['displayName']:bTagSearchValue">
<input value.bind="item.displayName" ref="item.rInputElement">
<button click.delegate="updateDataItemName(item.id)">Rename Item</button>
<button click.delegate="deleteDataItem(item.id)">Remove Item</button>
</div>
</template>
The filter value converter I am using:
export class FilterValueConverter
{
toView( array, property, query )
{
if ( query === void 0 || query === null || query === "" || !Array.isArray( array ) )
{
return array;
}
let properties = ( Array.isArray( property ) ? property : [property] ),
term = String( query ).toLowerCase();
return array.filter( ( entry ) =>
properties.some( ( prop ) =>
String( entry[prop] ).toLowerCase().indexOf( term ) >= 0 ) );
}
}
Any help would be greatly appreciated