angularjs post web service dropdown - html

I have created a web application with Eclipse where I use AngularJS and REST GET web services. Through the web services I query my MySQL database for data and send the values to my html page through controlles http GET controllers.
I successfully show dropdown lists with the database values in my HTML page. Now I want to send the selected items for each dropdown list back to a java page so that I can create a new query that will produce some new data. But I don't know how I can do this second part, can someone help me with it?
Thanks in advance!
this is part of my html page
<div id="three" ng-controller="mycontroller">
<table class="table table-hover">
<tbody>
<select ng-model="selecteditem">
<option ng-repeat="item in items">
{{item.itemname}}
</option>
</select>
</tbody>
</table>
<b>You selected: {{selecteditem}}</b>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('showitems', function($scope, $http) {
$http.get('http://localhost:8080/myproject/REST/WebService_items/GetItems').
success(function(data) {
$scope.items = data;
})
.error(function() {
$scope.items = "error in fetching data";
});
});
</script>

Thanks for both answers! My main issue is not how to show the items in the drop down lists, I have done that. I want to send the selected items to a Java page of my project through a REST web service function, so I don't know how to write the angularjs code in my HTML page that will send push/post the items in the web service's URL.

<label>example</label>
<md-select id="example" ng-model="vm.list.example" required>
<md-option ng-repeat="unit in vm.list" value="{{example.id}}">{{example.name}}</md-option>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="three" ng-controller="mycontroller">
<table class="table table-hover">
<tbody>
<select ng-model="selecteditem">
<option ng-repeat="item in items">
{{item.itemname}}
</option ng-click="callTheFunction(item.itemname})">
</select>
</tbody>
</table>
<b>You selected: {{selecteditem}}</b>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('mycontroller', function($scope, customService) {
$scope.items = [{itemname:'apple'},{itemname:'mango'}]
$scope.callTheFunction=function(itemSelected){
customService.restCallToJava(itemSelected)
}
app.service('customService',['$http', function($http) {
var restCallToJava= function(sensorObj){
return $http.post('/api',sensorObj);
}
}])
</script>

Related

how to pass value from angularjs template to controller?

HTML code:
<tr ng-repeat="x in products">
{{x.product_name}}
i just want to get this {{x.product_name}}, in angularjs scope.
means, if i click on any of the button, it should pass the x.product_name to angularjs in controller. How to do this?
I am new to angularjs.
Thank you in advance.
Put your code inside ng-app and assign ng-controller.
Make sure to add products property on $scope of controller
Make sure each product contains product_name
angular.module('myApp', [])
.controller('myController', ['$scope', function myController($scope) {
$scope.products = [{
product_name: 'cellphone'
}, {
product_name: 'laptops'
}, {
product_name: 'desktops'
}];
$scope.handleClick = (productName) => {
console.log(`${productName} clicked`);
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<table>
<tr ng-repeat="x in products">
<td>
{{x.product_name}}
</td>
</table>
</div>

How do I save multiple dropdown menus in an application relaunch?

I have a program that has multiple dropdown menus and I am running it as a chrome unpacked extension but I cannot get any of the dropdown menus to save after I reopen the app. How can I achieve this? I have already attempted some solutions but they have not worked. The dropdowns are in a table format.(Note: This is not all of my code but to make it more simple I only included two of my dropdowns). Thanks!
<!-- <head>
<link rel="stylesheet" href="styles.css">
<script src="java.js"></script>
</head> -->
<body>
<div class="imgcontainer">
<img src="images.jpeg" alt="Avatar" class="image">
</div>
<table id="t01">
<tr>
<th>Name</th>
<th>ID</th>
<th>In Use</th>
<th>Quantity</th>
<th>Connected to...
*Based on OUTPUT*</th>
<th>Comment</th>
</tr>
<tr><td id="master01">1/4 Inch Audio Cables</td>
<td>1-2</td>
<td>--</td>
<td>2</td>
<td><select align="center" id="master01dropdown">
<option value="option01">option01</option>
<option value="option02">option02</option>
<option value="option03">option03</option>
<option value="option04">option04</option>
<option value="option05">option05</option>
<option value="option06">option06</option>
<option value="Null">Null</option>
<option value="Not Defined">Not Defined</option>
<option value="Not in Use">Not in Use</option>
</select></td>
<td></td>
</tr>
<tr><td align="center" id="cable01">6 feet</td>
<td>1</td>
<td><input align="center" checked type="checkbox" name="void" value="In use"><br></td>
<td>1</td>
<td><select id="cable01dropdown">
<option value="option01">option01</option>
<option value="option03">option02</option>
<option value="option03">option03</option>
<option value="option04">option04</option>
<option value="option06">option05</option>
<option value="option07">option06</option>
<option value="Null">Null</option>
<option value="Not Defined">Not Defined</option>
<option value="Not in Use">Not in Use</option>
</select></td>
<td></td>
I added the suggested code and it still will not work. *Note: I am not very good with JavaScript.*
chrome.storage.sync.set({ items: 'master01,cable01,cable02'});
chrome.storage.sync.get('items', function(data) {
var items = data.items.split(',');
items[0] => (master01)
items[1] => (cable01)
items[2] => (cable02)
});
Store items in chrome storage:
Suppose you save comma separated items
chrome.storage.sync.set({ items: 'item1,item2,item3,item4'});
fetch it using:
chrome.storage.sync.get('items', function(data) {
var items = data.items.split(',');
// items[0] => item1
// items[1] => item2
});
I'll detail how to save and load an example options menu, containing a textbox and a checkbox. In your example, you'll want to use document.getElementById('master01dropdown').value in your save.
Setting up
First, create a file options.html in the root of the project, containing your form.
Next, make sure this options page is registered in your manifest.json:
"options_ui": {
"page": "options.html",
"chrome_style": true
}
At the bottom of your options.html, before </body>, include a JavaScript file:
<script src="js/options.js"></script>
Your form should also include a Save button somewhere with id save:
<button id="save">Save</button>
Saving
Inside your /js/options.js file, register the save() function to run when your save button is clicked:
document.getElementById('save').addEventListener('click', save);
Your save() function should use Chrome Storage to save every element from your form. For example, to save a textbox and a tickbox:
function save() {
var sitesToSave = document.getElementById('sites').value;
var displayPopup = document.getElementById('popup').checked;
chrome.storage.sync.set({
sites: sitesToSave,
popup: displayPopup
}, displaySavedMessage);
}
Loading
Inside your /js/options.js file again, register the load() function when your form is loaded:
document.addEventListener('DOMContentLoaded', load);
Now set those form fields from earlier with the saved values (and provide default values), again using Chrome Storage:
function load() {
chrome.storage.sync.get({
sites: 'supersecretsite.com',
popup: false
}, function(items) {
document.getElementById('sites').value = items.sites;
document.getElementById('popup').checked = items.popup;
});
}
Done! Settings can now be saved and loaded.
All code in this answer is from a very simple example Chrome Extension I made, which also has an accompanying detailed tutorial.

Adding a variable to a group of array

I'm using angular js to produce to add an array and display it as we go along. So far the given code adds an array to the list but it doesn't keep adding the array with the press of the function.
I'm quite new to angular and i think there's something I'm missing but right now I'm stuck.
The html code has a button linked which is supposed to carry out the function.
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = ["Testing Task 5.1", "Downloaded Task 5.1"];
$scope.addArray = function() {
$scope.records.unshift("saddfadffas")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="myCtrl" class="container">
<p>
<h3>Status: </h3>
<input type="text" ng-model="additem">
<button ng-click="addArray()">function</button>
</p>
<ul ng-repeat="x in records">
<li>{{x}}</li>
</ul>
</div>
You can resolve this issue by having the items in your array be keyed by their position rather than their value. To do this, you can use track by $index. Hope this helps!
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = ["Testing Task 5.1", "Downloaded Task 5.1"];
$scope.addArray = function() {
$scope.records.unshift("saddfadffas")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl" class="container">
<p>
<h3>Status: </h3>
<input type="text" ng-model="additem">
<button ng-click="addArray()">function</button>
</p>
<ul ng-repeat="x in records track by $index">
<li>{{x}}</li>
</ul>
</div>
Try push rather thanunshift? Sorry I am not at my computer right now so can't verify this, but I'm pretty sure this will solve the problem
EDIT: Perhaps your intention is
$scope.records.unshift($scope.additem)
rather than
$scope.records.unshift("saddfadffas")
but I am assuming you are perhaps running into an error we can't see from the excerpt you posted.

angular ui-sref on tr - link not visible

I placed ui-sref attribute in tr element.
<tr ui-sref="test" ng-repeat="d in data">
Everything runs well, but I would like the link to 'test' to be visible in the bottom of the browser when hover the row of the table. Is there a way to obtain it?
angular.module('myApp',[])
.controller('MyCtrl', function(){
this.model = {};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl as myCtrl">
<span ng-mouseover="myCtrl.model.mouseIsOver = true"
ng-mouseout="myCtrl.model.mouseIsOver = false">
Hover me
</span>
<div ng-if="myCtrl.model.mouseIsOver">
test
</div>
</div>
Not really related to ui-router but you can do this with built in directives in the ng main module.

Knockoutjs Options Binding with JSON data?

I am trying to list the options for a select tag from server with the help of knockout options binding. I have a PHP page which returns the JSON data which is pushed to a knockout observable array which is binded to the select tag. But somehow it is not working, please refer to the following code for reference:
HTML:
<div class="form-group">
<select class="form-control">
<option data-bind="options: Country_Names, optionsText: 'country_name'"></option>
</select>
</div>
JavaScript:
$(document).ready(function(){
function appModel(session_info){
/* Session Info related bindings are commented as they are working fine */
var self = this;
this.Country_Names = ko.observableArray();
// Bindings related to the batch processing
$(function(){
$.ajax({
url:"../api/master_list.php",
type:"get",
data:{mastertype: '1'},
cache:false,
success:function(country_list){
ko.mapping.fromJSON(country_list, {}, self.Country_Names);
}
});
});
};
$.ajax({
url:"../api/sessions.php",
type:"get",
data: {rqtype: '1'},
cache:false,
success:function(session_info){
var data = $.parseJSON(session_info);
if (data.status == 'Invalid_id'){
window.location.replace('../files/main.html');
} else {
ko.applyBindings(new appModel(session_info));
}
}
});
});
Sample JSON:
[{"country_name":"Albania"},{"country_name":"Chile"},{"country_name":"Cuba"}]
Question, Why are the options not listed in the select tag? Am i missing something obvious?
You are data-binding the option element, whereas you want to put the bindings on the select, like this:
<select class="form-control" data-bind="options: Country_Names, optionsText: 'country_name'">
</select>
The <option...> element is gone; the options will be generated by Knockout.
Note that the relevant documentation is very clear on how all this works. If you haven't already, I suggest (re-)reading it.
Or, see this full demo:
var data = [{"country_name":"Albania"},{"country_name":"Chile"},{"country_name":"Cuba"}];
ko.applyBindings({ Country_Names: data });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<select data-bind="options: Country_Names, optionsText: 'country_name'"></select>