Adding different bootstrap badge for one column in datatable

Adding different bootstrap badge for one column in datatable

davide.mancosdavide.mancos Posts: 8Questions: 2Answers: 0

Hi all,
I made a call ajax to my server to obtain data from MySQL and datatables working fine.
But I need to add different badges for different severities in one column of my datatables.
In the column definition my code is:
....
{ data: 'severity', name: 'severity',
render: function(data, type, row){
console.log('Content of data is : '+data);
sev='';
switch (data){
case 5:
sev = '<span class="badge badge-danger badge-pill">'+data+'</span>';
break;
case 4:
sev = '<span class="badge badge-warning badge-pill">'+data+'</span>';
break;
case 3:
sev = '<span class="badge bg-yellow badge-pill">'+data+'</span>';
break;
case 2:
sev = '<span class="badge badge-primary badge-pill">'+data+'</span>';
break;
case 1:
sev = '<span class="badge badge-secondary badge-pill">'+data+'</span>';
break;
case 0:
sev = '<span class="badge badge-secondary badge-pill">'+data+'</span>';
break;
}
return sev;
console.log('Content of sev is : '+data);
},
{ data: 'count', name: 'count' },
....

The console of chrome broeswer returns value for "console.log('Content of data is : '+data);" but nothing for "console.log('Content of sev is : '+data);"
so, the severity column is now empty!!

Someone can help me to find the correct code to show the severity value whith badge in my severity column?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,170Questions: 26Answers: 4,922
    edited April 2020 Answer ✓

    but nothing for "console.log('Content of sev is : '+data);"

    return sev;
    console.log('Content of sev is : '+data);
    

    You need to place the console.log statement before the return. Also you might be wanting this instead: console.log('Content of sev is : '+sev);.

    Without being able to see your data its hard to provide suggestions. Maybe you data is a string instead of numeric. You might need something like switch ( parseInt(data) ){ to convert to integer.

    If this doesn't help please provide a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • davide.mancosdavide.mancos Posts: 8Questions: 2Answers: 0

    Ok...I put console.log('Content of sev is : '+sev); before the return.
    but nothing happen.
    Severity data is devlared in the MYSQL like smallint.

    now the datatable doesn't work!!! and no message in browser conole.

  • davide.mancosdavide.mancos Posts: 8Questions: 2Answers: 0

    Ok. using return sev for each case it works!
    thank's a lot
    Best regards

  • kthorngrenkthorngren Posts: 21,170Questions: 26Answers: 4,922
    edited April 2020 Answer ✓

    Your columns.render code works here:
    http://live.datatables.net/togutuni/1/edit

    If this doesn't help then we will need to see a link to your page or a test case to offer suggestions. I moved and changed your console.log statement as described above.

    Kevin

  • davide.mancosdavide.mancos Posts: 8Questions: 2Answers: 0

    ok thank's a lot.

This discussion has been closed.