2

I am using jquery-datatables. I want that if the value is numeric its alignment should be right and if it is string then the alignment should be left. Is it possible with datatables or is there any method in it?

  <% @tasks.each do |task| -%> 
  <tr>
    <% col_order.each do |key| %>
      <td>
      <% value = task[key.column_name.split(" as ")[1]] || task[key.column_name.split(".")[1]] -%>
      <% if key["drilldown_reportid"].present? %>
        <%= link_to value, project_report_path(@current_project,key["drilldown_reportid"], :column=>"#{key.column_name}", :value=>"#{value}") %>
      <% else %>
        <%= value -%> </td>
      <% end %>
    <% end %>
  </tr>

  <% end -%>
madth3
  • 7,275
  • 12
  • 50
  • 74
Sneha Kachroo
  • 583
  • 2
  • 6
  • 12

2 Answers2

5

I solved this problems by changing the client side code (not changing any server-side code).

in file css/jquery.dataTables.css, I added a class

.alignRight { text-align: right; }

And in my javascript file for processing the data I changed "aoColumnDefs"...

///...
"aoColumnDefs" : [
//...col 1 -6
// col_07

{
    "aTargets" : [ 7 ],
    "fnRender" : function(oObj) {
            return Math.round(oObj.aData["endingDepth"] * 100) / 100;
    },
    "sTitle" : "Ending Depth [m]",
    "sWidth" : "5em",
    "sClass" : "alignRight"
},
//... more columns
knb
  • 9,138
  • 4
  • 58
  • 85
  • 2
    this is a little bit nasty because you are modifying the css bundled with datatables. Good practice is to use your own css file do to this. – ZenCodr Jul 11 '14 at 04:08
0
Another solution: add the following line and no need the update the CSS

///...
"aoColumnDefs" : [
//...col 1 -6
// col_07

{
    "aTargets" : [ 7 ],
    "fnRender" : function(oObj) {
            return Math.round(oObj.aData["endingDepth"] * 100) / 100;
    },
    "sTitle" : "Ending Depth [m]",
    "sWidth" : "5em",
    "sClass" : "right"
},
//... more columns
Jag
  • 26
  • 4