jsTree - Setting href attributes in Json data

北慕城南 提交于 2019-12-06 11:14:05

问题


Im trying to create a 'jsTree' treeview that gets it's data from a .Net webservice. Everything is working, except for the a-node's href attribute. Whatever I try, it always renders as '#'. As I understand from the documentation, all attributes in any data object get copied to the a-node.

Below is an example of my current json object. Can anyway figure out why the href attribute isn't copied to the nodes?

    [ { "attributes" : { "id" : "rootnode_2",
        "rel" : "root2"
      },
    "children" : [ { "attributes" : { "id" : "childnode_9",
              "rel" : "folder"
            },
          "children" : [ { "attributes" : { "id" : "childnode_23",
                    "rel" : "folder"
                  },
                "children" : null,
                "data" : { "href" : "http://www.google.com",
                    "title" : "Test_Below_1"
                  },
                "state" : null
              } ],
          "data" : { "href" : "http://www.google.com",
              "title" : "Test_1"
            },
          "state" : null
        },
        { "attributes" : { "id" : "childnode_10",
              "rel" : "folder"
            },
          "children" : [ { "attributes" : { "id" : "childnode_24",
                    "rel" : "folder"
                  },
                "children" : null,
                "data" : { "href" : "http://www.google.com",
                    "title" : "Test_Below_2"
                  },
                "state" : null
              } ],
          "data" : { "href" : "http://www.google.com",
              "title" : "Test_2"
            },
          "state" : null
        }
      ],
    "data" : { "href" : "http://www.google.com",
        "title" : "Glatt"
      },
    "state" : "closed"
  } ]

This is how I initialize the tree;

$("#jstreejson").jstree({
        json_data : {
            "data": treeObject
        },
        themes: {
            "theme": "apple",
            "dots": true,
            "icons": true,
            "url": "/Scripts/themes/apple/style.css"
        },
        plugins: ['core', 'themes', 'json', "json_data"]
    });

回答1:


So... I'm not sure that's entirely correct. You can't control the anchor attributes as far as I know, but what you can do add stuff to the attr hash in the json and then use the select_node.jstree event to open the desired link, i.e:

.bind("select_node.jstree", function (e,data) {
   var href_address = data.rslt.obj.attr("whatever");
   // open desired link
}


来源:https://stackoverflow.com/questions/9513301/jstree-setting-href-attributes-in-json-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!