I am using Jquery DataTables to improve the table view. The insert or update operations occur without any problem, but when deleting it does not update the table after the refresh.
For example: I have two records and delete only one. In the database it is only with one record but in the listing (table) it shows both, as if nothing happened. Only after a refresh that updates.
{:phoenix_live_view, "~> 0.13.1", override: true},
{:phoenix, "~> 1.5.0-rc.0", override: true},
// app.js
jQuery(document).ready(function($){
function initDataTable() {
$("#mytable").DataTable()
}
$(document).on("phx:update", initDataTable)
})
index.html.leex
<div class="col-12" phx-update="ignore">
<table id="mytable" class="table table-sm table-dashboard no-wrap mb-0 fs--1 w-100">
<thead class="bg-200">
<tr>
<th class="sort pr-1 align-middle">ID</th>
<th class="sort pr-1 align-middle">Inserted at:</th>
<th class="sort pr-1 align-middle">Updated at:</th>
<th class="no-sort pr-1 align-middle data-table-row-action">Ações:</th>
</tr>
</thead>
<tbody>
<%= for empresa <- @empresas do %>
<tr id="empresa-<%= empresa.id %>" class="btn-reveal-trigger">
<td class="align-middle"><%= empresa.id %></td>
<td class="align-middle"><%= empresa.inserted_at %></td>
<td class="align-middle"><%= empresa.updated_at %></td>
<td class="align-middle white-space-nowrap">
<div class="dropdown text-sans-serif">
<button class="btn btn-link text-600 btn-sm dropdown-toggle btn-reveal mr-3" type="button" id="dropdown0" data-toggle="dropdown" data-boundary="html" aria-haspopup="true" aria-expanded="false"><span class="fas fa-ellipsis-h fs--1"></span></button>
<div class="dropdown-menu dropdown-menu-right border py-0" aria-labelledby="dropdown0">
<div class="bg-white py-2">
<%= live_patch "Show", to: Routes.admin_empresa_show_path(@socket, :show, empresa), class: "dropdown-item" %>
<%= live_patch "Edit", to: Routes.admin_empresa_new_path(@socket, :edit, empresa), class: "dropdown-item text-warning" %>
<%= link "Delete", to: "#", phx_click: "delete", phx_value_id: empresa.id, data: [confirm: "Are you sure?"], class: "dropdown-item text-danger" %>
</div>
</div>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
index.ex
def handle_event("delete", %{"id" => id}, socket) do
empresa = Empresa.get_empresa!(id)
{:ok, _} = Empresa.soft_delete_empresa(empresa)
{:noreply,
socket
|> put_flash(:info, "Registro excluĂdo com sucesso!")
|> assign(:empresas, Empresa.list_empresas())
}
end