How do I split constructed jstree into 2 parts - jstree

I am using jstree and jstree grid for contructing my tree structure. Can I split the constructed jstree into 2 parts?

You can split it with code as below.
Check demo: Fiddle
$("#tree1")
.on("select_node.jstree", function(e, data) {
// destroy second tree and remove DOM elements
if ($("#tree2").find('ul').length > 0) {
$("#tree2").jstree('destroy').empty();
}
// get json format for branch to move selected node/branch from first tree
var json = data.instance.get_json(data.node);
// init second tree with new data
$("#tree2").jstree({
"core": {
"data": json
}
});
// remove selected node/branch from the first tree
$("#tree1").jstree('delete_node', [data.node.id]);
$("#" + data.node.id).remove();
})
.jstree({
"core": {
"data": data1
}
})

Related

AG-Grid - Show sort icon without reloading data

I have an angular application and when I load my page the columns are not yet defined.
The columns are defined when I retrieve the data (which are already sorted).
So if I apply a state on my sorted column (after the data are initialized), the data are refreshed again and I want to avoid this.
Is there a solution to show the sort icon without AG-Grid refresh the data ?
To be more explicit, here is my angular code :
getRows(params: any): void {
//retrieve my data
this.myservice.getAll(params.currentPage, params.pageSize, params.sort).subscribe((data) => {
//send my datas to AG-Grid
params.successCallBack(data.results, data.totalItems);
if (params.currentPage == 0 && this.firstLoad) {
//my datas contain the columns definition
this.initGrid(data.viewParams);
}
});
}
initGrid(viewParams: ViewParamsModel): void {
this.firstLoad = false;
this.dataGrid.setColumnDefs(viewParams.columns);
//I try with this but it reloads my data
this.gridColumnApi.applyColumnState({
state: [
{
colId: 'name',
sort: 'asc',
}],
});
}
When doing this.dataGrid.setColumnDefs(viewParams.columns)
add sort: 'desc'/'asc'
columnDefs: [
{
field: 'Column1',
sort: 'desc', // <====== add this
},

Vuetify TreeView + Drag and drop

I am trying to implement drag and drop on Vuetify Treeview and data table. It seems like it is not supported fully but a workaround is described in this thread. The workaround is however not complete. Perhaps the community would benefit if someone created a codepen or similar on this?
What confuses me is that the component DragDropSlot.vue is created but "drag-drop-slot" is used in the code. Also there is a "_.cloneDeep(this.tree)" call where _ is not defined. I assume it should be replaced by something. When I comment that out drag and drop does still not work. Probably missed something more like defining data. Not sure of correct data types. It seems to be based on react which I have not worked with. Have just started to learn vue and vuetify.
I'm open for any suggestion for how to solve this.
All the best
I use V-Treeview with Vue.Draggable (https://github.com/SortableJS/Vue.Draggable).
I use direct link.
<script src="//cdn.jsdelivr.net/npm/sortablejs#1.8.4/Sortable.min.js"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/Vue.Draggable/2.20.0 vuedraggable.umd.min.js"/>
<v-treeview
:active.sync="active"
:items="users"
:search="search"
item-key="Id"
item-text="UserName"
item-children="Children"
:open.sync="open"
activatable
color="warning"
dense
transition
return-object
>
<template v-slot:label="{ item }">
<draggable :list="users" group="node" :id="item.Id" :data-parent="item.ParentId" #start="checkStart" #end="checkEnd" >
<label>
<i class="fas fa-user mr-3" />
<span id="item.id" >{{item.UserName}}</span>
</label>
</draggable>
Also I add ParentId property to item tree model:
{
Id:1,
UserName: "John Doe",
ParentId: null,
Children:[{Id:2, ParentId: 1,...}]
}
Then I use start and end events where I search parent start node from I drag the item and parent end node where I drop the item. When parent is null the item is a root.
new Vue({
el: '#app',
vuetify: new Vuetify(),
components: {
vuedraggable
},
data() {
return {
active: [],
open: [],
users: [],
selectedItems: [],
}
},
mounted: function () {
this.fetchUsers();
},
methods: {
findTreeItem: function (items, id) {
if (!items) {
return;
}
for (var i = 0; i < items.length; i++) {
var item = items[i];
// Test current object
if (item.Id === id) {
return item;
}
// Test children recursively
const child = this.findTreeItem(item.Children, id);
if (child) {
return child;
}
}
},
checkStart: function (evt) {
var self = this;
self.active = [];
self.active.push(self.findTreeItem(self.users, evt.from.id))
},
checkEnd: function (evt) {
var self = this;
var itemSelected = self.active[0];
var fromParent = itemSelected.ParentId ? self.findTreeItem(self.users, itemSelected.ParentId) : null;
var toParent = self.findTreeItem(self.users, evt.to.id);
var objFrom = fromParent ? fromParent.Children : self.users;
objFrom.splice(objFrom.indexOf(itemSelected), 1);
if (toParent.Id === itemSelected.Id) {
itemSelected.ParentId = null;
self.users.push(itemSelected);
}
else {
itemSelected.ParentId = toParent.Id;
toParent.Children.push(itemSelected);
}
self.saveUser(itemSelected);
// self.active = [];
return false;
},
fetchUsers: function () {
//load from api
},
saveUser: function (user) {
//save
},
},
computed: {
selected() {
if (!this.active.length) return undefined
return this.active[0];
},
}
})
Hope I help you.
IngD.
After some additional work I ended up with implementing Drag and Drop on top of vuetify tree view and data table using this library:
https://www.vuetoolbox.com/projects/vue-drag-drop
At first I looked at draggable and similar but realized it was always based on that you move an element from position A to position B. I needed more control. For example I wanted the element to disappear when dropping on some drop zones.
found this component.
https://vuejsexamples.com/vuetify-draggable-v-treeview-component/
I didn't try it myself (because it has too few options), but it looks working well in demo.
Anyways, just to try

jsTree get siblings of a node

I'm using drag and drop with jsTree, and when I drag a node to a new position, I have to update not only its position, but the positions of its siblings too in the database.
Here's an example:
If I drag New node into node Gamme 1, now it's siblings are gamme 501 and gamme 500
Getting data about the moved node and updating it's position in db is not a problem:
.on("move_node.jstree", function (e, data) {
console.log(data);
$.ajax({
type: "POST",
url: Routing.generate('saveGammeNewPosition'),
data: {
gammeId: data.node.id,
newParentId: data.node.parent,
position: data.position
}
});
})
But I have no idea how to get information about the new siblings of a node that has been moved.
When I do something like this, unfortunately I get no data on position as the json jsTree accepts has no position attribute:
$('#tree').jstree(true).get_json(data.node.parent, {flat: true})
So is there a way I can get data on positions of the siblings?
You can get the new parent of the node in the move_node event and use that to get its immediate children.
function getSiblings(nodeID, parent) {
var tree = $('#tree').jstree(true),
parentNode = tree.get_node(parent),
aChildren = parentNode.children,
aSiblings = [];
aChildren.forEach(function(c){
if(c !== nodeID) aSiblings.push(c);
});
return aSiblings;
}
$('#tree').on("move_node.jstree", function (e, data) {
var aSiblings = getSiblings(data.node.id, data.parent);
console.log(aSiblings);
});

openui5: How to get current JSON model element in RowRepeater

I'm having trouble getting the current JSON model element which is bound to a RowRepeater element.
With tables and lists, I would simply retrieve the current index (or indices) and based on these values, I point to the matching element in my JSON model.
However, the RowRepeater element does not have a current index property. As I feel I should be able to retrieve the current element directly, as opposed to indirectly by the current index, is there a better, uniform way to retrieve the current element?
Sample code for model :
var mydata = {
"data": [
{
"key": "67b895bf-8d89-11e3-94a7-0000005341de",
"name": "my 1st item"
},
{
"key": "7780de05-8d83-11e3-bec4-0000005341de",
"name": "my 2nd item"
}
]
};
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(dummydata);
sap.ui.getCore().setModel(oModel);
Sample code for RowRepeater (I want to retrieve the current 'key' upon pressing the delete icon):
var oRowRepeater = new sap.ui.commons.RowRepeater();
//create the template control that will be repeated and will display the data
var oRowTemplate = new sap.ui.commons.layout.MatrixLayout();
var matrixRow, matrixCell, control;
// main row
matrixRow = new sap.ui.commons.layout.MatrixLayoutRow();
//Text
control = new sap.ui.commons.TextView();
control.bindProperty("text","name");
//add content to cell, cell to row
matrixCell = new sap.ui.commons.layout.MatrixLayoutCell();
matrixCell.addContent(control);
matrixRow.addCell(matrixCell);
//delete icon
var icon = new sap.ui.core.Icon({
src: sap.ui.core.IconPool.getIconURI("delete"),
size: "16px",
color: "#333",
activeColor: "#BBB",
hoverColor: "#888",
width: "60px",
});
icon.attachPress(function(oEvent) {
sap.ui.commons.MessageBox.alert("TODO: Implement delete based on current/data/?/key");
});
//add content to cell, cell to row
matrixCell = new sap.ui.commons.layout.MatrixLayoutCell({ hAlign : sap.ui.commons.layout.HAlign.Right });
matrixCell.addContent(icon);
matrixRow.addCell(matrixCell);
// add row to matrix
oRowTemplate.addRow(matrixRow);
//attach data to the RowRepeater
oRowRepeater.bindRows("/data", oRowTemplate);
the following works for me
icon.attachPress(function(oEvent) {
sap.ui.commons.MessageBox.alert(this.getBindingContext().getProperty('name'));
});
the selected object
var seletedRow = this.getBindingContext().getObject()

AngularJS: Move to next form input element after successful validation

I have written a custom directive for validation of my form fields. When certain criteria are met (i.e. it is dirty and valid), I want to set the focus automatically to the next input element. This is a requirement from my users, such that they can move through the forms most efficiently.
The simplified directive looks like this:
directive('custom', ['$parse', function($parse) {
return {
restrict: 'A',
require: ['ngModel', '^ngController'],
link: function(scope, element, attrs, ctrls) {
var model=ctrls[0], form=ctrls[1];
scope.next = function(){
return model.$valid
}
scope.$watch(scope.next, function(newValue, oldValue){
if (newValue && model.$dirty){
???
}
})
Now my question is: how can I identify
- the next input element (which is the next sibling) or possibly via the tabindex
- and focus on it
without using Jquery?
For me, it is currently not clear, how to get to the next input element from the available "scope" or "element" attributes without Jquery; and JQlite does nothave a "focus" method. Basically, I need a working substitute for ??? in my code.
Any help is highly appreciated. Thanks
Juergen
You can use [0] to get the underlying input element (which has a focus() function) from the angular/jqLite object (which doesn't).
app.directive('custom', ['$parse', function($parse) {
return {
restrict: 'A',
require: ['ngModel'],
link: function(scope, element, attrs, ctrls) {
var model=ctrls[0], form=ctrls[1];
scope.next = function(){
return model.$valid;
}
scope.$watch(scope.next, function(newValue, oldValue){
if (newValue && model.$dirty)
{
var nextinput = element.next('input');
if (nextinput.length === 1)
{
nextinput[0].focus();
}
}
})
}
}
}])
http://jsfiddle.net/Y2XLA/
element.next().focus() might not work if you have a complex form and input are nested into different divs.
I ended writing this directive (here I move the focus on Enter, but can be adapted to whatever event):
.directive('enterToTab', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var procAttr = 'data-ett-processed';
$timeout(function() { // Use $timeout to run the directive when the DOM is fully rendered
var formElements = element[0].querySelectorAll('input:not([' + procAttr + '="true"]), select:not([' + procAttr + '="true"]), textarea:not([' + procAttr + '="true"])');
// Run through all elements in form
var formElementsLength = formElements.length;
for (var i = 0; i < formElementsLength; i++) { // Add tabindex attribute
formElements[i].setAttribute('tabindex', i + 1);
// Go to next element on Enter key press
formElements[i].addEventListener('keypress', function(event) {
if (event.keyCode === 13) { // Enter
// Prevent Angular from validating all the fields and submitting
if (event.target.tagName !== 'TEXTAREA') { // Not on textarea, otherwise not possible to add new line
event.stopPropagation();
event.preventDefault();
}
var nextIndex = parseInt(event.target.getAttribute('tabindex')) + 1;
// Move focus to next element
// TODO: find next visible element
var nextElem = element[0].querySelector('[tabIndex="' + (nextIndex) + '"]');
if (nextElem) {
nextElem.focus();
}
}
});
formElements[i].setAttribute(procAttr, true); // Add attribute to prevent adding 2 listeners on same element
}
});
}
};
});
Event should be in HTML component (keypress) = "keyFocus($event)"
Method shoulb be like .ts file.
keyFocus(input1){
input1.srcElement.nextElementSibling.focus();
}
AngularJS already contains a light version of jQuery so you can as well use it...
http://docs.angularjs.org/api/angular.element
You could try something like this:
element.next().focus()