Closed softage0 closed 7 years ago
Supporting that case would be good. I could accept a PR, but I won't likely resolve it myself (no funding for the project anymore).
To implement the multiple sub parents, idField
must be declared in getParents
method.
And I'm considering the different approach to modify getParents
method, as let it looks for future parent
(searchingParent
on the below code) not previousParent
.
The code will looks like the below:
function getParents({
index,
idField = 'id',
parentField = 'parent'
} = {}) {
return (rows) => {
const parents = [];
let currentIndex = index;
let cell = rows[index];
let searchingParent;
if (
!cell ||
typeof cell[parentField] === 'undefined' ||
cell[parentField] === null
) {
return parents;
}
while (cell) {
if (typeof searchingParent === 'undefined') {
searchingParent = cell[parentField];
} else {
if (searchingParent === cell[idField]) {
parents.unshift(cell);
searchingParent = cell[parentField];
}
}
if (cell[parentField] === null) {
break;
}
currentIndex -= 1;
cell = rows[currentIndex];
}
return parents;
};
}
export default getParents;
It makes the code simpler but there are 2 issues needs to be considered:
idField
must be declared.
It's mandatory to implement the multiple sub parents.
It can be set default as id
same as other methods.index
, it will return empty array.
In my opinion, it's correct return.Please share your opinion. I'm not sure it's the right direction you intend to implement treetabular
.
If you agreed I will implemented this way and send PR.
idField must be declared.
That's fine. Most of the API requires it already so having it here doesn't hurt especially given the logic requires it.
If there's no matching parent of selected index, it will return empty array. In my opinion, it's correct return.
That can work. Another option would be to throw an error, but then the consumer would have to capture that. I'm fine with an empty array as long as it's documented.
@pzmudzinski Any thoughts on this?
Sounds good.
Also I think _.isNil(cell[parentField])
is way more pretty than typeof cell[parentField] === 'undefined' || cell[parentField] === null
;)
Cool :) Implementation is done. (@pzmudzinski thanks for your advice!) I will send PR after writing test cases and updating README.md
.
Pull has been requested.
This PR #4 includes the following features:
getParents
API has been refactored to show the multiple sub parents correctly.idField
argument has been added to getParents
API and relevant APIs.
README.md
and test cases also updated.toggleChildren
API have been added.Please let me know if I missed or did wrong codes :)
Nice work on a quick look. I'll try to provide a review in a day or two. If it's good enough, I'll likely just merge and cut a release. Thanks for all the work. 👍
I want to show the following hierarchy data:
I know you already mentioned on the README.md:
But there's no way to place the children right after their parents on the above hierarchy, and
treetabular
shows wrong hierarchy like the below image:Do you have plan to improve it?
The following code is my sample data: