I need a button that will generate a report in the background, for this I need to use ajax. I implemented a button template and a method to be called on this button, the button is visible in all rows, but it is called only on the first row and many at once (by clicking on the first row, the method is called as many times as there are rows). And it is necessary that it was called for each person separately. Those. by clicking on line 5, the button should fire and pass user_id = 5. Can you please tell me how to add a button to each row correctly and pass data to the controller?
easy_admin:
entities:
User:
class: App\Entities\User
label: User list
list:
fields:
- id
- { property: name, type: text }
- { property: generate, type: button, template: 'generate_report.html.twig' } // Here I need to somehow convey user_id
Template
{% block generate_report %}
<input type="button" value="Go" id="generate">
<script>
$(function () {
$('#generate').on('click', function (e) {
let userId = ? // user_id should be here
$.ajax({
url: "{{ path('generate.report') }}",
type: "POST",
data: {
userId: userId
},
success: function () {
console.log("success")
},
error: function () {
console.log("error")
}
});
});
});
</script>
{% endblock %}
Controller
/**
* @Route(methods={"POST"}, name="generate.report")
*/
public function generateReport(): Response
{
return $this->json(["success"]);
}