I'm trying to modify the event.currentTarget (a cell of a table) with the information returned by an Apex method, but every time I click on the element, the information of the apex class is not yet available and does nothing on the first click, I have to click on it a second time, so that it has the data already loaded.
Is there any way to be able to modify that event.currentTarget having collected the information and painting it with a single click?
JS
@api hour;
@api day;
@api target;
handleSubdivisionTouchStart(event){
this.hour= event.currentTarget.getAttribute("data-hour");
this.day= event.currentTarget.getAttribute("data-day");
this.target = event.currentTarget;
if(!this.disabled){
if(this.result != null){
this.target.textContent = this.result;
}else{
this.target.textContent = '';
}
}
}
@wire (getData, {hour: '$hour', day: '$day', target : '$target'})
getDataCalendar(result){
const { data, error } = result;
if(data){
this.field1 = data;
}
}else{
this.field1 = '';
this.currentType = '';
}
}
HTML
<span data-type="" data-hour={hour.name} data-day={day.name}
onmousedown={handleSubdivisionTouchStart}
ontouchmove={handleSubdivisionTouchMove}
onmouseup={handleSubdivisionTouchEnd} class="squareSubdivision">
APEX
public static String getData(String hour, String day, event target){
List<Data__c> lstResult = [SELECT Id, Field__c,
FROM Data__c
WHERE Hour__c =: hour AND Day__c =: day];
if (lstResult.size() != 0){
return lstMat[0].Field__c;
}else{
return null;
}
}
Thanks in advance.
lstMatin apex method is never defined, so it should belstResultand maybe in jsthis.resultandthis.field1are the same, lastly the instance variableshouranddayare used as object in HTML, seedata-hour={hour.name}, while inhandleSubdivisionTouchStartyou overwrite them with strings), keep in mind that doing this while not being 100% consisent only makes harder to help you. If possible, please post the code as-is. You could hide the issue (or add a new one) by changing it.