sa-si-dev / virtual-select

A javascript plugin for dropdown with virtual scroll
https://sa-si-dev.github.io/virtual-select
MIT License
252 stars 60 forks source link

Get item on `labelRenderer`? #363

Open alvarotrigo opened 2 weeks ago

alvarotrigo commented 2 weeks ago

It'd be nice to be able to get the DOM item on the labelRenderer callback. So we can apply modifications to it if we need to.

Right now I'm doing the following, but when scrolling up & down very fast it won't find the optionItem by the data-visible-index class. I guess because probably it's not yet created on the DOM.

labelRenderer: sampleLabelRenderer,
selectedLabelRenderer: sampleLabelRenderer

function sampleLabelRenderer(data) {
            let prefix = '';

            if (!data.isCurrentNew && !data.isNew) {
                var optionItem = document.querySelector('.vscomp-option[data-visible-index="'+data.visibleIndex+'"]');

                //fails to get the item
                if(!optionItem){
                    console.error("sale...");
                    console.log(data.label);
                    return `${prefix}${data.label}`;
                }
                console.log("vaa...");

            } else {

            }
            return `<span style="${styles}">${prefix}${data.label}</span>`;
}
alvarotrigo commented 2 weeks ago

So right now I haven't found a reliable way to get the item on the labelRenderer callback