Answer the question
In order to leave comments, you need to log in
How to get data-attribute value when clicking on custom placemarks?
I iterate over the array and attach the data-id attribute to each created element, which I then want to get when clicked.
Initial data:
const fakeShops = [
{ name: "name1", cord: [55.751574, 37.573856] , id:1 },
{ name: "name2", cord: [56.751574, 38.573856] , id:2 },
{ name: "name3", cord: [57.751574, 39.573856] , id:3 },
{ name: "name4", cord: [58.751574, 40.573856] , id:4 },
];
fakeShops.forEach((shop,i)=>{
let content = ymaps?.templateLayoutFactory?.createClass(
`<div id="map_point" data-id=${fakeShops[i].id} class="text">
123
<div class="place_title">
<div>test</div>
<div>
<img src="https://cdn1.iconfinder.com/data/icons/interface-travel-and-environment/64/star-interface-256.png" style="width:12px;heigth:12px">
<span class="text_small">
4,5 Капучино от 190 р
</span>
</div>
</div>
</div>`
);
const myGeoObject = new ymaps.Placemark(
fakeShops[i].cord,
{
hintContent: fakeShops[i].name,
},
{
iconLayout: content,
iconShape: {
type: "Rectangle",
coordinates: [
[-95, -88],
[85, 78],
],
},
}
);
localMap.geoObjects.add(myGeoObject);
myGeoObject.events.add("click", function (e) {
const dom = document.getElementById("map_point");
console.log(dom.dataset.id); // тут выводит всегда 1 , проблемка
});
})
Answer the question
In order to leave comments, you need to log in
No data attributes are needed. Pass these id to properties (second constructor parameter) of placemarks, when clicked, get:
e.get('target').properties.get('имяСвойстваСодержащегоId')
shop.id
)?<div data-id="{{ properties.имяСвойстваСодержащегоId }}">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question