在动态嵌套元素中分配级联ID(JavaScript)

问题描述 投票:0回答:1

我正在开发一个动态生成3个不同组件的脚本来构建网格:section - > row - > module。现在我正在研究更新功能,该功能应该能够在创建新组件后立即更新网格中存在的每个元素的ID:

function Update() {

    // Define components variables
    var sections = document.querySelectorAll('#canvas [data-component="section"]');
    var rows = document.querySelectorAll('#canvas [data-component="row"]');
    var modules = document.querySelectorAll('#canvas [data-component="module"]');

    /**
     * Assign IDs to each existing section, row and module
     */
    // If there are sections...
    if ( sections.length > 0 ) {

        for ( var x = 0; x < sections.length; x++ ) {

            sectionNum = x + 1; 
            sections[x].id = 'component-' + sectionNum;

            // If there are rows...
            if ( rows.length > 0 ) {

                for ( var y = 0; y < rows.length; y++ ) {

                    // If this row is a descendant of that section...
                    if ( rows[y].parentElement.parentElement == sections[x] ) {

                        rowNum = y + 1; 
                        rows[y].id = 'component-' + sectionNum + '-' + rowNum;

                        // If there are modules...
                        if ( modules.length > 0 ) {

                            for ( var z = 0; z < modules.length; z++ ) {

                                // If this module is a descendant of that row...
                                if ( modules[z].parentElement.parentElement == rows[y] ) {

                                    moduleNum = z + 1;
                                    modules[z].id = 'component-' + sectionNum + '-' + rowNum + '-' + moduleNum;
                                };
                            };

                            // If the loop has reached the end, reset the index and break
                            if ( modules.length - 1 === z ) { z = 0; break };
                        };
                    };

                    // If the loop has reached the end, reset the index and break
                    if ( rows.length - 1 === y ) { y = 0; break; };
                };
            };

            // If the loop has reached the end, reset the index and break
            if ( sections.length - 1 === x ) { x = 0; break; };
        };
    };
};

我觉得我已经接近完成它,但我一直在努力寻找生成我需要的输出的方法。

这就是我现在所得到的:

#component-1
    #component-1-1
        #component-1-1-1
        #component-1-1-2
    #component-1-2
        #component-1-2-3
        #component-1-2-4
#component-2
    #component-2-3
        #component-2-3-5
        #component-2-3-6
    #component-2-4
        #component-2-4-7
        #component-2-4-8

但我需要重置每个新部分的行和模块号,如下所示:

#component-1
    #component-1-1
        #component-1-1-1
        #component-1-1-2
    #component-1-2
        #component-1-2-1
        #component-1-2-2
#component-2
    #component-2-1
        #component-2-1-1
        #component-2-1-2
    #component-2-2
        #component-2-2-1
        #component-2-2-2

任何想法都将受到欢迎:)

javascript nested dynamically-generated id
1个回答
0
投票

我再一次回答了我自己的问题:P如果它可能对其他人有帮助,那就去了。

首先,这是最终的代码:

/**
 * Update Components
 */
function Update() {

    /**
     * Assign IDs to each section, row and module
     */
    // Grab the sections contained in the canvas
    var sections = document.querySelectorAll('#canvas [data-component="section"]');

    if ( sections.length > 0 ) {

        for ( var x = 0; x < sections.length; x++ ) {

            // Increase num by 1 to avoid "0" as first index
            var sectionNum = x + 1;

            // Assign an ID to the current section
            sections[x].id = 'component-' + sectionNum;

            // Grab the rows contained in this section
            var rows = document.querySelectorAll('#' + sections[x].id + ' [data-component="row"]');

            if ( rows.length > 0 ) {

                for ( var y = 0; y < rows.length; y++ ) {

                    // Increase num by 1 to avoid "0" as first index
                    var rowNum = y + 1;

                    // Assign an ID to the current row
                    rows[y].id = 'component-' + sectionNum + '-' + rowNum;

                    // Grab the modules contained in this row
                    var modules = document.querySelectorAll('#' + rows[y].id + ' [data-component="module"]');

                    if ( modules.length > 0 ) {

                        for ( var z = 0; z < modules.length; z++ ) {

                            // Increase num by 1 to avoid "0" as first index
                            var moduleNum = z + 1;
                            // Assign ID to module
                            modules[z].id = 'component-' + sectionNum + '-' + rowNum + '-' + moduleNum;
                        }
                    }
                }
            }
        }
    }
}

我最后定义了前一个循环中的行和模块的变量,最后完成了这个技巧(在sections循环中定义了rows变量,在rows循环中定义了modules变量)。如果您想知道原因,那是因为这样做我能够使用当前父ID来限制搜索到该特定父级所包含的子级,然后在循环新父级时重新开始计数。

这是一个小细节,但我也删除了每个循环结束时的循环变量重置,这甚至不是必需的。

而且,作为一个加号,这在jQuery中是相同的:

/**
 * Update Components
 */
function Update() {

    /**
     * Assign IDs to each section, row and module
     */
    // Grab the sections contained in the canvas
    var sections = $('#canvas [data-component="section"]');

    if ( sections.length > 0 ) {

        $(sections).each( function(x) {

            // Increase num by 1 to avoid "0" as first index
            var sectionNum = x + 1;

            // Assign an ID to the current section
            $(this).attr('id', 'component-' + sectionNum);

            // Grab the rows contained in this section
            var thisSectionID = this.id;
            var rows = $('#' + thisSectionID).find('[data-component="row"]');

            if ( rows.length > 0 ) {

                $(rows).each( function(y) {

                    // Increase num by 1 to avoid "0" as first index
                    var rowNum = y + 1;

                    // Assign an ID to the current row
                    $(this).attr('id', 'component-' + sectionNum + '-' + rowNum);

                    // Grab the modules contained in this row
                    var thisRowID = this.id;
                    var modules = $('#' + thisRowID).find('[data-component="module"]');

                    if ( rows.length > 0 ) {

                        $(modules).each( function(z) {

                            // Increase num by 1 to avoid "0" as first index
                            var moduleNum = z + 1;

                            // Assign an ID to the current module
                            $(this).attr('id', 'component-' + sectionNum + '-' + rowNum + '-' + moduleNum);
                        });
                    };
                });
            };
        });
    };

希望它可以帮助那里的人! :)

© www.soinside.com 2019 - 2024. All rights reserved.