javascriptjqueryfancytree

Recursively store parent nodes' titles up to root


I've got a table that looks like this: table.

So, any entry that is not a folder has a corresponding input tag. Right now I'm setting the name attribute for the inputs in the renderColumn method, like this:

const node = data.node;
const $tdList = $(node.tr).find('>td');

if (node.isFolder()) {
  $tdList.eq(1).empty();
} else {
  const $input = $tdList.eq(1).find('input');
  $input.attr('name', `${node.parent.title}_${node.title}`);
  $input.attr('value', node.data.value);
}

but what it does is effectively storing only the parent's title. Is there any way I could recursively store all the parents' titles up to root, so that, for example, in version column I had input[name="categoryCell__version"], then in backgroundColorcategoryCell_configuration_backgroundColor, etc?


Solution

  • You can do it recursively.

    function buildTitle(node) {
      if (node.parent.className !== 'alex-node`) { // just an example, change the condition to something else
        return '';
      } else {
        return buildTitle(node.parent) + node.parent.title + '__';
      }
    }
    

    (you'll need to trim the underscores, but it's rather simple)