My problem: I have problem with editing inline items on list. I edit elements by switch button and saving it to database. If I set button disabled I got message "{"data":{},"status":"Success"}", but I don't see any data here ...
Normal action like edit, create, delete, show item works, but only this edit inline items have problems ...
It's my goal: If button is disabled I got boolean 0 (false), but if is checked I got boolean 1 (true).
I would be gratefull for any help.
My Controller to edit:
/**
* @Route("/{id}/editaction", name="pb_editacion", methods={"GET","POST"})
* @return JsonResponse
*/
public function editAction(Request $request, $id): Response
{
if( $request->isXmlHttpRequest())
{
$pB = $this->getDoctrine()->getRepository(PB::class)->find($id);
$form = $this->createForm(PBType::class, $pB);
if ($form->isSubmitted())
{
$form->handleRequest($request);
if (false === $form->isValid()) {
return new JsonResponse([
'status' => 'error',
'errors' => $form->getErrors(),
], JsonResponse::HTTP_BAD_REQUEST);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return $this->redirectToRoute('p_b_index', [
'id' => $pB->getId(),
]);
}
return new JsonResponse([
'data' => $pB,
'status' => 'Success',
], JsonResponse::HTTP_CREATED);
}
}
My script to request:
$(function() {
const toggles = document.querySelectorAll('.checkbox-switch input[type="checkbox"]');
for (i = 0; i < toggles.length; i++) {
if ((toggles[i].value) === "1" )
{
toggles[i].checked = true;
}
else
{
toggles[i].checked = false;
}
toggles[i].addEventListener('change', function () {
const toggle = this;
const newValue = this.checked;
const oldValue = !newValue;
const toggleUrl = "{{ path('pb_editacion', { id: '__ID__' } ,{ view:'list' } )|raw }}";
const newURL = toggleUrl.replace('__ID__', this.closest('tr').dataset.id);
let toggleRequest = $.ajax({
type: "GET",
url: newURL,
data: {},
dataType: 'json',
success: function (data) {
console.log(data);
}
});
toggleRequest.done(function(data) {});
toggleRequest.fail(function() {
// in case of error, restore the original value and disable the toggle
toggle.checked = oldValue;
toggle.disabled = true;
toggle.closest('.checkbox-switch').classList.add('disabled');
});
});
}
});
And it's table on twig:
<table class="table datagrid">
<thead class="small-words-header">
<tr>
<th scope="col">Id</th>
<th scope="col">{{ 'form.name_of_project'|trans }}</th>
<th scope="col">{{ 'form.internal_project'|trans }}</th>
<th scope="col">{{ 'form.valuation'|trans }}</th>
<th scope="col">{{ 'form.valuation_date'|trans }}</th>
<th scope="col">{{ 'form.sold_amount'|trans }}</th>
<th scope="col">{{ 'form.beginning_date'|trans }}</th>
<th scope="col">{{ 'form.date_of_close'|trans }}</th>
<th scope="col">{{ 'form.date_of_last_action'|trans }}</th>
<th scope="col">{{ 'form.expect_date_of_close_project'|trans }}</th>
<th scope="col">{{ 'form.valuation_euro'|trans }}</th>
<th scope="col">{{ 'form.valuation_pln'|trans }}</th>
<th scope="col">{{ 'form.sold_time'|trans }}</th>
<th scope="col">{{ 'form.spent_time'|trans }}</th>
<th scope="col">{{ 'form.spent_time_in_percent'|trans }}</th>
<th scope="col">{{ 'form.finall_cost'|trans }}</th>
<th scope="col">{{ 'form.profit'|trans }}</th>
<th scope="col">{{ 'form.potential_profit'|trans }}</th>
<th scope="col">{{ 'form.project_id'|trans }}</th>
<th scope="col">{{ 'form.client_of_project'|trans }}</th>
<th scope="col">{{ 'form.project_manager'|trans }}</th>
<th scope="col">{{ 'form.valuer'|trans }}</th>
<th scope="col">{{ 'form.markup'|trans }}</th>
<th scope="col">{{ 'form.options.actions'|trans }}</th>
</tr>
</thead>
<tbody class="small-words">
{% for p_b in pt_bs %}
<tr data-id="{{ project_bilan.id }}">
<td>{{ p_b.id }}</td>
<td>{{ p_b.name }}</td>
<td>
<div class="checkbox-switch">
<label>
<input type="checkbox" id="ip{{ p_b.id }}" value="{{ p_b.internalProject }}">
<span></span>
</label>
{# <p>{{ p_b.internalProject ? 'form.choices.yes'|trans : 'form.choices.no'|trans }}</p>#}
</div>
</td>
<td>
<div class="checkbox-switch text-center">
<label>
<input type="checkbox" id="v{{ p_b.id }}" value="{{ p_b.valuation }}">
<span></span>
</label>
{# <p>{{ p_b.valuation ? 'form.choices.yes'|trans : 'form.choices.no'|trans }}</p>#}
</div>
</td>
<td>{{ p_b.valuationAt ? p_b.valuationAt|date('Y-m-d') : '' }}</td>
<td>{{ p_b.sold }}</td>
<td>{{ p_b.startAt ? p_b.startAt|date('Y-m-d') : '' }}</td>
<td>{{ p_b.closedAt ? p_b.closedAt|date('Y-m-d') : '' }}</td>
<td>{{ p_b.lastActionAt ? p_b.lastActionAt|date('Y-m-d') : '' }}</td>
<td>{{ p_b.expectClosedAt ? p_b.expectClosedAt|date('Y-m-d') : '' }}</td>
<td>{{ p_b.valuationEuro }}</td>
<td>{{ p_b.valuationPln }}</td>
<td>{{ p_b.soldTime }}</td>
<td>{{ p_b.spentTime }}</td>
<td>{{ p_b.spendTimePercent }}</td>
<td>{{ p_b.cost }}</td>
<td>{{ p_b.profit }}</td>
<td>{{ p_b.potentialProfit }}</td>
<td>{{ p_b.projectID }}</td>
<td>{{ p_b.client }}</td>
<td>{{ p_b.projectManager }}</td>
<td>{{ p_b.valuer }}</td>
<td bgcolor="{{ p_b.markup.color }}">{{ p_b.markup }}</td>
</tr>
{% else %}
<tr>
<td colspan="24">{{ 'site.no_records_found'|trans }}</td>
</tr>
{% endfor %}
</tbody>
</table>