javascript - 用递归方式怎么实现多层父子关系的元素进行删除?
阿神
阿神 2017-04-11 13:02:38
0
3
324

功能代码:
点击删除按钮触发事件:
备注:$(".btn_del")是删除按钮
需求:只要删除当前的行是有子集的数据,那么就一并删除子集的数据。
现在的关联关系的就是父级有id,子集有pid,一个或多个层级都是对应的。
用循环写不知道感觉太麻烦了,不知道用递归是否可以解决这类问题,理解意思的朋友能不能说说你们的思路?
这段代码应该怎么修改?

// 删除单项或父子级关系的数据
$(".btn_del").click(function() {
    var single_row = $(this).closest('tr');
    console.info($(this).closest('tbody').find('tr'));
    var arr_tr = $(this).closest('tbody').find('tr');
    var cur_trId = $(this).closest('tr').attr('id');
    console.log(cur_trId);
    // var data = single_row.attr('data');
    // console.info(JSON.parse(data));
    var cv = single_row.attr('hv');
    var tr_id = single_row.attr('id');
    var tr_pid = single_row.attr('pid');
    console.log(cv);
    if (cv == 1) {
        var stit = '审核';
        var scont = '该分类包含的自己分类也会被全部删除 您确定要删除该和子级分类吗?';
        layer.confirm(scont, {
            icon: 0,
            title: stit,
            shade: false
        }, function(index) {
            //todo 删除id和pid关联的数据方法
            //先发请求调用接口再做删除
            //用递归解决多层级删除
            arr_tr.each(function() {
                // console.log($(this).attr('id'));
                // console.log($(this).attr('pid'));
                var cid = $(this).attr('id');
                var Pid = $(this).attr('pid');
                console.log(Pid);
                if (cur_trId == Pid) {
                    // $(this).remove();
                    // console.info($(this));
                    console.log("ffffff");
                }
            });
            layer.msg('已删除!', {
                icon: 1,
                time: 1000
            });
        });
    } else {
        layer.confirm('确认要删除吗?', {
            icon: 0,
            title: '警告',
            shade: false
        }, function(index) {
            //todo 删除id和pid关联的数据方法
            //先发请求调用接口再做删除
            single_row.remove();
            layer.msg('已删除!', {
                icon: 1,
                time: 1000
            });
        });
    }

});
阿神
阿神

闭关修行中......

全部回复(3)
洪涛

用通配符写选择器呗
比方说你要删除 TR_0
先删除 $('tr[id^=TR_0_]').remove();
再删除 $('tr[id=TR_0]').remove();

黄舟

我说说思路吧,这个问题很简单,不用递归,而且不用pid,因为不需要。
方法:正则表达式
前提:你的id都是有规律的,比如你的 TR_0 表示一级,TR_0_1表示二级的第一个TR,TR_0_1_1表示三级的第一个TR。
之后,匹配正则表达式用:

other_id_str.test(id_str);

例如:
二级菜单的id是 TR_0_1
那么遍历一下该table的所有id,比如假设有一个id为 TR_0_1_1,那么它的:

"TR_0_1_1".test("TR_0_1")

值就是true,说明是它的子集,可以删除。

具体代码如下:

把原来的:

            arr_tr.each(function() {
                // console.log($(this).attr('id'));
                // console.log($(this).attr('pid'));
                var cid = $(this).attr('id');
                var Pid = $(this).attr('pid');
                console.log(Pid);
                if (cur_trId == Pid) {
                    // $(this).remove();
                    // console.info($(this));
                    console.log("ffffff");
                }
            });

修改为:

            arr_tr.each(function() {
                var cid = $(this).attr('id');
                if (cid.test(cur_trId)) {
                    $(this).remove();
                }
            });
Ty80

循环和递归都可以,递归的话就先删子再删父。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!