How to delete the null string from the td tag?

Asked

Viewed 46 times

-1

How to remove "null" from tag ?

I tried to use replace, but error occurs due to variable being null:
Example:

_st_versao_ofertada.replace("null","");  //Ná pode, ocorre erro. Como fazer ? <br>

Error:

TypeError: Cannot read property 'replace' of null

Excerpt from the code:

<table id="tbl_1">
<thead>
<tr>
<td>Versão atual</td><td>Versão Ofertada</td><td>Versão Vendida</td>
</tr>
</thead>
<tbody>
<tr>
<td data-st_versao_atual="Plus">Plus</td><td data-st_versao_ofertada=""></td><td data-st_versao_vendida ='" + _st_versao_vendida + "'>King</td>
</tr>
<tr>
<td data-st_versao_atual="King">King</td><td data-st_versao_ofertada="King"></td><td  data-st_versao_vendida ="King">King</td>
</tr>
<tr>
<td data-st_versao_atual="King">King</td><td>King</td><td  data-st_versao_vendida ="Plus">Plus</td>
</tr>
</tbody>
</table>

var table_1 = $("#tbl_1 > tbody");

var _st_versao_atual = "";
var _st_versao_ofertada = "";
var _st_versao_vendida = "";
var html = "";

table_1.find("tr").each(function () {                
_st_versao_atual = $(this).closest('tr').find('td[data-st_versao_atual]').data('st_versao_atual');
_st_versao_ofertada = $(this).closest('tr').find('td[data-t_versao_ofertada]').data('st_versao_ofertada');
_st_versao_vendida = $(this).closest('tr').find('td[data-st_versao_vendida]').data('st_versao_vendida');

html = html + "<tr>";
html = html + "<td>" + _st_versao_atual + "</td>";
html = html + "<td>" + _st_versao_ofertada + "</td>";
html = html + "<td>" + _st_versao_vendida + "</td>";
html = html + "</tr>";
});

$("#tbl_2 > tbody").html(html);

<table id="tbl_2">
<thead>
<tr>
<td>Versão atual</td><td>Versão Ofertada</td><td>Versão Vendida</td>
</tr>
</thead>
<tbody>
<tr>
<td>Plus</td><td>null</td><td>King</td>                     // <-- /Retirar ess Null
</tr>
<tr>
<td>King</td><td>null</td><td>King</td>                      // <-- /Retirar ess Null
</tr>
<tr>
<td>King</td><td>King</td><td>Plus</td>
</tr>
</tbody>
</table>
  • <td data-st_versao_atual="King">King</td data-st_versao_ofertada="King"> end tags should not have attributes

  • 1

    it is not easier when generating the table validate if the value is null than to do a repalce after?

  • Good morning! @Augusto Vasques was an error when pasting the tags in the editor, I fixed the code to make it easier to view.

  • Good morning! @Ricardo Pontual if you analyze you will see that when loading the table the tag comes empty then passing the data from tbl_1 to tbl_2 the empty field gets the string null.

  • then, you cannot before Motar the table string validate for example if _st_versao_atual is null and switch to ""?

  • Why don’t you do something like if (_st_versao_atual == null) { escreva somente "<td></td>" } else { escreva o _st_versao_atual dentro do td }?

  • Yes @Ricardo Punctual can do if Else as hkotsubo suggested, but the table has 15 columns and I will have to do this check for all columns, it will get a little polluted, but I see that there is no alternative to do this with less code.

  • Whenever there is repeated code or something that is often done with little variation, this is a sign that you can create a function and call it in a loop :-)

  • doesn’t need a if to do this, just use an operator: _st_versao_atual = $(this).closest('tr').find('td[data-st_versao_atual]').data('st_versao_atual') || ""; clean, simple and ready

Show 4 more comments

1 answer

1


You can use the || '' at the end of each allocation, so when null will generate a value falsy and will put the '' in place.

_st_versao_atual = $(this).closest('tr').find('td[data-st_versao_atual]').data('st_versao_atual') || '';
_st_versao_ofertada = $(this).closest('tr').find('td[data-t_versao_ofertada]').data('st_versao_ofertada') || '';
_st_versao_vendida = $(this).closest('tr').find('td[data-st_versao_vendida]').data('st_versao_vendida') || '';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.