How to return real value from a column in jQuery datatables

Asked

Viewed 718 times

1

I am feeding my table by ajax and customizing the return. In the column status( 0 or 1 ) I used the "fnRender" to return a label of active or inactive according to the value, but I need to use this same validation to show the button Deactivate or Activate and when I get to this step "fnRender" overwritten the actual value of status for "<span class='label label-success'>Ativo</span>"( or Inactive ) and my validation for "mData":"id" that is if(parseInt(oObj.aData['status']) == 1) It always ends up in Isis.

Possible solution

if(oObj.aData['status'] == "<span class='label label-success'>Ativo</span>")

The problem is that I didn’t want to perform this validation by string, it seems somehow that this isn’t right.

Javascript:

var tabela = $('#dynamic-table');

    tabela.dataTable( {
        "aaSorting": [[ 0, "asc" ]],
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "minhaURL",
        "aoColumns": [{
            "mData":"nome"
          },
          {
            "mData":"status",
            "bSortable": false,
            "fnRender": function (oObj)                              
            {
                var status;
                if(parseInt(oObj.aData['status']) == 1){
                    status = "<span class='label label-success'>Ativo</span>"; 
                }else{
                    status = "<span class='label label-danger'>Inativo</span>";
                }
                return status;
            }
          },
          { 
            "mData":"id",    
            "bSearchable": false,
            "bSortable": false,
            "fnRender": function (oObj)                              
            {
                var btAtivaDesativa;
                btEditar = "<a href='" + oObj.aData['id'] + "' class='btn btn-primary'>Editar</a>";
                if(parseInt(oObj.aData['status']) == 1){
                    btAtivaDesativa = "<a href='" + oObj.aData['id'] + "' class='btn btn-danger'>Desativar</a>"; 
                }else{
                    btAtivaDesativa = "<a href='" + oObj.aData['id'] + "' class='btn btn-success'>Ativar</a>";
                }
                return btEditar + " " + btAtivaDesativa;
            }
           }
        ] 
    } );

1 answer

1


Link to the reply on SOEN

Basically the correct is to use the "mRender" not changing the real value in the variable when accessed by var full:

{
  "mData": 1,
  "bSortable": false,
  "mRender": function(data, type, full) {
     if (full['status'] == '1') {
       return '<a class="btn btn-info btn-sm" >Edit</a>';
     }         
     return '';
   }
}
  • 1

    explains right Ale! rss kkkk

Browser other questions tagged

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