getOptions() for loop not working - javascript

The following code throws error. I try to get (alert) the value and options of an Optionset in MS CRM 2013, It successfully shows all the things but after that it shows error. i attached the screen shot that error
function GetOptionsetLable()
{
var OptionSetControl = Xrm.Page.getAttribute("test_613a");
for(var intCounter=0; OptionSetControl .getOptions().length; intCounter++)
{
var backendvalue=OptionSetControl .getOptions()[intCounter].value;
alert(backendvalue.toString());
}
}

function GetOptionsetLable()
{
var OptionSetControl = Xrm.Page.getAttribute("test_613a");
for(var intCounter=0; OptionSetControl .getOptions().length; intCounter++)
{
var backendvalue=OptionSetControl .getOptions()[intCounter].value;
alert(backendvalue.toString());
}
}
Your for loop will never end because you don't tell it when to stop.
OptionSetControl.getOptions().length
should be:
intCounter < OptionSetControl.getOptions().length
Full code:
function GetOptionsetLable()
{
var OptionSetControl = Xrm.Page.getAttribute("test_613a");
for(var intCounter=0; intCounter < OptionSetControl.getOptions().length; intCounter++)
{
var backendvalue=OptionSetControl.getOptions()[intCounter].value;
alert(backendvalue.toString());
}
}

Remember that the value property contains the optionset numeric value, and the .text property the label.
you can also use a shorter for condition:
var options = Xrm.Page.getAttribute("test_613a").getOptions();
for (var i in options) {
alert(options[i].value);
}
I also created a library, you can find here:
OptionSet JavaScript Helper Library

Related

Cannot read property 'enumNodeFragments' of undefined

I'm trying to change the color of elements in 3D Viewer using the Autodesk-forge platform, and for this I'm using this API https://forge.autodesk.com/cloud_and_mobile/2015/12/change-color-of-elements-with-view-and-data-api.html by Daniel Du.
But the problem is when running I got this
The error Pict
And this the function :
Autodesk.Viewing.Viewer3D.prototype.setColorMaterial = function(objectIds, color) {
var material = addMaterial(color);
for (var i=0; i<objectIds.length; i++) {
var dbid = objectIds[i];
//from dbid to node, to fragid
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
var it = viewer.model.getData().instanceTree;
console.log(it);
it.enumNodeFragments(dbid, function (fragId) {
var renderProxy = viewer.impl.getRenderProxy(viewer.model, fragId);
console.log("r prox : " + renderProxy);
renderProxy.meshProxy = new THREE.Mesh(renderProxy.geometry, renderProxy.material);
renderProxy.meshProxy.matrix.copy(renderProxy.matrixWorld);
renderProxy.meshProxy.matrixWorldNeedsUpdate = true;
renderProxy.meshProxy.matrixAutoUpdate = false;
renderProxy.meshProxy.frustumCulled = false;
viewer.impl.addOverlay(overlayName, renderProxy.meshProxy);
viewer.impl.invalidate(true);
}, false);
});
}
}
Hopefully, anyone has the solution to this problem...
Most likely you are running this code before the instance tree has been loaded, which provokes the error Cannot read property 'enumNodeFragments' of undefined on it variable. You would need to wait for the Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT before running that code.
Take also a look at previous question about modifying materials in the viewer.

Filling Data using Angular js filter method

I am new to Angular js,
I have the following code which binds data to the div,
app.filter("myfilter", function () {
return function (data, catName) {
if (angular.isArray(data) && angular.isString(catName)) {
var rs = [];
var key = {};
for (var i = 0; i < data.length; i++) {
var currdata = data[i][catName];
if (angular.isUndefined(key[currdata])) {
key[currdata] = true;
rs.push(currdata);
}
}
return rs;
}
else
return data;
}
})
But when i came across the code above i found a line "key[currdata] = true;"
what's this piece of code does?
when i comment this particular line data binding not happening?
Thanks in advance.
This line is just like a mark in order that in the next iteration the condition "angular.isUndefined..." is false and, in that way, it prevents push duplicated values into rs array.
it is simply checking that no duplicate key is pushed into rs

Values won't to an array with a callback - JavaScript

I have this code:
rtclient.listFiles = function(callback) {
gapi.client.load('drive', 'v2', function() {
gapi.client.drive.files.list({
'maxResults': 20
}).execute(callback);
});
}
which I try to assign to an array using this code:
var arrayStore;
rtclient.listFiles(function(x) {
for(var i in x.items) {
arrayStore.push(x.items[i].id);
}})
However, the array arrayStore is not storing anything. This is using the Google Drive API and I am trying to access some file IDs to iterate over. Can anyone help?
the point is you should have a error like:
TypeError: Cannot call method 'push' of undefined
because you haven't defined it as an array, the other point is don't use for(...in) for arrays, you can find reasons about that in this post:
Why is using “for…in” with array iteration such a bad idea?
var arrayStore = [];
rtclient.listFiles(function(x) {
for(var i = 0; i < x.items.length; i++) {
arrayStore.push(x.items[i].id);
}
});
The other important point as #toothbrush has commented is, since the Google Drive API is asynchronous, you can't access the contents of arrayStore immediately, in other word to continue your scenario you should do something like this:
var arrayStore = [];
rtclient.listFiles(function(x) {
for(var i = 0; i < x.items.length; i++) {
arrayStore.push(x.items[i].id);
}
nextStep();
});
function nextStep(){
//here you would have access to arrayStore
console.log(arrayStore);
}

How do I use a variable in this function?

I've got this problem i can't solve.
I want to make the part that says "aboutMe" a variable called page_name (after the parameter), but I can't get it working...
$(document).ready(function(page_name) {
$.getJSON('../json/text.json', function(obj) {
// fill the title
for(i in obj.titles) {
var id1 = obj.titles[i].id;
var title1 = obj.titles[i].title;
$("#"+id1+"_title").append(title1);
}
// fill the rest of the page
for(i in obj.pages.aboutMe.page_element) {
var id = obj.pages.aboutMe.page_element[i].id;
var title = obj.pages.aboutMe.page_element[i].title;
var content = obj.pages.aboutMe.page_element[i].content;
var left_content = obj.pages.aboutMe.page_element[i].content_left;
var right_content = obj.pages.aboutMe.page_element[i].content_right;
$("#"+id+"_title").append(title);
$("#"+id+"_content").append(content);
$("#"+id+"_left_content").append(left_content);
$("#"+id+"_right_content").append(right_content);
}
for(var i in obj.pages.study) {
}
});
});
Does Anyone know how to solve this?
I would be very gratfull!
What is the error? Try using $(document).ready(function(){}); syntax. It may happen that there is a Jquery conflict. You can also resolve that by JQuery(document).ready(function(){});

'undefined' elements in the array

I have the following JavaScript I am using to save selected check boxes:
function SubmitCheckBoxes()
{
alert("test");
var selectedIDs = [];
var x = 0;
a = document.getElementsByTagName("input");
for (i = 0; i < a.length; i++) {
if (a[i].type == "checkbox" ) {
if (a[i].checked) {
alert(a[i].value);
selectedIDs[x] = a[i].value;
x++;
}
}
}
$.post('./Courses/SaveAndRedirect', selectedIDs, function (data) { });
}
However when I look at the form data being submitted all its says is undefined: undefined for each element in the array.
Not sure what the problem is here.
It is the data attribute in the jquery post method that is wrong. It can't take an array as a parameter. Here is the documentation
data map or string that is sent to the server with the request.
Try using a object literal instead:
$.post('./Courses/SaveAndRedirect', {selectedIDs: selectedIDs}, function (data) { });
I would also try writing selectedIDs[x] = a[i].value; differently:
selectedIDs.push(a[i].value);
I think the problem may be that your post variables are associated with just a numeric instance rather than a field name
You can try something like this:
var selectedIDs = [];
$('input[type="checkbox"]:checked').forEach(function(i, e){
selectedIDs.push($(e).val());
});
$.post('./Courses/SaveAndRedirect', selectedIDs, function (data) { });

Categories

Resources