指定每个表的不同样式

时间:2011-10-28 21:27:11

标签: javascript ruby-on-rails css

我正在根据数据库中的某些字段生成表格,我需要为每个表格指定不同的样式。如何为每个表指定不同的样式。

这是我的代码

<table width="100%">
  <% @content_data['personnel_cv_data_types'].each do |personnel_cv_data_type| %>
 <tr width="100%">
 <td>
  <fieldset>
  <legend>
    <table style="font-size:13px;"width="100%" class="personnel_cvdata_view_table_first_row">
      <tr>
        <td>
          <%= @content_data['lable_title_'+personnel_cv_data_type] %>
        </td>
        <td>
          <a onClick="add_new_entry('<%= personnel_cv_data_type %>')">Add&nbsp;<%= @content_data['lable_add_'+personnel_cv_data_type] %></a>
        </td>
      </tr>
    </table>
  </legend>

此处每个表采用与 personnel_cvdata_view_table_first_row 相同的样式,如何生成具有不同样式类的表格,例如

  class="personnel_cvdata_view_table_first_row_1"
  class=""ersonnel_cvdata_view_table_first_row_2

等等,我可以为每个表写单独的样式

3 个答案:

答案 0 :(得分:3)

这样的东西似乎符合你的需求:

<% @content_data['personnel_cv_data_types'].each_with_index do |personnel_cv_data_type, index| %>
...
<table style="font-size:13px;"width="100%" class="personnel_cvdata_view_table_first_row_<%= index + 1 %>">

答案 1 :(得分:2)

而不是

<% @content_data['personnel_cv_data_types'].each do |personnel_cv_data_type| %>

使用

<% @content_data['personnel_cv_data_types'].each.with_index do |personnel_cv_data_type, counter| %>

然后你可以像这样创建你的表

<table style="font-size:13px;"width="100%" class="personnel_cvdata_view_table_first_row_<%= counter %>">

答案 2 :(得分:1)

您可以根据某些唯一标识符添加一个类,如果它是唯一的,甚至可能是字符串personnel_cv_data_type本身。

<table style="font-size:13px;"width="100%" class="#{personnel_cv_data_type}">
  <tr>
    <td>
      <%= @content_data['lable_title_'+personnel_cv_data_type] %>
    </td>
    <td>
      <a onClick="add_new_entry('<%= personnel_cv_data_type %>')">Add&nbsp;<%= @content_data['lable_add_'+personnel_cv_data_type] %></a>
    </td>
  </tr>
</table>

我不知道你想要对每种风格做出什么不同,但如果列表是确定的,你可以为上述每种类型设置样式。