Ztree Anti-Baidu dropdown box to achieve input keyword automatic matching results

Source: Internet
Author: User

The effect is shown in the following illustration


The idea is simply to loop through all the Ztree node data and then create the tree.

The code is as follows Copy Code

Initial data for Ztree
var znodes = [
{id:1, pid:0, Name: "Match"},
{id:2, pid:0, Name: "Match 2"},
{id:3, pid:0, Name: "Match 4343"},
{id:6, pid:0, Name: "Match 444"},
{id:4, pid:0, name: "Hebei Province", Open:true},
{id:41, Pid:4, Name: "Shijiazhuang"},
{id:42, Pid:4, Name: "Baoding"},
{id:43, Pid:4, Name: "Handan"},
{id:44, Pid:4, Name: "Chengde"},
{id:5, pid:0, name: "Guangdong Province", open:true},
{id:51, Pid:5, Name: "Guangzhou"},
{id:52, Pid:5, Name: "Shenzhen"},
{id:53, Pid:5, Name: "Dongguan"},
{id:54, Pid:5, Name: "Foshan"},
{id:6, pid:0, Name: "Fujian Province", Open:true},
{id:61, Pid:6, Name: "Fuzhou"},
{id:62, Pid:6, Name: "Xiamen"},
{id:63, Pid:6, Name: "Quanzhou"},
{id:64, Pid:6, Name: "Sanming"}
];

Click on a node and assign the name of the node to the Value text box
function OnClick (E, Treeid, TreeNode) {
var ztree = $.fn.ztree.getztreeobj ("Treedemo");
Get the selected node
var nodes = Ztree.getselectednodes (),
v = "";
Sort by ID
Nodes.sort (function compare (A, b) {return a.id-b.id;});
for (var i = 0, L = nodes.length i < l; i++) {
V + + nodes[i].name + ",";
}
Displays the name of the selected node in a text box
if (v.length > 0) v = v.substring (0, v.length-1);
var cityobj = $ ("#citySel");
Cityobj.attr ("value", V);
Hide Ztree
Hidemenu ();
return false;
}
Show Tree
function ShowMenu () {
var cityobj = $ ("#citySel");
var Cityoffset = $ ("#citySel"). Offset ();
$ ("#menuContent"). CSS ({left:cityOffset.left + "px", Top:cityOffset.top + cityobj.outerheight () + "px"}). Slidedown ("FA St ");
}
Hide Tree
function Hidemenu () {
$ ("#menuContent"). Fadeout ("fast");
$ ("body"). Unbind ("MouseDown", Onbodydown);
}
Restore initial data for Ztree
function Initialztree () {
$.fn.ztree.init ($ ("#treeDemo"), setting, znodes);
}
Automatic matching of tree nodes to Fuzzy Lookup based on keyword input of text box
function Automatch (txtobj) {
if (TxtObj.value.length > 0) {
Initialztree ();
var ztree = $.fn.ztree.getztreeobj ("Treedemo");
var nodelist = ztree.getnodesbyparamfuzzy ("name", Txtobj.value, false);
var Nodearr = new Array ();
This is done in order to avoid the parent node fuzzy matching data will automatically be brought into all its child nodes
for (var i = 0; i < nodelist.length; i++) {
var obj = new Object ();
Obj.id = nodelist[i].id;
Obj.pid = Nodelist[i].pid;
Obj.name = Nodelist[i].name;
Nodearr.push (obj);
}
Update the found NodeList node to Ztree
$.fn.ztree.init ($ ("#treeDemo"), setting, Nodearr);
ShowMenu ();
} else {
Hide Tree
Hidemenu ();
Initialztree ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.