I am new to Angular and Mongo, and am hoping to use Angular js to pull data from a Mongo database to populate a div on a single-page website I'm building. What data is pulled will depend on what option a user selects in a dropdown option on a form. There are a Lot of options out there for Angular, and I was hoping that someone with more experience with it could point me in the right direction. Below are the relevant bits of my code:
<body ng-app="">
<div id='ad'> </div><!-- to be populated with data from MongoDB -->
<select id='climateZone' ng-model="zone" >
<option value="z3">3</option>
<option value="z4">4</option>
<option value="z5">5</option>
<option value="z6">6</option>
<option value="z7">7</option>
<option value="z8">8</option>
<option value="z9">9</option>
<option value="z10">10</option>
</select>
<button id='Go'><h2>Go!</h2></button>
I want it so that when a user selects one of these options, and presses the 'go' button, the 'ad' div will be populated with option-specific information from the database.
I hope that's all clear; any help is appreciated!
As far as I know you can't make HTTP request directly to MongoDB. Instead you need a server-side implementation that communicates with the database for you. If you are using Node for the back-end you can take a look on Mongoose: http://mongoosejs.com/
Mongoose is one of the many libs for MongoDB, you define you models, make queries, post data, and so on. Let's say I want to get the data based on a parameter. At first you need a model for your data:
var Cat = mongoose.model('Cat', { name: String });
Then you can start operating on top of the DB connection. The example bellow should bring you all cats named "Toddy":
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myDB');
Cat.find({name: 'Toddy'}, function (err, result) {
if (err) return console.error(err);
console.log(result);
});
That said, you can use the ngChange and ngModel directives to put the value of the combo on a variable and react properly when it changes, like so:
<select id='climateZone' ng-model="zone" ng-change="loadData()">
<option value="z3">3</option>
<option value="z4">4</option>
<option value="z5">5</option>
<option value="z6">6</option>
<option value="z7">7</option>
<option value="z8">8</option>
<option value="z9">9</option>
<option value="z10">10</option>
</select>
Being both "zone" and "loadData()" members of your $scope.
Related
I have two reports: Checklist and Location. Depending on which report is chosen decides the action of the form of what path to take. I also have a select that ask the user whether or not they want to print automatically, preview as a pdf, or open in excel.
<select name="Format" required>
<option selected value="">Select Format</option>
<option value="print">Print</option>
<option value="pdf">Preview</option>
<option value="xls">Excel</option>
</select>
I am curious of how I am suppose to forward this information in order to do the queries and process the choice the user is requesting. I am sure people do things like this all the time. Would anyone like to share any examples or advice - stepping stones to get me on the right path? Thanks in advance!
Tagged below are all the languages I am using that I could use to achieve this goal.
If you are just trying to send data to a different page, then you can use query parameters for that. One option is to build the query parameters as users update the dropdown:
var select = $('select[name=Format]');
function buildUrl(option) {
return 'some/endpoint?option=' + option;
}
select.change(function() {
var value = $(this).val();
var url = buildUrl(value);
console.log('URL to call:', url);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select name="Format" required>
<option selected value="">Select Format</option>
<option value="print">Print</option>
<option value="pdf">Preview</option>
<option value="xls">Excel</option>
</select>
and then you can use JavaScript or CF to detect the presence (or absence) of the query parameters to meet your app's requirements.
Hope this helps.
I have tried using pdfmake on my reports. It has the option to generate a report with Excel, Print, or download pdf. All you have to do is pass a json array. You can read the documentation on github. Here's the link: PDFMake
This might not be the best answer but it would be a stepping stone for you.
So I have a mongodb database that will house several department names. I want to http.get() the names from my nodejs server. From that get, I want to populate the names into a select box. The important part of this question isn't the node/mongo part, its the js/html part of, I have the response from the get, how do I put it into a select box? Here is my code:
The part of code:
$http.get('http://localhost:8000/getData').then(function(response) {
// code to place here
});
And the html part:
<select placeholder="Select a User">
<option ng-model="selectUser"></option>
</select>
First, you have to put the data from the response object into a controller property like this:
$http.get('http://localhost:8000/getData').then(function(response) {
$scope.options = response.data;
});
And then you can use the ng-options directive from angular to populate the select box with values like this:
<select placeholder="Select a User" ng-model="selectUser" data-ng-options="option.Member for option in options">
</select>
Somthing like:
$scope.values; //values from server
$http.get('http://localhost:8000/getData').then(function(response) {
console.log(response.data); //make sure that this is what you are looking for
$scope.values = response.data;
});
<select placeholder="Select a User" data-ng-options="option.name for option in values">
</select>
When a user logs in to my app, it loads the previous UI setup that they were using. To switch between interfaces there is a dropdown that lists all the options that specific user is authorized to use.
I would like the selected option in that list to reflect the previous UI when the page is loading but I'm having trouble coming up with how to set the selected <option>
Here is some example code:
//User object from DB
var user = {
username: admin,
previousUI: 'Build',
authorizedUI: [{title:'Default'}, {title:'Edit'}, {title:'Build'}]
}
html:
<form class="navbar-form">
<label>System: </label>
<select (change)="systemSelect($event.target.value)">
<option *ngFor="let system of user.authorizedUI" [value]="system.title">
{{system.title}}
</option>
</select>
</form>
In Angular2 how do I set the selected parameter on the option to reflect user.previousUI?
I tried adding this:
<option *ngFor="let system of user.authorizedUI" [value]="system.title" [selected]="system.title === user.previousUI">
But that didn't seem to do it.
One way would be
<select [ngModel]="user?.previousUI" (change)="systemSelect($event.target.value)">
I am using play framework for the first time.
I want to update the web page when a value is selected from drop down with out refreshing the web page.
Consider the following example:
<select>
<option value= "Apple"> Apple </option>
<option value = "Banana"> Banana </option>
</select>
When a value is selected from drop down it should be posted to server. Then server should return some information based on the value it got. Now we should display the content corresponding to the value selected from drop down with out page refresh.
I didn't find a way to implement this using play framework. Conventionally we can do this by hiding a div initially and when a value is selected from drop down we can add content (fetched from back end) to the div and show it. I didn't understand how to implement the server side part. In play framework, controller generally returns Result type. How to return a Json value on a request in play framework ?
Can anyone please suggest other ideas ??
Thanks
A solution using jQuery (see change() and load()):
<select id="select">
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
</select>
<div id="result"></div>
<script>
$('#select').change(function() {
$('#result').load('/foo/bar?fruit=' + $(this).val());
});
</script>
EDIT: To address the JSON/server-side part of the question have a look at ScalaJsonHttp (or JavaJsonActions).
I'm trying to build a custom multi select form input () so that I can send an array of items through a form. The format of the data needs to be native to rails, like:
item[tags][]=1,5,7
Normally I would do this by coding:
<select multiple="multiple" name="item[tags][]">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
But as I mentioned earlier, this information needs to be hidden because I am customizing the look of the control. Any ideas how I could pass data in this way using angular?
(please let me know if my explanation is clear enough)