【问题标题】:Giving right align to numeric data in datatables对数据表中的数字数据进行右对齐
【发布时间】:2014-07-11 22:53:18
【问题描述】:

我正在使用 jquery 数据表。我希望如果值是数字,它的对齐应该是正确的,如果它是字符串,那么对齐应该是左对齐。是否可以使用数据表或其中有任何方法?

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

【问题讨论】:

    标签: ruby-on-rails-3 jquery-datatables


    【解决方案1】:

    我通过更改客户端代码(不更改任何服务器端代码)解决了这个问题。

    在文件css/jquery.dataTables.css, 我添加了一个类

    .alignRight { text-align: right; }
    

    在用于处理数据的 javascript 文件中,我更改了“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
    

    【讨论】:

    • 这有点讨厌,因为您正在修改与数据表捆绑的 css。好的做法是使用您自己的 css 文件来执行此操作。
    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2010-11-26
      • 2015-05-05
      • 2011-03-31
      • 2015-05-14
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      相关资源
      最近更新 更多