Showing previously selected items in drop down menu - html

Using angular and angular-xeditable I have a drop down menu with a number of options from which to select in the 'amenities' array.
Once I save the selections from the drop down and saved them, I want to make it possible for the user to come back to the page and edit previously selected items.
HTML:
<select multiple class="w-select am-dropdown" size="12" data-ng-model="Amenities"
data-ng-options="amenity.amenity for amenity in amenities" required=""></select>
JS:
$scope.amenities = [{amenity: coffee}, {amenity: beer}, {amenity: parking}];
$scope.Amenities = [];
$scope.selectedAmenities = [coffee, beer];//these are amenities saved in the
database that I want to be able to show as selected using the editable form

Have a case as same as this
Add $scope.$watch to put selected value to $scope.selectedValues as below
$scope.$watch('selectedAmenities ', function (nowSelected) {
$scope.selectedValues = [];
if (!nowSelected) {
return;
}
angular.forEach(nowSelected, function (val) {
$scope.selectedValues.push(val.amenity.toString());
});
});
And then use it like below:
select multiple ng-model="selectedValues" class="w-select am-dropdown" size="12" >
<option ng-repeat="amenity in amenities" value="{{amenity.amenity}}" ng-selected="{{selectedValues.indexOf(amenity.amenity)!=-1}}">{{amenity.amenity}}</option>
</select>
Full code at Plunker
Hope it helps you.

do u mean this?
var m = angular.module('m', []).controller('c', ['$scope',
function($scope) {
$scope.avilibleValues = ['a1', 'a2', 'a3', 'a4', 'a5'];
$scope.selected = [];
$scope.last = 'a1';
$scope.selecting = 'a1';
$scope.select = function(it) {
console.log('select:' + it);
$scope.selecting = it;
};
$scope.change = function() {
console.log($scope.last);
$scope.last && $scope.selected.push($scope.last);
$scope.last = $scope.selecting;
};
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="script.js"></script>
<div ng-app="m">
<div ng-controller="c">
<div class="row">
<label>seleted:</label>
<div>
<p ng-repeat="it in selected">
<a ng-click="select(it)">{{it}}</a>
</p>
<div>
</div>
<div class="row">
<label>selet</label>
<select ng-model="selecting" ng-options=" i for i in avilibleValues" ng-change="change()"></select>
</div>
</div>
</div>
<p>
selecting:{{selecting}}
<p>
selected:{{selected}}
<p>
last:{{last}}
<p>
</div>
</div>

Related

How can I make my list save even after the page is reloaded

this is my whole thing I just want my comments to help Please.
<!DOCTYPE html>
<html>
<head>
<title>Forum</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="pixel">
<br>
<br>
<div class="blackBox">
<p class="white">| <a class="white" href="file:///home/chronos/u-885ea83f13a0b735b4185df5cf6edb6dafb04e43/MyFiles/HTML%20Final/index.html">Home</a> | <a class="white" href="file:///home/chronos/u-885ea83f13a0b735b4185df5cf6edb6dafb04e43/MyFiles/HTML%20Final/textboxPhase.html">Textbox Phase</a> | <a class="white" href="file:///home/chronos/u-885ea83f13a0b735b4185df5cf6edb6dafb04e43/MyFiles/HTML%20Final/breakoutPhase.html">Breakout Phase</a> | <a class="white" href="file:///home/chronos/u-885ea83f13a0b735b4185df5cf6edb6dafb04e43/MyFiles/HTML%20Final/goatPhase.html">Goat Cage Phase</a> | <a class="white" href="file:///home/chronos/u-885ea83f13a0b735b4185df5cf6edb6dafb04e43/MyFiles/HTML%20Final/forum.html">Forum</a> |</p>
</div><br>
<br>
<h1>Welcome to The "There is no Game" Forum</h1>
<div class="container">
<h2>Leave us a comment</h2>
<form>
<textarea id="" placeholder="Add Your Comment" value=" "></textarea>
<div class="btn">
<input id="submit" type="submit" value="Comment"> <button id="clear">Clear</button>
</div>
</form>
<div class="comments">
<h2>Comments</h2>
<div id="comment-box">
<ul>
<li>WELCOME</li>
</ul>
</div>
</div>
</div>
<script type="text/javascript">
const field = document.querySelector('textarea');
const backUp = field.getAttribute('placeholder')
const btn = document.querySelector('.btn');
const clear = document.getElementById('clear')
const submit = document.querySelector('#submit')
// const comments = document.querySelector('#comment-box')
const comments = document.getElementById('comment-box');
// array to store the comments
const comments_arr = [];
// to generate html list based on comments array
const display_comments = () => {
let list = '<ul>';
comments_arr.forEach(comment => {
list += `<li>${comment}<\/li>`;
})
list += '<\/ul>';
comments.innerHTML = list;
}
clear.onclick = function(event){
event.preventDefault();
// reset the array
comments_arr.length = 0;
// re-genrate the comment html list
display_comments();
}
submit.onclick = function(event){
event.preventDefault();
const content = field.value;
if(content.length > 0){ // if there is content
// add the comment to the array
comments_arr.push(content);
// re-genrate the comment html list
display_comments();
// reset the textArea content
field.value = '';
}
}
</script><br>
<br>
<br>
<div class="as-console-wrapper">
<div class="as-console"></div>
</div>
</div>
</div>
</body>
</html
I know this code works but every time I reload it the comments disappear and I want it to save and all the code is too complex for my new code brain to understand. This is for my website and it's a basic wiki on how to beat there is no game, the old one. I need to turn this into my professor and wanted to add something extra but I want it so if I publish this the comment section isn't just a stupid gimmick.

ng-change sends different values on chosen select

Dears, I am using a chosen select drop down list in select tag, and I added an ng-change tag to bind it to a function. Also I made a console.log to monitor the sent key. I found that when page load and select one item from the list it works good and sends the correct key. BUT when I select the same item it sends the key of the next item in the list. any help how to fix it
HTML
<div class="form-group">
<select ng-change="GetUsersList(j)" data-placeholder="Choose a Job"
ng-model="j" id="chosenValueID" class="chosen-select"
tabindex="1"
ng-options="j.JobKey as j.JobDesc for j in jobs">
</select>
<!--<select ng-change="GetUsersList(job)" data-placeholder="Choose a Job" ng-model="job" id="chosenValueID" class="chosen-select" tabindex="2">
<option ng-repeat="j in jobs track by $index" value="{{j.JobKey}}">{{j.JobDesc}}</option>
</select>-->
</div>
Angularjs
$scope.GetUsersList = function (job) {
//var jobkey1 = $("#chosenValueID").chosen().val();
//$scope.jobkey2 = jobkey1.substring(7);
console.log(job);
$scope.ShowLoading('slow');
FieldsSecuritySrv.GetUsersList("FieldsSecurity/GetUsersList?hospid=" + $scope.hospitalid + '&jobkey=' + job).then(function (response) {
$scope.UsersList = (response.data);
GetFieldsList();
GetCategriesList();
GetPatientSheets();
$scope.HideLoading('slow');
})
}
Thanks in Advance
No idea how you can bind jobs but can try ...
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
$scope.count = 0;
$scope.jobs=[
{JobKey : "1", JobDesc : "IT"},
{JobKey : "2", JobDesc : "Sales"},
{JobKey : "3", JobDesc : "Marketing"}
];
$scope.GetUsersList = function(job){
alert(job);
}
}]);
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<div class="form-group">
<select ng-change="GetUsersList(j)" data-placeholder="Choose a Job" ng-model="j" id="chosenValueID" class="chosen-select" tabindex="1" ng-options="j.JobKey as j.JobDesc for j in jobs"> </select>
</div>
</div>
</body>
</html>
your j variable in HTML is somehow messy, it is indexed for ng-repeat and used as ng-modal as well, so $scope.j is also created :(
<select ng-change="GetUsersList()" data-placeholder="Choose a Job" ng-model="selectedJob" id="chosenValueID" class="chosen-select"
tabindex="1" ng-options="j.JobDesc for j in jobs"></select>
In angular:
//initialize the way u like
$scope.selectedJob = $scope.Jobs[0];
$scope.GetUsersList = function () {
//var jobkey1 = $("#chosenValueID").chosen().val();
//$scope.jobkey2 = jobkey1.substring(7);
console.log($scope.selectedJob.jobKey);
$scope.ShowLoading('slow');
FieldsSecuritySrv.GetUsersList("FieldsSecurity/GetUsersList?hospid=" + $scope.hospitalid + '&jobkey=' + $scope.selectedJob.jobKey).then(function (response) {
$scope.UsersList = (response.data);
GetFieldsList();
GetCategriesList();
GetPatientSheets();
$scope.HideLoading('slow');
})
}

Angularjs Parse value from input box to controller

I have an input box of quantity that the user can change it's value by clicking the - and + buttons. The problem is that if I entre the value directly in the input box I can parse this value to my controller. If I change the value by clicking the buttons + or - I can not parse the value to controller, it keeps always the old value. Can anyone help me?
<button
onclick="var result = document.getElementById('qty'); var qty = result.value; if( !isNaN( qty ) && qty > 0 ) result.value--;return false;"
class="reduced items-count"
type="button"
>
<i class="fa fa-minus"> </i>
</button>
<input
type="text"
class="input-text qty"
title="Qty"
maxlength="12"
id="qty"
name="qty"
ng-init="qty='1'"
ng-model="qty"
>
<button
onclick="var result = document.getElementById('qty'); var qty = result.value; if( !isNaN( qty )) result.value++;return false;"
class="increase items-count"
type="button"
>
<i class="fa fa-plus"> </i>
</button>
And my controller,
$scope.$watch('qty', function (val) {
if (val) {
alert("qty2:" + val);
}
});
I don't quite understand what you are trying to implement, but you can use ng-click on your buttons to execute some function in your controller.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.qty = 0;
$scope.change = function(value) {
$scope.qty += value;
$scope.changed(); // execute after update
}
$scope.changed = function() {
/*
Do what you want after the update,
but $scope.qty is dynamic anyway
*/
//alert("qty: " + $scope.qty);
}
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<button ng-click="change(-1)" type="button">-</button>
<input type="number" ng-model="qty" ng-change="changed()" />
<button ng-click="change(1)" type="button">+</button>
<br/>{{qty}}
</div>
</div>
</body>
</html>
This is totally the wrong approach... never use dom methods in angular app like that.
Use ng-click to modify the ng-model property. Also always follow the golden rule of making sure you have a dot in ng-model
angular
.module('app', [])
.controller('Ctrl', function($scope) {
$scope.data = {
qty: 0
}
$scope.updateQty = function(amt) {
$scope.data.qty += amt
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
<button ng-click="updateQty(-1)">-1</button>
<input ng-model="data.qty">
<button ng-click="updateQty(1)">+1</button>
</div>

Unable to add value in string array in angularjs?

I'm trying to add value in students array, but below code isn't working.
js code is --
angular.module('formExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.students=[
'Scarlett Johansson','Jennifer Lawrence','Emma Stone','Kristen Stewart'
];
$scope.add = function(name){
$scope.students.push(name);
};
}]);
html code is --
<body ng-app="formExample">
<div ng-controller="ExampleController">
<p ng-repeat="stud in students">
{{stud}}
</p>
</div>
<input type='text' ng-model="name"/>
<button ng-click="add(name);">add</button>
</body>
Your button and input are not included within the div ng-controller="ExampleController" scope.
<body ng-app="formExample">
<div ng-controller="ExampleController">
<p ng-repeat="stud in students">
{{stud}}
</p>
<input type='text' ng-model="name"/>
<button ng-click="add(name);">add</button>
</div> <!-- close div here -->
</body>
You need to push into $scope.students array, as there is no students array defined in the scope:
$scope.add = function(name) {
$scope.students.push(name);
};
$scope.add = function(name) {
$scope.students.push(name);//
};

Error when creating Filterable Elements in Jquery mobile

I hv done a program for filterable element using Jquery Mobile
When search for the particular character in the search box its displaying all the characters and names instead of the particular character I have searched
The output I got is this
**Here is my coding**
<link rel="stylesheet" href="../css/jquery.mobile-1.4.3.min.css">
<script src="../js/jquery-1.11.1.min.js"></script>
<script src="../js/jquery-ui.js"></script>
<script src="../js/jquery.mobile-1.4.3.min.js"></script>
<div data-role="page" id="pageone">
<div data-role="header">
<h1> Names </h1>
</div>
<div data-role="main" class="ui-content">
<form class="ui-filterable">
<input id="myFilter" data-type="search" placeholder="Please type here">
</form>
<ul data-role="listview" data-filter="true" data-input="#myFilter" data-
autodividers="true" data-inset="true">
<li data-filtertext="hello">Abdul</li>
<li>Alvin</li>
<li>Bob</li>
<li>Balmer</li>
<li>Nixon</li>
<li>Mitchelle</li>
<li>william</li>
<li>vincent</li>
<li>domney</li>
<li>Brad</li>
<li>Tarvin</li>
<li>Pamela</li>
<li>Jenney</li>
<li>Rose</li>
<li>Pepe</li>
<li>Ronaldo</li>
<li>Messi</li>
<li>Kroos</li>
<li>Klose</li>
<li>Neymar</li>
<li>Fabrigas</li>
<li>Iniesta</li>
<li>Cavani</li>
<li>Silva</li>
<li>Beckham</li>
</ul>
</div>
</div>
By default, jQM filters for items that contain the search text rather than items that start with the search text. For a starts with filter, you will need to use the filterable widget's filterCallback option:
$(document).on("pagecreate", "#pageone", function(){
$("#myList").filterable('option', 'filterCallback', startsWithSearch);
});
function startsWithSearch( idx, searchValue ) {
if (searchValue && searchValue.length > 0){
var theListItems = $("#myList li");
var text = theListItems.eq(idx).text().toLowerCase();
var filttext = theListItems.eq(idx).data("filtertext") || '';
filttext = filttext.toLowerCase();
//if either text or filtertext starts with searchvalue, return false
if( text.lastIndexOf(searchValue, 0) === 0 || filttext.lastIndexOf(searchValue, 0) === 0){
return false;
} else {
return true; //filter this one out
}
} else {
return false;
}
}
On pagecreate I am setting the filterCallback option to a javascript function called startsWithSearch. That function checs to see if a searchValue has been entered. If so, it gets the text and data-filtertext of the current listitem and checks if either start with the searchValue. Any items that don't start with the value are filtered out by returning true.
Here is a working DEMO