Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Font Awesome (or other font icons) instead of the jQuery sprites in Primefaces DataTable?

I'm using PrimeFaces to generate a datatable. The default sorting icons are chevrons from the JQuery library but as they are not Vector they look ugly. I wan't to replace them using some font like font awesome but I have no clue how to do this. The below CSS handles I can use to change the image to different sprites etc.

If I could somehow change the class primefaces adds from ui-icon-triangle-1-n to af fa-sort-alpha-desc for example I would already be helped.

.ui-state-default .ui-icon{
        background-image: url("../Assets/images/jquery-ui/ui-    icons_0072b6_256x240.png");
    }
    .ui-datatable .ui-icon-carat-2-n-s {
    background-position: -160px 0 !important;
}

.ui-datatable .ui-icon-triangle-1-n{
    background-position: 0 -48px !important;
}

.ui-datatable .ui-icon-triangle-1-s{
    background-position: -64px -48px !important;
}
like image 778
irongollem Avatar asked Jan 18 '16 13:01

irongollem


People also ask

How do I use all Font Awesome icons?

You can place Font Awesome icons just about anywhere using the CSS Prefix fa and the icon's name. Font Awesome is designed to be used with inline elements (we like the <i> tag for brevity, but using a <span> is more semantically correct). icon If you change the font-size of the icon's container, the icon gets bigger.


2 Answers

I ended up with this bit of CSS for the DataTable:

.ui-datatable .ui-sortable-column-icon.ui-icon {
    background-image: none; text-indent: 0; margin: 0 0 0 .5em;
}
.ui-paginator > span:before, .ui-sortable-column-icon:before {
    font-family: FontAwesome; color: #fff;
}
.ui-paginator > span > span, .ui-paginator a span { display: none; }
.ui-paginator-first:before { content: '\f049'; }
.ui-paginator-prev:before { content: '\f048'; }
.ui-paginator-next:before { content: '\f051'; }
.ui-paginator-last:before { content: '\f050'; }
.ui-icon-carat-2-n-s:before { content: '\f0dc'; }
.ui-icon-carat-2-n-s.ui-icon-triangle-1-n:before { content: '\f0de'; }
.ui-icon-carat-2-n-s.ui-icon-triangle-1-s:before { content: '\f0dd'; }
.ui-paginator .ui-state-disabled { opacity: .25; }

All the unicodes can be found here: http://fontawesome.io/cheatsheet/

Font Awesome offers some alternative icons:

  •  fa-sort-alpha-asc \f15d
  •  fa-sort-alpha-desc \f15e
  •  fa-sort-amount-asc \f160
  •  fa-sort-amount-desc \f161
  •  fa-sort-numeric-asc \f162
  •  fa-sort-numeric-desc \f163

Update

After doing some CSS overriding for my theme and taking Kukeltje's comment into consideration I decided I would be a good idea to add a JSF ResourceHandler to the theme I'm working on.

Simply add the dependency to your project and add the resource handler

<application>
    <resource-handler>org.jepsar.primefaces.theme.jepsar.FontAwesomeResourceHandler</resource-handler>
</application>

The handler will detect PrimeFaces themes and will patch the CSS. It removes the jQuery UI icons and adds Font Awesome rules (which includes a separate icon mapping SCSS).

like image 132
Jasper de Vries Avatar answered Sep 26 '22 08:09

Jasper de Vries


here is an example:

.ui-paginator-first.ui-state-default.ui-corner-all:before{
    content: "\f049";
    font-family: FontAwesome;
    color: white;
}

you can find the codes you cann add for content instead of "\f049" in the font-awesome website http://fortawesome.github.io/Font-Awesome/icon/pencil/

the :before is important behind the css class

like image 36
Lule Avatar answered Sep 24 '22 08:09

Lule