Search code examples
symfonybootstrap-4x-editable

Symfony 4 x-editable - update


My mission is to use the x-editable library for bootstrap in Symfony 4. After clicking on the field in the table appears "input" and the value from the field inside. I have a problem to make an update to the database. In the code below I am doing an update to the database with a fixed value of "Koralik". I would like the update with the value from "input" to go to the base.

<table id="tab1" class="table table-bordered table-striped table-hover"> 
<thead style="color:blue;">
<tr><th>Imię</th><th>Nazwisko</th></tr>
</thead>

<tbody id="myTable">
{%for kierowca_one in kierowca%}

<tr class="text-success">
<td>{{kierowca_one.Imie}}</td>
<td><span class="myElement" data-type="text" data-pk=" 
{{kierowca_one.id}}" data-url="{{path('app_update', 
{id:kierowca_one.id})}}">{{kierowca_one.Nazwisko}}</span> </td>
</tr>

{%endfor%}
</tboody>
</table>

$('.myElement').editable({ 
 type: "POST",
 emptytext: 'Brak',
 showbuttons: false,
 mode: 'inline',
 //dataType: 'json',
 validate: function(value){
 if($.trim(value) == '')
 {
 return 'This field is required';
 }
 }
}); 

   /**
* @Route("/update/{id}", name="app_update", methods={"GET"})
* @IsGranted("ROLE_USER")
*/
public function updateMethod(EntityManagerInterface $em, $id)
{

        $repository=$em->getRepository(Kierowcy::class);
        $kierowca=$repository->find($id);

        $kierowca->setNazwisko('Kowalik');
        $em->persist($kierowca);
        $em->flush();

        return new Response();

}

Solution

  • here is my approach.

    js file

     $('#s').editable({
        selector: '.js-editable',
        url: urlUpdateTopic,
        params:function(params){
            params.pk = $(this).closest('tr').data('id');
            return params;
        },
        success: function(value, response) {
            if(response){
                $(this).html(value);
            }
        },
        error: function(response) {
            if(response.status === 500) {
                console.log('Service unavailable. Please try later.');
            } else {
                console.log(response.responseText);
            }
        },
        type: 'text',
        pk: 1,
        mode: 'inline',
        showbuttons: false,
        toggle: 'dblclick',
        onblur: "submit",
        clear: false,
        emptytext: ''
    });
    

    here is controller

    public function updateTopicAction(Request $request)
    {
        $id = $request->request->get('pk');
        $value = $request->request->get('value');
    }
    

    also you can see a good example here Submitting data via Ajax in X-Editable