您是否正在尝试实现主细节/一对多编辑?如果是这样,Netzke 演示应用程序中给出了一个很好的例子(请参阅http://netzke-demo.herokuapp.com/中的老板和文员)。
您需要创建三个组件:
- 继承自 Netzke::Basepack::Grid 的问题组件(主)
- QuestionOption 组件(详情)继承自
- Netzke::Basepack::Grid Composite 组件结合了前两个
组件并添加了一些 JavaScript 来同步它们。
首先,QuestionOption 模型必须允许访问主表的 ID(Netzke 的东西,没有它就无法工作)。
class QuestionOption < ActiveRecord::Base
attr_accessible :title, :question_id
validates_presence_of :title
belongs_to :question
end
这是组件代码:
class Questions < Netzke::Basepack::Grid
def configure(c)
super
c.model = 'Question'
c.items = [
:description,
:title
]
end
end
class QuestionOptions < Netzke::Basepack::Grid
def configure(c)
super
c.strong_default_attrs = {question_id: c.question_id}
c.scope = {question_id: c.question_id}
c.model = 'QuestionOption'
c.columns = [
{ name: :title, header: 'Option Title'}
]
end
end
class QuestionsAndOptions < Netzke::Base
def configure(c)
super
c.items = [:questions, :question_options]
end
js_configure do |c|
c.layout = :border
c.border = false
c.height = '100%'
c.init_component = <<-JS
function() {
this.callParent();
var questions = this.netzkeGetComponent('questions');
var question_options = this.netzkeGetComponent('question_options');
questions.on('itemclick', function(view, record) {
this.selectQuestion(record.get('id'));
question_options.getStore().load();
}, this);
}
JS
end
endpoint :select_question do |id, this|
component_session[:selected_question_id] = id
end
component :questions do |c|
c.region = :center
end
component :question_options do |c|
c.region = :south
c.question_id = component_session[:selected_question_id]
c.height = '50%'
end
end
请尝试让我知道这是否是您需要的。对不起,如果我误解了你的问题。
问候
德拉赞