如何使用jquery删除树视图子节点

How to remove treeview child node using jquery

本文关键字:视图 子节点 删除 何使用 jquery      更新时间:2023-09-26

我有一个像下面这样的树视图,我需要从基于用户的列表中删除一个孩子。谁能告诉我如何从树视图中删除子视图?

var treedata = [
           {
               label: 'Floor', type: 'Country',
               children: [
                   {
                       label: 'Bangalore', type: 'Location',
                       children: [{ label: 'Technopolis', type: 'facility', id: 1 }, { label: 'Ecity Tower-2', type: 'facility', id: 2 }, { label: 'Bagmane', type: 'facility', id: 3 }, { label: 'Cyber Park', type: 'facility', id: 4 }]
                   },
                   {
                       label: 'Hyderabad', type: 'Location',
                       children: [{ label: 'Hitech City ', type: 'facility', id: 5 }, { label: 'Cyber City', type: 'facility', id: 6 }]
                   },
                   {
                       label: 'Chennai', type: 'Location',
                       children: [{ label: 'XXX', type: 'facility', id: 7 }]
                   },
                   {
                       label: 'Mumbai', type: 'facility', id: 8
                   }
               ]
           },
             {
                 label: 'Role Administration', type: 'Role',
                 children: [{ label: 'Assign Role', type: 'Role', id: 1 }]
             },
{
    label: 'Hoteling Admin', type: 'Hoteling',
    children: [{ label: 'Hoteling', type: 'Hoteling', id: 1 }]
}

];

以上是我的jquery树数据。如果用户是普通用户,我想通过检查用户角色来删除角色管理。

谁来教我如何使用jquery。

谢谢

您可以使用$.grep()根据您想要的任何条件将数组筛选为新的数组。

var userRole='normal';
if( userRole === 'normal'){    
    treeview = $.grep(treeview, function(item){
         return item.label != 'Role Administration';
    });    
}

grep() API docs