DOM not updated when taken value from localstorage - javascript

I have created an button as:
<ion-radio ng-repeat="business in multipleBusiness track by business.id" ng-model="choice.value" ng-value="business">{{business.name}}</ion-radio>
and In controller I am doing...
$scope.choice.value = localStorageService.get('defaultBusiness') || $scope.multipleBusiness[0];
Now the radio buttons are shown with a checkmark when the value passed to $scope.choce.value is $scope.multipleBusiness[0] but when the value from localStorage is used i.e. localStorageService.get('defaultBusiness') , the chekmark is not shown
Even though on consoling both the values are same structure i.e objects with id and name keys.

The localStorageService.get('defaultBusiness') will return string. You need to use JSON.parse() to have it as an json object.

Related

How to push an object in a object - javascript

I have two ways to fill the same hidden input in a form
Using an import CSV button
Adding data using another inputs
When I use the first option, the hidden input is filled with this
for example:
correct data
[{"url":"http://www.restaurant.com","businessTypeId":"1"},{"url":"http://www.hotel.com","businessTypeId":"2"}]
and works correctly if I store this data
but when I use the second option, the input is filled with this:
incorrect data
{"url":"http://www.google.com","businessTypeId":"3"}
That's incorrect because it doesn't have [brackets] at the beginning neither at the end
Another problem is when I insert data and fill that hidden input (with the first way) and then I try to add more data using the second way,
I get this
[{"url":"http://www.restaurant.com","businessTypeId":"1"},
{"url":"http://www.hotel.com","businessTypeId":"2"}],
{"url":"http://www.google.com","businessTypeId":"3"}
the first 2 data was inserted using the first way, the third data was inserted using the 2nd way
all data should be inside those brackets []
how can I "open" the brackets to push new data and "close" them?
at the beginning of all the code, the variable starts like this
let placesArray = [];
after the first method, data is inserted using this
placesArray.push(JSON.stringify(csvResult));
document.getElementById('places').value = placesArray;
them, after the second method, data is inserted using this
placesArray.push(JSON.stringify(placeData));
console.log('placeData datatable ' + placeData);
document.getElementById('places').value = placesArray;
Note: if I use the first method two times, brackets are duplicated, like this
[{"url":"http://www.restaurant.com","businessTypeId":"1"}
{"url":"http://www.hotel.com","businessTypeId":"2"}],
[{"url":"http://www.beauty-shop.com","businessTypeId":"3"},
{"url":"http://www.dentist.com","businessTypeId":"5"}]
I definitely need to "open" the object so that I can insert the new data and not go through this, how could I do that?
In the console.log, I have this for placeData [object Object], same result for csvResult, both are object
You could flatten the array before every value set
placesArray = placesArray.flat()
document.getElementById('places').value = placesArray;
Seems like csvResult is itself an array and as you stringify it and push it to placesArray, it doesn't look like the result you want.
I'd go with this instead
placesArray.push(...csvResult) // push each `csvResult` item into `placesArray`
document.getElementById('places').value = JSON.stringify(placesArray)
SOLVED:
I erased all JSON.stringify and added it to every line when I push data into inputs
like this
document.getElementById('places').value = JSON.stringify(placesArray);
in all lines when this is used.
thanks to #hgb123
for prevent to accumulate [brackets[more brackets]] I used .flat()
placesArray = placesArray.flat()
document.getElementById('places').value = JSON.stringify(placesArray);

array object with empty field name

In node-red i have array object in msg.payload.
the array object is [{"":"This Pallet ID is already in Pick Request List"}]
The array object has empty field name. please suggest how to retrieve value form the array.
I have tried msg.payload[0][0], msg.payload[0].Value. But not working
What you need is
var text = msg.payload[0][""];
Works in Chrome at least...

How to add new key/value pair element to an existing array?

var onHomePageLoaded = function(retMsg)
{
$scope.data = retMsg.data.records;
$scope.data.link : 'http://www.newwebsite.com'
}
After i have added link element (key/value) to the javascript object, i am not able to get the same in the HTML template
<div ng-repeat="record in data">
<a ng-href="{{record.link}}"> Click Here </a>
</div>
Javascript is a dynamic language. You can add properties to existing objects in a very simple way , like assigning a value to an existing property. Just add a new property
$scope.data.link = 'http://www.newwebsite.com'
if retMsg.data.records is an array, still you can add a property to $scope.data.
if you want different link for every object in array then, do this.
$scope.data.forEach(function(obj){
obj.link = "your custom link" // write your logic here to produce different link.
});
If data is an array you can use
$scope.data.push(yourData);
for example
$scope.data.push({link : 'http://www.newwebsite.com'});
Or if you want to access the objects inside the array and add them a key value pair you can do as follow:
// add the link to the first entry
$scope.data[0].link = 'http://www.newwebsite.com';
Sorry. Do not know if I understood well.
Maybe you can define scope.data as:
$scope.data = {retMsg.data.records}
Then for example a function:
$scope.addNew = funtion(){
$scope.data.newElement = $scope.viewElement
};
In your HTML
<label>{{data}}</label> // Which makes reference to the $scope.data at the controller
<input ng-change="addNew()" ng-model="viewElement"></input>
<label>{{data.newElement}} // Will be empty at the very beginning but will show the new element once it is created.
Hope it helps
I see several issues with your code.
First, you use the variable name record in your ng-repeat, but then use report in ng-href. I assume those should be the same.
Also, link isn't a member of record, it is a member of data. You set it as a member of data here: $scope.data.link : 'http://www.newwebsite.com'. If you want to add that link to each record, in your onHomePageLoaded function, you'll need to loop through all the records you add to data, and add the link property to each one.

AngularJS not finding values in array

I have a AngularJS app developed.
On a view I have a list of items - each with its own set of radio buttons. The list and buttons are built at run-time from an API call.
Both the list and radio buttons can be of any length e.g. list item 1 might contain 3 radio buttons while list item n might contain 2 radio buttons.
When the user selects a radio button I fire a ng-click method that sends both the list ID as well as the radio button ID to the controller.
The view looks as follows and I have tested this and the values are being sent to the controller.
<label class="radio-button" ng-repeat="option in checkItemDescription.options">
<input type="radio" name="option_question_id_{{checkItemDescription.fleetcheckitemid}}"
ng-model="checkItemDescription.selected_id"
ng-click="doSomething(checkItemDescription.fleetcheckitemid, option.fleetcheckid)"
ng-value="option.fleetcheckid">
<div class="radio-button__checkmark"></div>
Description: {{option.checkvaluedesc}}
</label>
In my doSomething() method i am trying to see if either the checkItemDescription.fleetcheckitemid or the option.fleetcheckid id is in a existing array.
I use indexOf() method to compare the values but the method does not seem to work - even though console.log() shows values are sent to the controller.
Below is my doSomething() method.
$scope.doSomething = function(fleetCheckItemID, fleetCheckID)
{
if ( $scope.initialFleetCheckIssueItemIDArray.indexOf(fleetCheckItemID))
{
console.log("Yeah!"); // Never gets to here even though logs show values exist
}
else
{
console.log("Ah!"); // Always get here
}
}
What I need to do is to to create a JSON object with the list item ID, radio button ID and some other values e.g. date/time based on the user selection to send to my Database.
Is this the best way to do it or is there a better way?
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
-referenced from MDN.
so if your element is first in the collection then indexOf would return 0 which would result in the execution of else block so change your if condition like this.
if ( $scope.initialFleetCheckIssueItemIDArray.indexOf(fleetCheckItemID) != -1 )
{
//element found, do stuff here.
}
else
{
//element does not found, else stuff here.
}

Get field value from mongoDB and assign to a var gives undefined

I am using MeteorJS and trying to get the value of a field from MongoDB and assign to a variable. But when want to print to console, it gives all the time 'undefined'. It works fine in HTML template, but i need to store the value in a var in .js file.
var num = ButtonsList.find({_id:'ZcLkjSwNGTpgHkoeq'});
var n = num.button1;
console.log("button number is: "+n);
The code below works fine by the way, if i want them to output in the browser. It outputs the buttons numbers in html using {{}} namespace. But as I said, i need to store the values in variables.
ButtonsList = new Meteor.Collection('list');
Template.theList.helpers({
'buttons': function(){
//return ButtonsList.find().fetch();
return ButtonsList.find('ZcLkjSwNGTpgHkoeq');
}
});
ButtonsList.find() returns a cursor.
ButtonsList.find().fetch() returns an array of buttons.
ButtonsList.findOne() returns will return a single button.
ButtonsList.findOne().fieldName will return the field fieldName of the button that was found.
The reason it works with the {{#each}} template block helper is that each blocks know how to iterate over cursors.
Your using Find , doesnt that mean your getting multiple reccords back? Shouldnt you be using FindOne instead? otherwise youll get an array of objects which means youd have to use num[i].button1 to get to the value.

Categories