Giving right align to numeric data in datatables

你。 提交于 2019-12-10 23:28:35

问题


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 -%>

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/12527707/giving-right-align-to-numeric-data-in-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!