2013-05-05 46 views
1

域模型:type1,type2,type3和plant。如何筛选依赖于网格或表单中其他组合框的组合框(netzke)?

$ rails g model type1 name:string 
$ rails g model type2 name:string 
$ rails g model type3 type1:references type2:references name:string 
$ rails g model plant type1:references type2:references type3:references name:string 

在工厂的网格面板中,会出现三个组合框:type1,type2和type3。 Type3取决于type1和type2。如何选择类型1和类型2中的任何一个时,过滤type3组合框?

回答

4

我是Rails和Netzke的新手,但这是我如何解决问题的方法。

首先,如果类型3依赖于类型1和类型2,那么在工厂中只应引用类型3。

所以,与其

rails g model plant type1:references type2:references type3:references name:string 

使用

rails g model plant type3:references name:string 

你总是可以通过使用Netzke的__(双下划线)符号引用Type1和Type2。这是我的植物网格版本。我不允许内联编辑,除了最平凡的模式。

class Plants < Netzke::Basepack::Grid 
    def configure(c) 
    super 
    c.model = 'Plant' 
    c.persistence = true 
    c.columns = [ 
     { name: :type3__type1__name, header: 'Type 1'}, 
     { name: :type3__type2__name, header: 'Type 2'}, 
     { name: :type3__name, header: 'Type 3'}, 
     { name: :name, header: 'Plant Name' } 
    ] 
    c.enable_edit_inline = false 
    c.enable_add_inline = false 
    end 

    def preconfigure_record_window(c) 
    super 
    c.form_config.klass = PlantForm 
    end 
end 

要连接你需要的组合框:

  1. 定义类型3的组合范围,以便其数据取决于Type1和Type2的ID。

    #Example scope definition 
    scope: {type1_id: component_session[:type1_id], 
         type2_id: component_session[:type2_id]} 
    
  2. 定义Type1和Type2连击(见js_configure方法)监听器。监听器将检测type1和type2中的任何更改,并准备type3以在下一次选择它时刷新其数据。

  3. 端点和会话变量用于ID交换。

    //JavaScript code 
    //Definition of listener function for type1 combo 
    var handleType1Change = function() { 
    
        //Type3 value is no longer valid 
        type3Combo.clearValue(); 
    
        //Setting lastQuer to null will force data refresh for type3 combo 
        //next time it gets selected. 
        type3Combo.lastQuery = null; 
    
        //Call endpoint to define session variable with ID of type1 combo 
        this.selectType1({type1_id: type1Combo.value}); 
    }; 
    
    #Ruby code 
    #The endpoint is called from handleType1Chnage listener to 
    #set session variable with selected ID in type1 combo. 
    endpoint :select_type1 do |params, this| 
        component_session[:type1_id] = params[:type1_id] 
    end 
    

这是我厂的形式完整代码:

class PlantForm< Netzke::Basepack::Form 
    def configure(c) 
    super 
    c.model = 'Plant' 
    c.title = 'Plant' 
    c.items = [ 
     { 
     field_label: 'Type1', 
     xtype: :combo, 
     store: Type1.select([:id, :name]).map { |x| [x.id, x.name] }, 
     id: 'type1Combo', 
     #Sets the value for type1 in case change form is opened 
     value: record && record.id ? record.type3.type1_id : nil 
     }, 
     { 
     field_label: 'Type2', 
     xtype: :combo, 
     store: Type2.select([:id, :name]).map { |x| [x.id, x.name] }, 
     id: 'type2Combo', 
     #Sets the value for type2 in case change form is opened 
     value: record && record.id ? record.type3.type2_id : nil 
     }, 
     { 
     field_label: 'Type3', 
     name: :type3__name, 
     id: 'type3Combo', 
     data_store: {auto_load: false}, 
     scope: {type1_id: component_session[:type1_id], 
       type2_id: component_session[:type2_id]} 
     }, 
     { field_label: 'Name', name: :name } 
    ] 
    end 

    js_configure do |c| 
    c.init_component = <<-JS 
     function() { 
     this.callParent(); 

     var type1Combo = this.getComponent('type1Combo'); 
     var type2Combo = this.getComponent('type2Combo'); 
     var type3Combo = this.getComponent('type3Combo'); 

     var handleType1Change = function() { 
      type3Combo.clearValue(); 
      type3Combo.lastQuery = null; //force data refresh in type3 combo 
      this.selectType1({type1_id: type1Combo.value}); 
     }; 

     var handleType2Change = function() { 
      type3Combo.clearValue(); 
      type3Combo.lastQuery = null; 
      this.selectType2({type2_id: type2Combo.value}); 
     }; 

     type1Combo.addListener('select', handleType1Change, this); 
     type2Combo.addListener('select', handleType2Change, this); 
     } 
    JS 
    end 

    endpoint :select_type1 do |params, this| 
    component_session[:type1_id] = params[:type1_id] 
    end 

    endpoint :select_type2 do |params, this| 
    component_session[:type2_id] = params[:type2_id] 
    end 

end 
+0

万分感谢你!它工作得很好。 – douyw 2013-05-27 00:36:11

+0

太棒了。投了票,谢谢。 – ejo 2013-06-26 06:29:09

+0

@Drazen,你能解释一下这种形式如何用于过滤记录。如果需要,我可以提出一个单独的问题。 – ejo 2013-06-27 17:28:38

相关问题