-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Angular] How to give a component variable the value of a clicked item #357
Comments
Maybe selected.mp4 |
If for some reason you don't want to use the let selectedTimeslot: TimeSlot = {} as TimeSlot;
function myItemClickEvent( ev ){
const itemElement = ev.target.closest('.gstc__chart-timeline-items-row-item');
const itemId = itemElement.dataset.gstcid;
const item = gstc.api.getItem(itemId);
console.log('Item click from template', item);
// now you have item id so you can get item data and save it to your global variable
selectedTimeslot = gstc.state.get('config.chart.items.'+itemId);
}
function chartTimelineItemsRowItemTemplate({
className,
labelClassName,
styleMap,
cache,
shouldDetach,
cutterLeft,
cutterRight,
getContent,
actions,
slots,
html,
vido,
props,
}) {
const detach = shouldDetach || !props || !props.item;
return cache(
detach
? null
: slots.html(
"outer",
html`
<div
class=${className}
data-gstcid=${props.item.id}
data-actions=${actions()}
style=${styleMap.directive()}
@click=${myItemClickEvent}
>
${slots.html(
"inner",
html`
${cutterLeft()}
<div class=${labelClassName}>${slots.html("content", getContent())}</div>
${cutterRight()}
`
)}
</div>
`
)
);
}
// config
const config = {
/* ... other settings ... */
templates: {
"chart-timeline-items-row-item": chartTimelineItemsRowItemTemplate,
},
/* ... other settings ... */
}; |
PS try not to use the keyword To solve this problem and the code from the gantt has to be in the class you can use methods inside the class that return the appropriate configuration using the class MyClass {
public gstc; // your gstc instance https://github.com/neuronetio/angular-gantt-schedule-timeline-calendar-example/blob/master/src/app/app.component.ts#L98
public selectedTimeslot = {};
public getItemClickEvent(){
const self = this;
return function myItemClickEvent(ev) {
const itemElement = ev.target.closest(".gstc__chart-timeline-items-row-item");
const itemId = itemElement.dataset.gstcid;
// now you have item id so you can get item data and save it to your variable
self.selectedTimeslot = self.gstc.api.getItem(itemId);
console.log("Item click from template", self.selectedTimeslot);
// or with state
// self.selectedTimeslot = self.gstc.state.get("config.chart.items." + itemId);
}
}
public getItemTemplate() {
const self = this;
const myClickEvent = self.getItemClickEvent(); // we can get things right away to prevent garbarge colletor cleanup
return function chartTimelineItemsRowItemTemplate({
className,
labelClassName,
styleMap,
cache,
shouldDetach,
cutterLeft,
cutterRight,
getContent,
actions,
slots,
html,
vido,
props,
}) {
const detach = shouldDetach || !props || !props.item;
return cache(
detach
? null
: slots.html(
"outer",
html`
<div
class=${className}
data-gstcid=${props.item.id}
data-actions=${actions()}
style=${styleMap.directive()}
@click=${myClickEvent} // or self.getItemClickEvent()
>
${slots.html(
"inner",
html`
${cutterLeft()}
<div class=${labelClassName}>${slots.html("content", getContent())}</div>
${cutterRight()}
`
)}
</div>
`
)
);
};
}
public getGSTCConfig() {
const self = this;
// config
const config = {
/* ... other settings ... */
templates: {
"chart-timeline-items-row-item": self.getItemTemplate(),
},
/* ... other settings ... */
};
return config;
}
} 💡 We do similar things in angular example |
Your question and description of the problem goes here
How can give my variable the value of an item I clicked on? I can't use classwide varibales with
this.
inside of the action function. I want to display an item's properties, e.g. label, color, time, inside of another html element. In my case I want to set the variableselectedTimeslot
to the item that has been clicked on.Code
You can paste your code and data examples here if it will help to answer the question.
Now I want
public selectedTimeslot
to bedata.item
- how can I achieve that?Thanks a lot in advance!
The text was updated successfully, but these errors were encountered: