atatanasov / gijgo

Gijgo - Free Javascript Controls
http://gijgo.com
MIT License
475 stars 187 forks source link

Treeview parents #488

Open LordRhialto opened 5 years ago

LordRhialto commented 5 years ago

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).

`<!DOCTYPE HTML>

Treeview

Tree view

`
Nuriman2 commented 5 years ago

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); } });

FenceDad commented 5 years ago

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.

azee commented 5 years ago

+1

anasuryana commented 1 year ago

how the progress ? i also face the case

Hedva commented 7 months ago

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;
  }
});