如何在jqgrid中显示外键关系数据?

时间:2011-02-25 18:30:15

标签: ruby-on-rails jqgrid

我知道如何在scaffold索引视图中访问外键属性。我可以使用点表示法简单地引用属性,例如 property.que.name 。鉴于以下模型:

class Priority < ActiveRecord::Base
  belongs_to :que
  ...
end

class Que < ActiveRecord::Base
  has_many :priorities
  ...
end

在索引视图中,我可以执行以下操作来获取名称值:

<td><%=h priority.que ? priority.que.name : "" %></td>

我如何在jqgrid中执行此操作?

我尝试了这个,但是jqgrid又回来了:

优先事项控制人:

@priorities = Priority.find(:all, :order => "position", :conditions => "multitenant_team_id = " + current_user.team.id.to_s ) do
  if params[:_search] == "true"
    id =~ "%#{params[:id]}%" if params[:id].present?
    issue_id =~ "%#{params[:issue_id]}%" if params[:issue_id].present?
    que =~ "%#{params[:que]}%" if params[:que].present?
    customer =~ "%#{params[:customer]}%" if params[:customer].present?
    title =~ "%#{params[:title]}%" if params[:title].present?
    reporting_source =~ "%#{params[:reporting_source]}%" if params[:reporting_source].present?
    priority =~ "%#{params[:priority]}%" if params[:priority].present?
    product =~ "%#{params[:product]}%" if params[:product].present?
    current_owner =~ "%#{params[:current_owner]}%" if params[:current_owner].present?
  end
  paginate :page => params[:page], :per_page => params[:rows]
  order_by "#{params[:sidx]} #{params[:sord]}"

end

if request.xhr?
end

respond_to do |format|
  format.html # index.html.erb
  format.json { render :json => @priorities.to_jqgrid_json(
    [:id, :issue_id, :que.name, :customer, :title, :reporting_source, 
     :priority, :product, :current_owner],
     params[:page], params[:rows], @priorities.total_entries)}
  format.xml  { render :xml => @priorities }
 end

索引视图:

<%= jqgrid("Priorities", "priorities", "/priorities",
[
  {:field => "id", :label => "ID", :width => 35, :resizable => false},
  {:field => "issue_id", :label => "Issue Id"},
  {:field => "que", :label => "Queue"},
  {:field => "customer", :label => "Customer"},
  {:field => "title", :label => "Title"},
  {:field => "reporting_source", :label => "Reporting Source"},
  {:field => "priority", :label => "Priority"},
  {:field => "product", :label => "Product"},
  {:field => "current_owner", :label => "Current Owner"}
],
  { :rows_per_page => 12, :height => 450 }
)%>

如果我指定que而不是que.name,我将数据返回到网格中,但Queue字段显示“#”符号,所以我怀疑.to_jqgrid_json调用不喜欢我的语法。

以前有人试过这个吗?我希望如此。

2 个答案:

答案 0 :(得分:1)

我解决了我的问题。我最终将我的查找更改为 find_by_sql ,因此我可以在问题表上执行左外连接。我认为有几个问题。我认为* to_jqgrid_json *存在null外键值的问题,我无法弄清楚如何以任何其他方式获取Que.name。我正在使用SQLServer,所以我不得不使用 isnull(ques.name,'')将null转换为空格。

所以我更改了我的发现如下:

@priorities = Priority.find_by_sql ["select priorities.id id, issue_id, isnull(ques.name,' ') queue_name, customer, title, reporting_source, priority, product, current_owner from priorities left outer join ques on priorities.que_id = ques.id where priorities.multitenant_team_id = ? order by issue_id", current_user.team.id.to_s]

这引入了另一个问题,即find_by_sql返回一个打破@ prioritiess.total_entries调用的数组。所以我不得不用 array.count 替换它。

format.json { render :json => @priorities.to_jqgrid_json(
    [:id, :issue_id, :queue_name, :customer, :title, :reporting_source, :priority, :product, :current_owner], 
     params[:page], params[:rows], @priorities.count)}

我的网格看起来很棒!

修改

我的网格看起来不错,但它没有分页或排序。回到绘图板。 :(

答案 1 :(得分:1)

好的,我想这次我已经修好了。

@priorities = Priority.find(:all,
                        :select => "priorities.id, priorities.issue_id, 
                                        priorities.customer, priorities.title, 
                                        priorities.reporting_source, priorities.priority,
                                        priorities.product, priorities.current_owner,
                                        priorities.position,
                                        isnull(ques.name,' ') queue_name",
                            :joins => "LEFT OUTER JOIN ques ON ques.id = priorities.que_id",
                            :order => "priorities.position", 
                            :conditions => "priorities.multitenant_team_id = " + current_user.team.id.to_s ) do

我知道我可以指定这样的连接。这使结果集保持2dc_jqgrid插件所喜欢的格式。现在排序,分页和搜索所有工作。现在我的网格看起来很好并且实际上有效。