gijgo icon indicating copy to clipboard operation
gijgo copied to clipboard

Treeview parents

Open LordRhialto opened this issue 6 years ago • 5 comments

I'm trying to find the parents of a specific node, but the code crashes on line 10709 (parents.push(list[i].data[data.textField]);) of the gijgo.js file (v1.9.11).

`

	<title>Treeview</title>
	
	<script src="js/jquery-1.9.1.js"></script>
	<script src="js/bootstrap-v3.0.2.js"></script>
	<script src="js/gijgo.js"></script>
	
	<link rel="stylesheet" href="css/bootstrap.css" />
	<link rel="stylesheet" href="css/gijgo.css" />
	<style type="text/css">
		.list-group-item.active {
			background-color: magenta;
		}
	</style>
</head>

<body>
	<h1>Tree view</h1>
	<div id="tree"></div>
</body>

<script type="text/javascript">
	$('document').ready(function() {
		
		var tree = $('#tree').tree({
				uiLibrary: 'bootstrap',
				primaryKey: 'id',
				dataSource: [ { 'id': 1, text: 'Organisation', children: [ { 'id': 2, text: 'Marketing & Sales', children: [ { 'id': 4, text: 'Sales account executive' } ] }, { 'id': 3, text: 'Research & Development' } ] }],
				icons: {
					expand: '<span class="glyphicon glyphicon-chevron-down"></span>',
					collapse: '<span class="glyphicon glyphicon-chevron-up"></span>'
				}
		});
		
		tree.on('select', function (e, node, id) {
			console.log(tree.parents(id));
		});
	});
</script>
`

LordRhialto avatar Jan 15 '19 14:01 LordRhialto

gijgo-combined-1.9.11/js/gijgo.js and early versions This is a bag in the : pathFinder: function (data, list, id, parents) { var i, result = false;

    for (i = 0; i < list.length; i++) {
        if (list[i].id == id) {
            result = true;    
            break;
        } else
        {
         if (gj.tree.methods.pathFinder(data, list[i][data.childrenField], id, parents)) {
            // parents.push(list[i].data[data.textField]); // original with error
             parents.push(list[i][data.textField]);           // my correction, it works for me
            result = true;
            break;
           }
        }
    }
    return result;
}

ps: you must define primaryKey: 'id' in yor tree init statement!!!

$('#tree').tree({ primaryKey: 'id', dataSource: [ { id: 101, text: 'foo', children: [ { id: 202, text: 'bar' } ] } ], select: function (e, node, id) { alert('Your key is ' + id); } });

Nuriman2 avatar Feb 26 '19 06:02 Nuriman2

Any update on this? Fixing the line above fixes the error, but parents are still returned as an empty array. This includes when setting the primaryKey in the init. This would be super helpful to have this working.

FenceDad avatar Apr 18 '19 15:04 FenceDad

+1

azee avatar May 08 '19 08:05 azee

how the progress ? i also face the case

anasuryana avatar May 21 '23 12:05 anasuryana

Anyone feel free to make a pull request

Fix:

pathFinder: function (data, list, id, parents) {
    var i, result = false;

    if (typeof list !== undefined && list !== null) {
        for (i = 0; i < list.length; i++) {
            if (list[i].id == id) {
                result = true;
                break;
            } else if (gj.tree.methods.pathFinder(data, list[i][data.childrenField], id, parents)) {
                parents.push(list[i][data.textField]);
                result = true;
                break;
            }
        }
    }

    return result;
}

Or if you want to place your fix in a seperate file:

$.extend(gj.tree.methods, {
  pathFinder: function (data, list, id, parents) {
      var i, result = false;

       if (typeof list !== undefined && list !== null) {
          for (i = 0; i < list.length; i++) {
              if (list[i].id == id) {
                  result = true;
                  break;
              } else if (gj.tree.methods.pathFinder(data, list[i][data.childrenField], id, parents)) {
                  parents.push(list[i][data.textField]);
                  result = true;
                  break;
              }
          }
      }

      return result;
  }
});

Hedva avatar Jan 31 '24 10:01 Hedva