I have a basic CRUD situation, where in the form, when I send the data, it inserts the mask normally, and when sending it to my local API, I format it and leave it in numeric format. But how am I going to apply the mask again on the item being displayed in a grid?
in my form, is like this
and on the grid, it displays like this
now, I need to apply the mask again, but on the grid that is showing. How to make?
to show the items on the grid, I am doing this via Javascript:
JavaScript
x
28
28
1
const exibirEmpresas = (u) => {
2
Array.from(u).forEach((lista) => {
3
dadosEmpresa += `
4
<tr>
5
<td class="idEmp" id="idEmp">${lista.idEmpresa}</td>
6
<td class="nomeEmp">${lista.nomeEmpresa}</td>
7
<td class="emailCad">${lista.email}</td>
8
<td class="cnpjCad" id="cnpjList">${lista.cnpj}</td>
9
<td class="dataCadastroCad">${lista.dataCadastro}</td>
10
<td class="dataAtualizacaoCad">${lista.dataAtualizacao}</td>
11
<td>
12
<button id="atualiza-empresa" onclick="editItem(${lista.idEmpresa})">Editar</button>
13
</td>
14
<td>
15
<button class="deletebtn" onclick="removeItem(${lista.idEmpresa})">Excluir</button>
16
</td>
17
</tr>
18
`;
19
});
20
listaEmpresa.innerHTML = dadosEmpresa;
21
};
22
23
// GET
24
25
fetch(urlAPI)
26
.then((s) => s.json())
27
.then((dados) => exibirEmpresas(dados));
28
Advertisement
Answer
I understand that you are essentially looking for a way to turn a 14-digit string like “19879847984784” to “19.879.847/9847-84”.
You can add this JavaScript code to your script. The HTML is just an example with hard coded values.
JavaScript
1
9
1
function formatCnpj() {
2
for (let td of document.querySelectorAll(".cnpjCad")) {
3
td.textContent = td.textContent
4
.replace(/D/g, "")
5
.replace(/(..)(...)(...)(....)/, "$1.$2.$3/$4-");
6
}
7
}
8
9
formatCnpj();
JavaScript
1
2
1
table { border-collapse: collapse }
2
td, th { border: 1px solid }
JavaScript
1
30
30
1
<table>
2
<tr>
3
<td class="idEmp" id="idEmp">28</td>
4
<td class="nomeEmp">John Larkin</td>
5
<td class="emailCad">john.larkin@x.com</td>
6
<td class="cnpjCad" id="cnpjList">19961423596110</td>
7
<td class="dataCadastroCad">2000-09-09</td>
8
<td class="dataAtualizacaoCad">2020-09-09</td>
9
<td>
10
<button id="atualiza-empresa" onclick="editItem(${lista.idEmpresa})">Editar</button>
11
</td>
12
<td>
13
<button class="deletebtn" onclick="removeItem(${lista.idEmpresa})">Excluir</button>
14
</td>
15
</tr>
16
<tr>
17
<td class="idEmp" id="idEmp">12</td>
18
<td class="nomeEmp">Helene Park</td>
19
<td class="emailCad">helene.park@n.com</td>
20
<td class="cnpjCad" id="cnpjList">19879847984784</td>
21
<td class="dataCadastroCad">2000-01-01</td>
22
<td class="dataAtualizacaoCad">2020-01-01</td>
23
<td>
24
<button id="atualiza-empresa" onclick="editItem(${lista.idEmpresa})">Editar</button>
25
</td>
26
<td>
27
<button class="deletebtn" onclick="removeItem(${lista.idEmpresa})">Excluir</button>
28
</td>
29
</tr>
30
</table>