drqwbh2150 2019-07-31 08:38
浏览 36

symfony列表中内联编辑元素的问题

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?