KendoUI Treeview with AngularJS uncheck checkboxes - angularjs

I'm using KendoUI (v2015.1.318) with AngularJS (v1.3.14). After every click on a node, the data will be fetched from an API.
After selecting some items and clicking on the button "add", the items will be added in a seperated list, the expanded treenodes must stay visible but all the
checked items must be unchecked.
After I iterated through the datasource to uncheck the checked items , I have call SetDataSource again. When I have expanded a lot of nodes and checked a few, the UI freezes some seconds while it's processing.
I was wondering if there wasn't a more efficient way to execute this.
I made an example without API call:
$scope.saveTreeFields = function () {
var data = $scope.tree.dataSource._data;
for (var i = 0, j = data.length; i < j; i++) {
checkChildren(data[i]);
}
function checkChildren(data) {
if (data.checked) {
data.checked = false;
}
if (data.items !== undefined) {
for (var i = 0, j = data.items.length; i < j; i++) {
checkChildren(data.items[i]);
}
}
}
$scope.tree.setDataSource(data)
};
Plunker example

Received an answer from Telerik itself.
you can call the dataItem set("checked", false) method to notify the
model of the field change, and the TreeView will update automatically.
Code changes
$scope.saveTreeFields = function () {
var data = $scope.tree.dataSource.data();
for (var i = 0, j = data.length; i < j; i++) {
checkChildren(data[i]);
}
function checkChildren(data) {
if (data.checked) {
data.set("checked", false);
}
if (data.items !== undefined) {
for (var i = 0, j = data.items.length; i < j; i++) {
checkChildren(data.items[i]);
}
}
}
};

Related

AngularJS - Delete check box is not working correctly and only deleting one at a time

I've written out a block of code that allows the user to check or uncheck entities that will be added or removed via web services. My add function seems to be working correctly and provides the ability to add multiple entities. However, my delete function isn't working the same. It doesn't delete each time, and can only delete one at a time. I'm struggling since the code is effectively the same as the add, so I don't know if the issue is AngularJS related or perhaps my web service isn't working correctly.
Edit: I've actually noticed that the for loop goes through it all but doesn't select the correct id, it always starts from the first one.
var toDeleteService = [];
for (var i = 0; i < $scope.siteServices.length; i++) {
if ($scope.siteServices[i].chosen != $scope.siteServices[i].origChosen) {
if ($scope.siteServices[i].chosen == true) {
toAddService.push(i);
}
else {
toDeleteService.push(i);
}
}
}
if (toDeleteService.length > 0) {
var deleteRequest = {};
deleteRequest.services = [];
for (var i = 0; i < toDeleteService.length; i++) {
var parentServiceName = $scope.siteServices[i].parentServiceName;
var j = 0;
for (; j < deleteRequest.services.length; j++) {
if (deleteRequest.services[j].parentServiceName == parentServiceName) {
break;
}
}
if (j == deleteRequest.services.length) {
deleteRequest.services[j] = {};
deleteRequest.services[j].parentServiceName = parentServiceName;
deleteRequest.services[j].subservices = [];
}
var service = {};
service.serviceId = $scope.siteServices[i].serviceId;
deleteRequest.services[j].subservices.push(service);
}
var deleteUrl = "api/sites/" + $scope.targetEntity.siteId + "/services/" + service.serviceId;
$http.delete(deleteUrl)
.then(function (response) {
});
}
As I understood it you are trying to remove siteServices based by numbers stored in var toDeleteServices = [] so you need to access those numbers by its index. but in service.serviceId = $scope.siteServices[i].serviceId; you are using i instead.
service.serviceId = $scope.siteServices[toDeleteServices[i]].serviceId; as you need actual number of the service to delete.
If I understood your code correctly.

Fabric.js group/ungroup - individual objects disappear while ungrouping

I have this functions in my angularJS controller:
function group(){
var activegroup = canvas.getActiveGroup();
var objectsInGroup = activegroup.getObjects();
activegroup.clone(function(newgroup) {
canvas.discardActiveGroup();
objectsInGroup.forEach(function(object) {
canvas.remove(object);
});
canvas.add(newgroup);
});
}
function ungroup(){
var activeObject = canvas.getActiveObject();
if(activeObject.type=="group"){
var items = activeObject._objects;
activeObject._restoreObjectsState();
canvas.remove(activeObject);
for(var i = 0; i < items.length; i++) {
canvas.add(items[i]);
canvas.item(canvas.size()-1).hasControls = true;
}
}
canvas.renderAll();
}
Working almost perfectly - jus when I ungroup the objects are not rendered (they are still in the canvas, but not visible). I can select the objects individually even if they are invisible. Selectin multiple object will show them while they are selected – deselecting one will let the other disappear. In short: ungrouping objects will not show as individual objects again.
This applys only to primitives like, box, circle, triangle. Text an images are not affected.
Help is appreciated!
function ungroup(){
var activeObject = canvas.getActiveObject();
if(activeObject.type=="group"){
var items = activeObject._objects;
alert(items);
activeObject._restoreObjectsState();
canvas.remove(activeObject);
for(var i = 0; i < items.length; i++) {
canvas.add(items[i]);
items[i].dirty = true; //set object dirty true
canvas.item(canvas.size()-1).hasControls = true;
}
canvas.renderAll();
}
}
If you set object dirty true, it will render in renderAll() call and redraw again.

Angular values update in both elements

I am adding same element (json object) into a list (from another list) twice using .copy to break the reference. But even after doing that when I change some values in one both of them are getting updated.
$scope.addProduct = function (item) {
var index = $scope.itemsProduct.indexOf(item);
$scope.scopItem = {};
angular.copy(item , $scope.scopItem);
for(var j in $scope.scopItem['ABC']) {
$scope.scopItem['ABC'][j].dataType='Discount';
$scope.scopItem['ABC'][j]['Discount'] = '';
}
if (index != -1) {
$scope.itemsTags.push($scope.scopItem);
$timeout(function() {
$scope.$apply(function () {
$scope.calculate($scope.scopItem);
});
}, 10);
}
};
$scope.calculateParam = function (indexQ) {
var index = $scope.itemsTags.indexOf(indexQ);
$scope.itemsTags[index]['ABC']['Discount'] = '10'; //or some other logic
}
Need help as even i am not adding the same element(using .copy) and updating the "Discount" property of one updates both??
Note: ABC is a inner list with property as "Discount" and I am changing "Discount"
Some comments:
for(var j in $scope.scopItem['ABC'])
it's absolutely incorrect and should be rewrited to
for(var j=0; j < $scope.scopItem['ABC'].length; j++)
also scopItem is changed in calculate method and stored in itemsTags

Avoid duplicates in a repeater are not allowed

Is there any built-in feature on AngularJS to avoid ng-repeater to receive duplicated entries?
Right now I'm using the following code to prevent it:
$scope.tags = ['black','white','red','yellow','blue'];
$scope.selectedTags = [];
// textarea value
var words = $scope.message.split(' ');
for(var j = 0; j < words.length; j++) {
for (var k = 0; k < $scope.selectedTags.length; k++) {
if ($scope.selectedTags[k].Name == words[j]) {
contains = true;
}
}
if (!contains)
{
$scope.selectedTags.push($scope.tags[i]);
contains = false;
}
}
Angular UI has a unique filter:
Filters out all duplicate items from an array by checking the specified key
Alternatively, if it's just a string array you can filter your array like:
arr.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});

How to make an Array visible while also making the other arrays hidden once clicked AS3

I have a set of movieclips setup as arrays in As3. I got the code to work were once a button is clicked a movieclip show on stage; but once the user clicks on another thumbnail the previous movieclip is still on stage. What I am trying to accomplish is I want the other arrays or movieclips to become hidden once the main array or clip that the user clicks becomes visible. I know I probably need a if loop for the arrays heres my code:
var soles:Array = [sle1,sle2,sle3,sle4];
var Slbtn_arr:Array = [sole,sole2,sole3,sole4];
for (var i= 0; i < Slbtn_arr.length; i++)
{
trace(i,Slbtn_arr[i]);
var temp_Slbtn = Slbtn_arr[i];
temp_Slbtn.addEventListener(MouseEvent.CLICK, btnCl);
temp_Slbtn.count = i;
soles[i].visible = false;
soles[0].visible = true;
}
function btnCl(e)
{
var num = e.target.count;
trace(e.target, e.target.count, soles[e.target.count]);
//hideAll()
soles[num].visible = true;
}
function hideAll()
{
for (var i= 0; i < soles.length; i++)
{
soles[i].visible = false;
}
}

Resources