2

I have been working with Knockout and FontAwesome for a couple of days and I'm trying to display a FontAwesome icon using it's Unicode.

I have a list of items called alerts which contains different informations about an alert and one icon for it.

if (alerts.Category == 0)
    alerts['Category'] = '';
else if (element.Category == 1)
    alerts['Category'] = '';
else if (alerts.Category == 2)
    alerts['Category'] = '';
else if (alerts.Category == 3)
    alerts['Category'] = '';
else if (alerts.Category == 4)
    alerts['Category'] = '';

The thing is that when I'm trying to get them into the HTML page like:

<table class="table" id="alertstable">
 <tbody data-bind="foreach: alerts">
   <tr style: { backgroundColor: Color, color: TextColor }">
    <td>
      <button type="button" class="btn btn-default"
        data-bind="click:$root.GetAlertClick, text:VehicleShortName">
      22
      </button>
    </td>
    <td>
      <i style="font-family: 'FontAwesome', 'Helvetica'; font-size: 20px;" 
          data-bind="image:Category"></i>
    </td>
   </tr>
 </tbody>
</table>

It displays all the VehicleShortNames but it doesn't display the icon for each one of them. It is possible to display the icon in the table this way?

JotaBe
  • 38,030
  • 8
  • 98
  • 117
Alex Mihai
  • 172
  • 1
  • 10

2 Answers2

1

On request from OP, heres the answer... you should be using class="fa fa-something" as your class name ;-)

An0nC0d3r
  • 1,275
  • 13
  • 33
0

When you want to show an icon from a font icon, you just need to show the character text, and use the font icon to style it.

In your case, you're styling the text, but are not showing the text. You must change the binding like this:

<i style="font-family: 'FontAwesome', 'Helvetica'; font-size: 20px;" 
      data-bind="text:Category"></i>

If you still can't see it, try using the html binding, instead of text, like this:

<i style="font-family: 'FontAwesome', 'Helvetica'; font-size: 20px;" 
      data-bind="html"></i>

And finally, if you still have some problems, please check this SO answer which explains other possible reasons for which your icon isn't shown.

NOTE: your HTML have several defects, for example, the text shown in a <button> is the buttons value attribute. This syntax <tr style: { backgroundColor: Color, color: TextColor }"> is completely worng. And also your viewmodel doesn't look right

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117