2012-01-11 8 views
0

> =関連の親のためのAjaxの設定オプションを使用してJSONデータをレールにjstreeを作成します。json_data例と私のモデルがある子供

class Project < ActiveRecord::Base 
    belongs_to :parent, :foreign_key => :project_id, :class_name => "Project" 
    has_many :children, :dependent => :destroy, :class_name => "Project", :foreign_key => :project_id 

jquery tree plugin

$(function() { 
    $("#demo2").jstree({ 
     "json_data" : { 
      "ajax" : { 
       "url" : "/static/v.1.0pre/_docs/_json_data.json", 
       "data" : function (n) { 
        return { id : n.attr ? n.attr("id") : 0 }; 
       } 
      } 
     }, 
     "plugins" : [ "themes", "json_data" ] 
}); 
}); 

と供給するための基本的な構造json形式のデータは次のとおりです。

{ 
    "data" : { 
     "title" : "The node title", 

    // omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function 
    "attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" } }, 
    // `state` and `children` are only used for NON-leaf nodes 
    "state" : "closed", // or "open", defaults to "closed" 
    "children" : [ /* an array of child nodes objects */ ] 
} 

コレクションのjstreeを作成したいとします。動的にプロジェクトを進める。このコレクションは子を持つことができ、各子は子を持つことができます。私のコントローラーは既にjsonに応答しています。私は今、ツリーを作成するコードを持っています。

私はこのJSONデータの例をテストし、それは私がしたいツリーを作成:私は上記のスキーマの例を次のプロジェクトの私のコレクションに自動的にJSONデータを生成したい

{ 
"data" : { 
    "title" : "Projectos", 
    "attr" : { "href" : "/projects" } }, 
    "children" : [ { 
     "data" : { 
      "title" : "teste", 
      "attr" : { "href" : "/projects/7" , "class" : "selected" } }, 
      "state" : "open" , 
      "children" : [ { 
       "data" : { 
        "title" : "teste_1", 
        "attr" : { "href" : "/projects/9" } }, 
        "children" : [ ] } 
      ] } , { 
"data" : { 
    "title" : "teste1", 
    "attr" : { "href" : "/projects/8" } }, 
    "children" : [ ] } 
], "state" : "open" } 

を。助言がありますか?

+0

:ソリューションは、以下のいくつかの機能にありました。何ができないのですか?ダイナミックな部分か...? – Radek

+0

私はダイナミックな部分を動作させることができませんでした。数日後に私はそれを得た!私の編集を表示 – ihmabreu

答えて

-1

ザ・OPは書いた:

を、私は問題を解決しました。私はあなたの質問を理解していなかった

def tree(id_node_selected) 

    if id_node_selected.to_i==0    parentes=[]   else 
     parentes=Project.find(id_node_selected).getPais   end 

    filhos = ""   if [email protected]?   @projects.each_with_index do 
    |p,i| 
      filhos += p.to_node(id_node_selected,parentes) 
      if i < @projects.count-1 
       filhos += "," 
      end    end   end 

    str = " { 
      \"data\" : { 
      \"title\" : \"Projectos\", 
      \"attr\" : { \"href\" : \"/projects\" " 

    if id_node_selected=="0"   str += " , \"class\" : \"selected\" " 
    end 

    if filhos==""   str += " } }, \"children\" : [ ] } "  else   str 
    += " } }, \"children\" : [ " + filhos + " ], \"state\" : \"open\" } "  end 

    return str end 

def pais(array) 

    if !self.parent.nil?   array << self.parent.id 
     self.parent.pais(array)   end 

    return array end 

def getPais  pais = [] 

    res = self.pais(pais) 

    return pais  end 

def to_node(id_node_selected,pais) 

    str = " { \"data\" : { \"title\" : \"#{self.name}\", 
      \"attr\" : { \"href\" : \"/projects/" + self.id.to_s + "\" " 

    if id_node_selected==self.id.to_s   str += " , \"class\" : 
    \"selected\" "   if !self.children.empty? 
      str += " } }, \"state\" : \"open\" , \"children\" : [ "    else 
      str += " } }, \"children\" : [ "   end   else   if !pais.empty? && pais.include?(self.id) 
      str += " } }, \"state\" : \"open\" , \"children\" : [ " 
      pais.delete(self.id)   else 
      str += " } }, \"children\" : [ "   end   end 

    filhos = ""   if !self.children.empty? 
     self.children.each_with_index do |c,i| 
      filhos += c.to_node(id_node_selected,pais) 
      if i < self.children.count-1 
       filhos += "," 
      end    end   end 

    str += filhos + " ] } " 

    return str end 
+0

([質問に回答し、コミュニティのwikiに変換しました](http://meta.stackoverflow.com/questions/267434/what-is-the-appropriate-action-when-the-answer-to -a-question-is-the-queに追加されています)。 –

関連する問題