I have an issue, where my treeview is instantiated inside a bootstrap modal.
When I frist open the modal everything works correctly.
If I close said modal, then reopen it, the already checked boxes are there which is fine, but if I check/uncheck a checkbox I get a console error saying that getCheckedNodes is not a function.
I've tried assign tree both to a const inside a function and globally.
The issue is for some reason after the modal re-open tree loses the method mentioned in the title.
function onModalToggle() {
const treeviewInput = document.querySelector('#treeview');
const modal = document.querySelector('#exampleModal');
const treeviewSearchInput = document.querySelector('#treeviewSearch');
// console.log(treeviewInput, modal, treeviewSearchInput);
fetchData();
}
function fetchData() {
fetch('src/assets/data/get-user-interests-categories.json').then(response => response.json()).then(resp => createTreeviewFromData(resp.categories));
}
function remapDataToTreeView(data) {
return {
text: data.title,
categoryId: data.categoryId,
children: data?.child?.length > 0 ? data.child.map(c => remapDataToTreeView(c)) : [],
parentCategoryId: data.parentCategoryId,
}
}
function createTreeviewFromData(data) {
data = data.map(d => remapDataToTreeView(d));
dataSource = data;
tree = $('#tree').tree({
primaryKey: 'categoryId',
uiLibrary: 'bootstrap4',
dataSource: data,
checkboxes: true,
icons: {
expand: '<i class="material-icons">chevron_right</i>',
collapse: '<i class="material-icons">expand_more</i>'
}
});
tree.on('checkboxChange', (e, $node, record, state) => onTreeCheckboxChange(e, $node, record, state));
}
function onTreeCheckboxChange(e, $node, record, state) {
console.log(tree);
const checked = tree.getCheckedNodes();
const parentCategories = dataSource.filter(cat => cat.parentCategoryId === 0);
console.log(checked);
// fiddle with checked ids that clearly not in the scope of the issue
}
NOTE: I am new to jQuery (mainly I learned Angular so I am unfamiliar how Jquery handles stuff in the background), if it's a main jQuery issue let me know.
"tree" needs to be declared as global variable accessible by the onTreeCheckboxChange function. I think that you don't have access to this variable in that function.
I have an issue, where my treeview is instantiated inside a bootstrap modal. When I frist open the modal everything works correctly. If I close said modal, then reopen it, the already checked boxes are there which is fine, but if I check/uncheck a checkbox I get a console error saying that
getCheckedNodes is not a function
.I've tried assign tree both to a const inside a function and globally.
The issue is for some reason after the modal re-open tree loses the method mentioned in the title.
HTML structure:
main JS file
NOTE: I am new to jQuery (mainly I learned Angular so I am unfamiliar how Jquery handles stuff in the background), if it's a main jQuery issue let me know.