使用关联父项的ajax配置选项使用json数据在rails中创建jstree =>孩子们

发布于 2024-12-26 02:38:06 字数 2033 浏览 3 评论 0原文

我的模型是:

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 树插件 与 json_data 示例:

$(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 数据示例进行测试,它创建了我想要的树:

{
"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" }

我想按照上面的架构示例自动生成 json 数据到我的项目集合。有什么建议吗?

My model is:

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 with json_data example:

$(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" ]
});
});

and the basic structure to supply data in the json format is:

{ 
    "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 */ ]
}

I want to create the jstree for a collection of projects dynamically. this collection can have childs, and each child childs and so on. my controller already responds to json and in view I have now the code to create the tree.

I test with this json data example and it creates the tree that I want to:

{
"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" }

I want to generate json data automatically to my collection of projects following the schema example above. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2025-01-02 02:38:06

OP 写道:

我解决了这个问题。解决方案是在以下一些函数中:

def 树(id_node_selected)

  if id_node_selected.to_i==0 父母=[] 否则
      Parentes=Project.find(id_node_selected).getPais 结束

  filhos = "" if [电子邮件受保护]? @projects.each_with_index 做
   |p,i|
          filhos += p.to_node(id_node_selected,parentes)
          如果我< @projects.count-1
              filhos += ","
          结束结束结束

  字符串=“{
          \“数据\” : {
          \"标题\" : \"项目\",
          \"attr\" : { \"href\" : \"/projects\" "

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

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

  返回字符串结尾

def pais(数组)

  if !self.parent.nil?数组<< self.parent.id
      self.parent.pais(数组) 结束

  返回数组末尾

 def getPais pais = []

  res = self.pais(pais)

  返回 pais 结束

 def to_node(id_node_selected,pais)

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

  如果 id_node_selected==self.id.to_s str += " , \"class\" :
    \"已选择\" " 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 += " } }, \"孩子\" : [ " end end

  filhos = "" if !self.children.empty?
      self.children.each_with_index 做 |c,i|
          filhos += c.to_node(id_node_selected,pais)
          如果我< self.children.count-1
              filhos += ","
          结束结束结束

  str += filhos + " ] } "

  返回字符串结尾

The OP wrote:

I solved the problem. Solution was in some functions below:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文