how to load json in dependable drop downs? - javascript

<b>Country:</b>
<select id="country" ng-model="statessource" ng-disabled="!type" ng-options="country for (country, states) in countries" ng-change="GetSelectedCountry()">
<option value=''>Select Country</option>
</select>
<b>State:</b>
<select id="state" ng-disabled="!statessource" ng-model="citiessource" ng-options="state for (state,city) in statessource"
ng-change="GetSelectedState()" >
<option value=''>Select State</option>
</select><br>
<b>City:</b>
<select id="city" ng-disabled="!citiessource || !statessource" ng-model="codessource" ng-options="city for (city,code) in citiessource" ng-change="GetSelectedCity()"><option value=''>SelectCity
</option>
</select><br>
<b>PostCode: </b>
<select id="code" ng-disabled="!citiessource || !statessource || !codessource" ng-model="code" ng-change="GetSelectedCode()">
<option value=''>SelectZipCode
<option ng-repeat="code in codessource" value='{{code}}'>{{code}}</option>
</select>
$scope.countries = {
'USA': {
'IL': {'CHICAGO':['60633','60646','60653','60656','60607','60632','60643','60652','60620','60612','60661','60624','60634','60601','60827','60644','60631','60630','60617','60610','60659','60621','60647','60609','60616','60637','60655','60649','60602','60622','60642','60603','60629','60641','60619','60615','60640','60707','60654','60605','60623','60606','60614','60608','60604','60651','60638','60618','60611','60657','60613','60636','60639','60628','60625','60660','60645','60626']},
'CA': { 'SAN FRANCISCO':['94127','94115','94124','94131','94104','94103','94121','94188','94129','94122','94109','94134','94141','94144','94123','94110','94116','94158','94133','94105','94102','94112','94108','94111','94130','94117']},
'LA': { 'BATON ROUGE':['70812','70808','70806','70807','70805','70816','70810','70802','70819','70811','70809','70815','70820','70814','70801']},
}
};
Here, I am passing values manually as you can see in "$scope.countries" in dependable drop downs. i want to load these values through json.
My json is like:
[{
"country": "USA",
"state": "CA",
"city": "SAN FRANCISCO",
"postcode": "94127"
}, {
"country": "USA",
"state": "CA",
"city": "SAN FRANCISCO",
"postcode": "94107"
}, {
"country": "USA",
"state": "NY",
"city": "NEW YORK",
"postcode": "10026"
}, {
"country": "USA",
"state": "NY",
"city": "NEW YORK",
"postcode": "10016"
}, {
"country": "USA",
"state": "CA",
"city": "SAN FRANCISCO",
"postcode": "94115"
}, {
"country": "USA",
"state": "NY",
"city": "NEW YORK",
"postcode": "10009"
}, {
"country": "USA",
"state": "IL",
"city": "CHICAGO",
"postcode": "60623"
}, {
"country": "USA",
"state": "CA",
"city": "SAN FRANCISCO",
"postcode": "94124"
}, {
"country": "USA",
"state": "IL",
"city": "CHICAGO",
"postcode": "60606"
}, {
"country": "USA",
"state": "CA",
"city": "SAN FRANCISCO",
"postcode": "94131"
}, {
"country": "USA",
"state": "LA",
"city": "BATON ROUGE",
"postcode": "70812"
}, {
"country": "USA",
"state": "IL",
"city": "CHICAGO",
"postcode": "60614"
}, {
"country": "USA",
"state": "NY",
"city": "BRONX",
"postcode": "10461"
}, {
"country": "USA",
"state": "NY",
"city": "BROOKLYN",
"postcode": "11221"
}, {
"country": "USA",
"state": "IL",
"city": "CHICAGO",
"postcode": "60608"
}, {
"country": "USA",
"state": "NY",
"city": "NEW YORK",
"postcode": "10003"
}, {
"country": "USA",
"state": "IL",
"city": "CHICAGO",
"postcode": "60604"
}, {
"country": "USA",
"state": "IL",
"city": "CHICAGO",
"postcode": "60651"
},
}]
Please suggest me how should i do this.

Related

Here API - geocode search using zipcode not returning city field

I am searching a City and state by zip code using below HERE API
https://geocode.search.hereapi.com/v1/geocode?qq=postalCode=43026&apiKey=key&in=countryCode%3AUSA,
but it's not returning city field.
the result is below
{ "items": [ { "title": "43026, OH, United States", "id": "here:cm:namedplace:22236211", "resultType": "locality", "localityType": "postalCode", "address": { "label": "43026, OH, United States", "countryCode": "USA", "countryName": "United States", "stateCode": "OH", "state": "Ohio", "postalCode": "43026" }, "position": { "lat": 39.9972, "lng": -83.15518 }, "mapView": { "west": -83.0952, "south": 39.97889, "east": -83.26074, "north": 40.06921 }, "scoring": { "queryScore": 1, "fieldScore": { "postalCode": 1 } } } ] }
can anyone please help
If you run several queries against this api, you'll notice that some results contain a city, and some do not.
For example:
If we run (with api key set to a valid value):
https://geocode.search.hereapi.com/v1/geocode?qq=postalCode=60606&apiKey=key&in=countryCode%3AUSA
We get an address like so
"address": {
"label": "60606, Chicago, IL, United States",
"countryCode": "USA",
"countryName": "United States",
"stateCode": "IL",
"state": "Illinois",
"county": "Cook",
"city": "Chicago",
"postalCode": "60606"
}
Which includes a city value.
Your query returns
"address": {
"label": "43026, OH, United States",
"countryCode": "USA",
"countryName": "United States",
"stateCode": "OH",
"state": "Ohio",
"postalCode": "43026"
},
Which is missing the city field.
You could try using the ZIP+4 code (if you have it) as mentioned in the API docs:
https://developer.here.com/documentation/geocoding-search-api/dev_guide/topics-api/code-geocode-hpc-hsn.html
For example,
https://geocode.search.hereapi.com/v1/geocode?qq=postalCode=43026-0001&apiKey=key&in=countryCode%3AUSA
"address": {
"label": "43026-0001, Hilliard, OH, United States",
"countryCode": "USA",
"countryName": "United States",
"stateCode": "OH",
"state": "Ohio",
"county": "Franklin",
"city": "Hilliard",
"postalCode": "43026-0001"
},
Will return us a city.
NB: Not all Zip+4 codes will return a city name. For example: 43026-0101 will not (this may be because it doesn't exist)
It doesn't look to me like there is any way to guarantee a city field response with just a ZIP code.

How to sort an object of objects in JavaScript?

I have an object of objects named teams:
{
"1": {
"number": "134C",
"robotName": "chazzen",
"name": "Team Discovery - Charlie",
"organization": "Pembroke Academy",
"city": "Pembroke",
"region": "New Hampshire",
"country": "United States"
},
"2": {
"number": "134G",
"robotName": "Ghammer Time",
"name": "Team discovery -Golf",
"organization": "Pembroke Academy",
"city": "Pembroke",
"region": "New Hampshire",
"country": "United States"
},
"3": {
"number": "134K",
"robotName": "Team Discovery kilo",
"name": "Team Discovery -Kilo",
"organization": "Pembroke Academy",
"city": "Pembroke",
"region": "New Hampshire",
"country": "United States"
},
"4": {
"number": "134E",
"robotName": "Team Discovery -Echo",
"name": "Team Discovery - Echo",
"organization": "Pembroke Academy",
"city": "Pembroke",
"region": "New Hampshire",
"country": "United States"
}
}
How do I sort these "sub-objects" alphabetically and numerically based on values such as "number" or "name" in a function?

JavaScript how to set nested json object data into single nested map

I want to set nested json data into nested map and iterate it.Consider the below sample json as example, i want to set firstName, lastName, address object, address 1 object value into single nested map.Also how to iterate it to get value from address object city field value.
Kindly provide the better solution.
Sample json:
[{
"firstName": "Jihad",
"lastName": "Saladin",
"address": {
"street": "12 Beaver Court",
"city": "Snowmass",
"state": "CO",
"zip": "81615"
},
"address1": {
"street": "16 Vail Rd",
"city": "Vail",
"state": "CO",
"zip": "81657"
}
}]
If you want to combine all the addresses, you can do the following
const people = [{
"firstName": "Jihad",
"lastName": "Saladin",
"address": {
"street": "12 Beaver Court",
"city": "Snowmass",
"state": "CO",
"zip": "81615"
},
"address1": {
"street": "16 Vail Rd",
"city": "Vail",
"state": "CO",
"zip": "81657"
}
}]
const newPeople = people.map(person => {
const addresses = [person.address]
for (let i = 1; person['address' + i]; i++) {
addresses.push(person['address' + i])
}
return {
firstName: person.firstName,
lastName: person.lastName,
addresses
}
})
console.log(JSON.stringify(newPeople))
/*
[{
"firstName": "Jihad",
"lastName": "Saladin",
"addresses": [
{
"street": "12 Beaver Court",
"city": "Snowmass",
"state": "CO",
"zip": "81615"
},
{
"street": "16 Vail Rd",
"city": "Vail",
"state": "CO",
"zip": "81657"
}
]
}]
*/

how to get data from a json file in real time

I try to make this for a long time and i wrote some test script .I use setInterval to update the content once at 500s but i can't acces the inputs for exemple if i have a form there i don't know how i can write in input without the write been deleted.I use setInterval because i want to see every input from the database .
The index script
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat="x in myData">
{{ x.Name + ', ' + x.Country }}<input type="text" />
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
setInterval(function(){$http.get("customers.json").then(function (response) {
$scope.myData = response.data.records;
});},500);
});
</script>
</body>
</html>
And the json file
{
"records": [
{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
},
{
"Name": "Ana Trujillo Emparedados y helados",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Antonio Moreno Taquería",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Around the Horn",
"City": "London",
"Country": "UK"
},
{
"Name": "B's Beverages",
"City": "London",
"Country": "UK"
},
{
"Name": "Berglunds snabbköp",
"City": "Luleå",
"Country": "Sweden"
},
{
"Name": "Blauer See Delikatessen",
"City": "Mannheim",
"Country": "Germany"
},
{
"Name": "Blondel père et fils",
"City": "Strasbourg",
"Country": "France"
},
{
"Name": "Bólido Comidas preparadas",
"City": "Madrid",
"Country": "Spain"
},
{
"Name": "Bon app'",
"City": "Marseille",
"Country": "France"
},
{
"Name": "Bottom-Dollar Marketse",
"City": "Tsawassen",
"Country": "Canada"
},
{
"Name": "Cactus Comidas para llevar",
"City": "Buenos Aires",
"Country": "Argentina"
},
{
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Chop-suey Chinese",
"City": "Bern",
"Country": "Switzerland"
},
{
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
},
{
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
},
{
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
},{
"Name": "Bon app'",
"City": "Marseille",
"Country": "France"
},
{
"Name": "Bottom-Dollar Marketse",
"City": "Tsawassen",
"Country": "Canada"
},
{
"Name": "Cactus Comidas para llevar",
"City": "Buenos Aires",
"Country": "Argentina"
},
{
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Chop-suey Chinese",
"City": "Bern",
"Country": "Switzerland"
},
{
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
},
{
"Name": "Bottom-Dollar Marketse",
"City": "Tsawassen",
"Country": "Canada"
},
{
"Name": "Cactus Comidas para llevar",
"City": "Buenos Aires",
"Country": "Argentina"
},
{
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Chop-suey Chinese",
"City": "Bern",
"Country": "Switzerland"
},
{
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
}
]
}
If you want do real-time application, nodejs + socket.io (websocket) will best fit your needs. Here is an example of how to use socket.io(websocket lib):
https://socket.io/get-started/chat/
In addition, any ajax calls will takes time, can easily take more then one second. Therefore, real-time web application need to use websocket instead of transitional ajax call.

Unique counter in ng-repeat

I have this table with 15 row.
Plunkr
Code:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp">
<table ng-controller="myCtrl" border="1">
<tr ng-repeat="item in myObj">
<td>{{$index +1}}</td>
<td>{{item.Name}}</td>
</tr>
</table>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.myObj = [ {
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
},
{
"Name": "Ana Trujillo Emparedados y helados",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Antonio Moreno Taquería",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Around the Horn",
"City": "London",
"Country": "UK"
},
{
"Name": "B's Beverages",
"City": "London",
"Country": "UK"
},
{
"Name": "Berglunds snabbköp",
"City": "Luleå",
"Country": "Sweden"
},
{
"Name": "Blauer See Delikatessen",
"City": "Mannheim",
"Country": "Germany"
},
{
"Name": "Blondel père et fils",
"City": "Strasbourg",
"Country": "France"
},
{
"Name": "Bólido Comidas preparadas",
"City": "Madrid",
"Country": "Spain"
},
{
"Name": "Bon app'",
"City": "Marseille",
"Country": "France"
},
{
"Name": "Bottom-Dollar Marketse",
"City": "Tsawassen",
"Country": "Canada"
},
{
"Name": "Cactus Comidas para llevar",
"City": "Buenos Aires",
"Country": "Argentina"
},
{
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
},
{
"Name": "Chop-suey Chinese",
"City": "Bern",
"Country": "Switzerland"
},
{
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
}]
});
</script>
</body>
</html>
and here's the output:
Let's say I delete an object in the array, total row will be 14 and when I add an object into array, it will return to 15. That was expected.
Now what I want to do is, WITHOUT using controller and everything is done in the VIEW; after I remove an object and add it back, I want the row will shown as 16 and not 15. I mean $index should be increasing and not decreasing.
I have done it with controller but is it possible to do it with only in the view?
Thanks for your time and any help in advance.
You could "soft-delete" the row. Instead of actually removing it from the array, you can simply mark it as deleted. Then the count will continue to increase as you add items:
function softDelete(item) {
item.deleted = true;
}
I don't know why you wouldn't want to use the controller, but this could also be done in the view:
<tr ng-repeat="item in myObj" ng-click="item.deleted = true" ng-hide="item.deleted">
<td>{{$index +1}}</td>
<td>{{item.Name}}</td>
</tr>
Here is a plunker demonstrating:
https://plnkr.co/edit/yuAIXn5Pe9YbwxXW2qih?p=preview

Categories

Resources