Knockout add an item to an array conditionally - arrays

My application is MVC5. Using Ajax I am getting the array from Database, works well. However when I check if the value of specific item is not null and try to add it to the array it does not add it. I am using the following:
if (result && result.ohaMed.length > 0) {
for (var l = 0; l < result.ohaMed.length; l++) {
self.ohaitems.push({ OhaTypeId: result.ohaMed[l].OhaTypeId, Name: result.ohaMed[l].Name});
if (result.ohaMed[l].InfoURL != null) {
if (self.ohaitems.indexOf(result.ohaMed[l].InfoURL) < 0) {
self.ohaitems.push({ InfoURL: result.ohaMed[l].InfoURL });
}
};
};
};
If I add InfoURL as part of the first push it works.

Related

Adding another check property to select default rows on antd table

Currently i have this code on my api call to add a check property (list) (based on api called data "resRole" and "res") which can be used inside of rowSelection to select all the default checked row.
However, now i have another table which I need to do the same thing. Just that instead of using resRole, now I will use resProject. Which i need to first add a key to, before i add a checkProject in "res".
As such, i updated the check to checkRole and intend to put in an additional checkDept (list) in the getAllUserRole's res.data.
Looking at my code, I do not know where I can implement it. It seems like I have to create it inside of the getDataUserRole() function but that seems too messy. And might cause some async issues.
Below is the code:
async function getDataProject() {
let resProject = await getAllProject();
if (resProject) {
setDataSourceProject(resProject.data);
}
}
async function getDataUserRole() {
let resRole = await getAllRoles();
if (resRole) {
//Add a key to every Role
for (var i = 0; i < resRole.data.length; i++) {
resRole.data[i]["key"] = i;
}
setDataSourceRole(resRole.data);
let res = await getAllUserRole();
if (res) {
console.log("getAllUserRole =", res);
for (var i = 0; i < res.data.length; i++) {
//add "check" to every email in the array
res.data[i]["checkRole"] = [];
//loop through all the roleIds array in each email
for (var j = 0; j < res.data[i].roleIds.length; j++) {
//if roleIds is not empty
if (res.data[i].roleIds.length != 0) {
//loop through all Role from getAllRoles and check if any roleIds match the Role. If match push the key of the Role into check
for (var k = 0; k < resRole.data.length; k++) {
if (res.data[i].roleIds[j] == resRole.data[k].id) {
res.data[i]["checkRole"].push(resRole.data[k].key);
}
}
}
}
}
//If groupChange (groupChange is state for storing value of radio button) is null, return untransformed data
if (!(groupChange)) {
setDataSourceUserRole(res.data);
}
//If groupChange has value, call the function with the state value as a parameter
else {
var treeData = groupData(res.data, groupChange)
setDataSourceUserRole(treeData);
}
}
}
}
Instead of Using it inside getDataUserRole(). Use it inside getAllUserRole(). and once you get your result just add additional data with the role and send it back to one function.
If you want to call it separately so then you to depend it one function on another because due to async it will not work properly

Setting multiple values from a for loop, using an array in app script

I'm trying to make my code more compact. What I have now works, but it's slow and I don't think it's a best practice. I have a set of array data from Google Sheets, and I want to update a set of page_break values on Google form. I've managed to use a for loop to list all of the page_break ID's, and now I'd like to update them using my array data from Google sheets. I've managed to do this using if statements that filter each page_break ID based on it's title. However, is there a way to make this more slick? Is there a way to turn my page_break ID's into an array and update one with the other?
Thanks for your help. My current code is below.
//Trigger
function updateregentspark() {
var fApp = FormApp;
var formId = fApp.openById('1l5OQflzO3R5nW0ZPTHSGqcog47ADLfcnm7m2_82WokU').getId();
var form = fApp.openById('1l5OQflzO3R5nW0ZPTHSGqcog47ADLfcnm7m2_82WokU');
var formItems = form.getItems();
//Get Spreadsheet Location Tasks
var sApp = SpreadsheetApp;
var spreadsheet = sApp.openById('1iGWSUhaGxGtsxwOUZpcJBD-kXwubcenN0Q0H6lKAyFQ');
var locationSheet = spreadsheet.getSheetByName('Classic Scavenger Hunt Tasks');
var locationTasks = locationSheet.getRange('A2:A41').getValues();
Logger.log(locationTasks);
//Get Form Page Break Sections and Questions
var i, L=0, thisItem, thisItemType, myCheckBoxItem;
L = formItems.length;
for (i=0;i<L;i++) {
thisItem = formItems[i];
thisItemIndex = thisItem.getIndex();
thisItemType = thisItem.getType();
thisItemTitle = thisItem.getTitle();
//Update Page Break Values
if (thisItemIndex>6 && thisItemIndex<16 && thisItemType===fApp.ItemType.PAGE_BREAK) {
var sectionHeadersID = thisItem.getId();
if (thisItemTitle.indexOf('1.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[0]);
}
if (thisItemTitle.indexOf('2.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[1]);
}
if (thisItemTitle.indexOf('3.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[2]);
}
if (thisItemTitle.indexOf('4.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[3]);
}
if (thisItemTitle.indexOf('5.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[4]);
}
if (thisItemTitle.indexOf('6.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[5]);
}
}
};
}
I have no idea what your code is doing and how it could be speed up.
But you can make it a little shorter if you replace these lines:
if (thisItemTitle.indexOf('1.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[0]);
}
if (thisItemTitle.indexOf('2.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[1]);
}
if (thisItemTitle.indexOf('3.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[2]);
}
if (thisItemTitle.indexOf('4.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[3]);
}
if (thisItemTitle.indexOf('5.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[4]);
}
if (thisItemTitle.indexOf('6.') > -1) {
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[5]);
}
with this:
for (var j=0; j<6; j++)
if (thisItemTitle.indexOf( (j+1) + '.') > -1)
form.getItemById(sectionHeadersID).asPageBreakItem().setTitle(locationTasks[j]);

How to grab variable from one API that is within a nested Array response body?

How to grab the contentID and content title add it within an array and have it used in the URL of API 2
if i use the code below for section title and sectionid it is working because it is not in an nested array but for the contentid and contenttitle it is not working as it is in a nested array.
In the API 1 test tab i have:
for (i = 0; i < resultCount; i++) {
var id = jsonData[i].contents[0].contentId;
var modelString = jsonData[i].contents[0].contentTitle;
console.log(id)
console.log(modelString)
if (modelString.includes(“APIAUTOMATIONcontent”) || modelString.includes(“Testcontent”) || modelString.includes("{{POST-NewSlide}}") || modelString.includes(“stringcontent”)) {
hasDelete.push(id);
// (id) - this creates an integer (int)
// "announcementId": id, (creating object)
// "hasDelete": modelString.includes("Delete") || modelString.includes("Test")
// });
} else {
doesntHaveDelete.push(id)
// "announcementId": id
// });
}
}
// Check that each object in response contained keyword and length matches from test
pm.test(Number of Content that has APIAUTOMATIONcontent or Test ready to be deleted = ${hasDelete.length} out of Total ${resultCount} , function() {
console.log(hasDelete);
console.log(doesntHaveDelete);
pm.expect(hasDelete.length);
});
pm.collectionVariables.set(‘deletesections’, JSON.stringify(hasDelete));
Like you're iterating over each section in your response body, you also need to iterate over the contents array, you can do it like below:
for (i = 0; i < resultCount; i++) {
contentCount = jsonData[i].contents.length;
for (j = 0; j < contentCount; j++) {
let content = jsonData[i].contents[j];
console.log(content.contentId);
console.log(content.sectionId);
console.log(content.contentTitle);
console.log(content.pdfLink);
console.log(content.videoLink);
console.log(content.sortOrder);
}
}

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.

Listener afteritemexpand from ExtJS 4.2.1

I am working with a treepanel in ExtJS 4.2.1.
I used to set modifications on dom elements (precisely change the class name) with the listener afteritemexpand when I expand nodes of my tree. In fact it was to have leaves with even index with a different color than leaves with odd color.
It worked fine.
Getting the ids of the items which interest me, I can access them, and then modify the className.
I did the same way for another tree but the problem is that when I create this tree I expand it with an expandAll(), so the listener afteritemexpand is not called.
I need this expandAll(), but I need the afteritemexpand listener too. The reason I use this listener is because I have an easy access to item.id with the prototype afteritemexpand( node, index, item, eOpts ). With this id I can get the element I am looking for with the Ext.get(id) method.
I can do it with the afterlayout listener but I would rather not because the access to the id is not so easy.
I can't do it with the load listener because the dom elements are not present yet.
So I want to know, how could I totally expand my tree and use the code I made for my afteritemexpand?
Here is my listener so you can understand better want I want to do (in fact just add 'tree-even-node' to the className of even leaves of my tree).
listeners: {
afteritemexpand: function( node, index, item, eOpts ){
var domLeaf = Ext.get(item.id).next();
for ( var int = 0; int < node.childNodes.length; int++) {
if (node.childNodes[int].data.leaf && (int % 2) == 0) {
if (ids.indexOf(domLeaf.id) == -1) {
ids[indiceIds] = domLeaf.id;
indiceIds++;
}
}
domLeaf = domLeaf.next();
}
for ( var int = 0; int < ids.length; int++) {
domLeaf = Ext.get(ids[int]);
if (domLeaf != null) {
for ( var int2 = 0; int2 < domLeaf.dom.children.length; int2++) {
if (domLeaf.dom.children[int2].className.search('tree-even-node') == -1){
domLeaf.dom.children[int2].className += ' tree-even-node';
}
}
}
}
}
I finally used both load and afteritemexpand listeners. The loading enables me getting the right ids quite easily and I can set the classNames with afteritemexpand because I know the dom elements are loaded, so I don't get a null with my Ext.get(id).
It works great.
Here is the code:
listeners: {
load: function(node, records, successful, eOpts) {
var ownertree = records.store.ownerTree;
var boundView = ownertree.dockedItems.items[1].view.id;
var generalId = boundView+'-record-';
if (!node.tree.root.data.leaf) {
// Process each child node
node.tree.root.cascadeBy(function(currentChild) {
// Process only leaf
if (currentChild.data.leaf) {
var nodeId = ""+generalId+currentChild.internalId;
var index = currentChild.data.index;
if ( (index % 2) == 0 && ids.indexOf(nodeId) == -1 ) {
// even nodes
ids[indiceIds] = nodeId;
indiceIds++;
}
console.log(ids);
}
});
}
},
afteritemexpand: function( node, index, item, eOpts ){
for ( var int = 0; int < ids.length; int++) {
domLeaf = Ext.get(ids[int]);
if (domLeaf != null) {
for ( var int2 = 0; int2 < domLeaf.dom.children.length; int2++) {
if (domLeaf.dom.children[int2].className.search('tree-even-node') == -1){
domLeaf.dom.children[int2].className += ' tree-even-node';
}
}
}
}
},

Resources