locked
Only Show HyperLink If Value >= 1 RRS feed

  • Question

  • User1402230106 posted

    I am attempting to check if the value of data is != 0 and if that evaluates to true then display a hyperlink.  If it evalueates to false then show 0

    I have the below code but this code is Always showing the hyperlink regardless of the value of data.  How do I need to change this syntax so that the hyperlink will only be displayed if the value of data is >= 1?

    var data = [
        { "Supervisor": "Supervisor1", "Employee": "Employee1", "NumCustomers": "3", "NumSales": "0" }
    ];
    
    $('#example').dataTable({
       "data": data,
       "columns": [
          { "data": "supervisor" }, 
          { "data": "employee" },
          { "data": "numcustomers" },
          { 
             "data": "numsales",
             "render": function(data, type, row, meta){
                if (data != 0) {
                  if(type === 'display'){
                    data = '<a href="' + data + '?param='+row.supervisor+'">' + data + '</a>';
                  }
                
                  return data;
                }
             }
          } 
       ]
    });
    });

    Thursday, August 1, 2019 11:59 AM

All replies

  • User288213138 posted

    Hi IndigoMontoya,

    I modified your code according to your description.

    if the value of data is >= 1,then showing the hyperlink,Otherwise returns data.

    The code:

    <script>
            $(document).ready(function () {
                var data = [
                    { "Supervisor": "Supervisor1", "Employee": "Employee1", "NumCustomers": "3", "NumSales": "0" },
                    { "Supervisor": "Supervisor1", "Employee": "Employee1", "NumCustomers": "3", "NumSales": "1" }
                ];
                $('#example').dataTable(
                    {
                        data: data,
                        columns: [
                            { "data": "Supervisor" },
                            { "data": "Employee", },
                            { "data": "NumCustomers" },
                            {
                                "data": "NumSales",
                                "render": function (data, type, full, meta) {
                                    if (data >= 1) {
                                        return '<a href="' + data + '">' + data + '</a>';
                                    }
                                    else {
                                        return data;
                                    }                               
                                }
                            },
                        ]
                    }
                );
            })
        </script>
    <div>
                <table id="example""></table>
            </div>
    

    The result:

    Best regards,

    Sam

    Friday, August 2, 2019 7:45 AM