I have a collection of rooms in my $scope, with the structure:
$scope.model.rooms = [
{ RoomNumber: 1, Type: 'Single'},
{ RoomNumber: 3, Type: 'Single'},
{ RoomNumber: 5, Type: 'Double'},
{ RoomNumber: 6, Type: 'Single'},
{ RoomNumber: 12, Type: 'Double'}
];
I then create a 5x5 grid on my view as follows, and want to have the correct room type selected on the drop down, but I don't know how to do this binding properly.
<div data-ng-repeat="y in [1, 2, 3, 4, 5]" class="hotelRooms">
<div data-ng-repeat="x in [1, 2, 3, 4, 5]">
<h6>
Room #{{(y-1)*5+x}}
<select id="binType{{(y-1)*5+x}}" data-ng-model="model.rooms[/*WHAT DO I PUT HERE?*/].Type" data-ng-change="setRoomType((y-1)*5+x)">
<option value="Single">Single</option>
<option value="Double">Double</option>
</select>
</h6>
<div id="bin{{(y-1)*5+x}}" data-droppable="{{(y-1)*5+x}}" data-drop="addToRoom">
<div id="{{p.Id}}" data-draggable="" data-ng-repeat="p in model.participants | filter:{RoomNumber:(y-1)*5+x}">{{p.Name}}</div>
</div>
</div>
</div>
The array index isn't the same as the RoomNumber property.
I find it much more intuitive if you put all of your behavior into your controller. Your view just binds to those behaviors (a la MVVM). You get more control over your code this way and aren't at the mercy of {{binding syntax}} and its limitations.
I removed the drag and drop stuff, but here's how I got it to work. Notice you don't need the ngChange directive in the select because this way it's bound right to the room model Type property. So they stay in sync when you change it.
Here's a fiddle with the working code, or just see below: http://jsfiddle.net/854wk/7/
Controller:
function MyController($scope) {
$scope.calcBin = calcBin;
$scope.getRoomByBin = getRoomByBin;
function calcBin(row, col) {
return ((row - 1) * 5) + col;
};
function getRoomByBin(row, col) {
var bin = calcBin(row, col);
var foundRoom;
$scope.model.rooms.some(function(room) {
if (room.RoomNumber === bin)
{
foundRoom = room;
return true; // break
}
});
return foundRoom;
}
}
View:
<div ng-app>
<div ng-controller="MyController">
<div data-ng-repeat="y in [1, 2, 3, 4, 5]"
class="hotelRooms">
<div data-ng-repeat="x in [1, 2, 3, 4, 5]">
<h6>
Room #{{calcBin(y,x)}}
<select id="binType{{calcBin(y,x)}}"
data-ng-model="getRoomByBin(y,x).Type" >
<option value="Single">Single</option>
<option value="Double">Double</option>
</select>
Room Type is: {{getRoomByBin(y,x).Type}}
</h6>
</div>
</div>
</div>
Related
I am fetching some records which are already assigned to users with checked checkboxes. I want to assign some additional ones that are coming as unchecked checkboxes. How can I show checked checkboxes first and then show whatever is unchecked after them?
You can use angular filters to show checked checkboxes first and whatever is unchecked, show it after them
try this:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.data = [
{id: 1, value: 1},
{id: 2, value: 0},
{id: 3, value: 1},
{id: 4, value: 0},
{id: 5, value: 0}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<h2>Checked</h2>
<div ng-repeat="item in data | filter : { value : 1}">
{{item.id}} : <input type="checkbox" ng-checked="item.value=='1'"/>
</div>
<h2>Unchecked</h2>
<div ng-repeat="item in data | filter : { value : 0}">
{{item.id}} : <input type="checkbox" ng-checked="item.value=='1'"/>
</div>
</div>
</div>
Hope it helps..
I have the following:
http://jsfiddle.net/Qgmz7/119/
I am trying to keep track of what is selected and what is not, how do I go about doing this?
<div ng-app>
<div ng-controller="cCtrl">
<select multiple ng-model="campaign" ng-options="c.ID as c.Name for c in campaigns">
<option ng-selected="c.Selected" value="">-- choose campaign --</option>
</select>
<ul>
<li ng-repeat="item in campaigns">{{item.Name}} - {{item.Selected}}</li>
</ul>
</div>
</div>
function cCtrl($scope) {
$scope.campaigns = [
{Selected:true, ID: 1, Name:"James"},
{Selected:false, ID: 2, Name:"James"},
{Selected:true, ID: 3, Name:"James"},
];
}
Hi try this http://jsfiddle.net/a63k106s/
function cCtrl($scope) {
$scope.campaigns = [
{Selected:true, ID: 4, Name:"James"},
{Selected:false, ID: 2, Name:"James"},
{Selected:true, ID: 3, Name:"James"},
];
$scope.$watch('campaign', function (nowSelected) {
angular.forEach($scope.campaigns, function(campaign) {
campaign.Selected = nowSelected.indexOf(campaign.ID) >= 0
});
});
}
I have a view where a filter is being used filter: test_id.It is working absolutely fine.What I want to do is if the search filter does not return any data can I call a function in the controller at that instant of time when the filter returns null or no data in the list is shown as per search.
<input type="text" id="test_id" ng-model="test_id" value=""/>
<div class="test_list">
<ul>
<p> testID :{{test_id}}</p>-->
<li ng-repeat="test in test_list | filter: test_id">
{{test}}
</li>
</ul>
</div>
Try this.
In your controller
$scope.filteredData = $scope.data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // dummy data
$scope.applyFilter = function () {
$scope.filteredData = $scope.$eval('data | filter: filterWith');
if (!$scope.filteredData || !$scope.filteredData.length) {
// no-data.
// do your stuff.
}
}
In your markup
<input type="text" ng-model="filterWith" ng-change="applyFilter()">
<div data-ng-repeat="item in filteredData" >
{{item}}
</div>
Please look at http://jsfiddle.net/mahbub/sbNty/4/
<div ng-app="">
<div ng-controller="Ctrl">
<ul>
<li ng:repeat="status in statuses"><label><input type="checkbox" data-ng-model="status.type" data-ng-true-value="closed" data-ng-false-value="{{status.type}}" />{{status.type}}</label></li>
</ul>
</div>
</div>
As you can see the label of the checkboxes are printed based on the status type from the JSON. Now on unchecking, the label becomes false. I must be missing some correct way to get back to the originial label text upon unchecking the checkbox.
I mean when I uncheck, the label needs to be "open" or whatever it was initially.
Any help is greatly appreciated.
Thanks
Finally i did it using ngInit and setting a different variable within the scope object. See the demonstration here http://jsfiddle.net/mahbub/sbNty/5/
<div ng-app="">
<div ng-controller="Ctrl">
<ul>
<li ng:repeat="status in statuses"><label><input ng-init="status.oldStat=status.type" type="checkbox" ng-model="value" ng-click="selectV(value,this)">{{status.type}}</label></li>
</ul>
</div>
</div>
Controller :
'use strict';
function Ctrl($scope) {
$scope.statuses = [{
id: 1,
type: 'open'},
{
id: 2,
type: 'open'},
{
id: 3,
type: 'new'},
{
id: 4,
type: 'closed'},
{
id: 5,
type: 'open'},
{
id: 6,
type: 'new'},
{
id: 7,
type: 'open'}
];
$scope.selectV = function(val, stat) {
if (val) {
stat.status.type = "closed";
} else {
stat.status.type = stat.status.oldStat;
}
}
}
I'm building a CRUD and I want to some model to be linked to others. So on some update/create template I need to add some other model and save their id when submitting the form.
So I did a basic form that is served by my backend. Here is the part where I try to display the linked model:
<div ng-repeat="item in otherItems">
<select ng-model="item" name="item" >
<option value="1" ng-selected="item">
<option value="2" ng-selected="item">
<option value="3" ng-selected="item">
</select>
<a class="remove" ng-click="removeRelated(item)">remove</a>
</div>
<a ng-click="addRelated()"><i class="icon-plus"></i></a>
Note: otherItems could be empty at the beginning (when doing a create or when an item is not linked to any other related model's item).
So when one press add/remove it will trigger a controller's function:
$scope.addRelated = function (){
if (typeof $scope[otherItems]=="undefined")
$scope[otherItems] = new Array();
$scope[otherItems].push($scope[otherItems].length);
};
$scope.removeRelated = function (item){
console.debug(item);
var idx = $scope[otherItems].indexOf(item);
if (idx !== -1) {
$scope[otherItems].splice(idx, 1);
}
};
My problem is when I save I get the position of item in items (so it's always 0, 1, 2...) I won't have an array of selected ID.
I guess there is something wrong with my addRelated maybe. What am I doing wrong?
Here is a screenshot to understand the idea since I may not be very clear:
So something like this? http://plnkr.co/edit/6eqWL5
The markup..
<body ng-controller="MainCtrl">
<div ng-repeat="otherItem in otherItems">
<select ng-model="otherItem.selectedItem" ng-options="item for item in otherItem.items"></select>
<a ng-click="removeOtherItem($index)">remove</a>
</div>
<a ng-click="addOtherItem()">add</a>
<hr/>
Selected:
<ul>
<li ng-repeat="otherItem in otherItems">
otherItem[{{$index}}].selectedItem = {{otherItem.selectedItem}}
</li>
</ul>
</body>
The code
app.controller('MainCtrl', function($scope) {
$scope.otherItems = [
{
selectedItem: null,
items: [1, 2, 3]
},
{
selectedItem: null,
items: [4, 5, 6]
}
];
$scope.addOtherItem = function(){
$scope.otherItems.push({ items: [7, 8, 9]});
};
$scope.removeOtherItem = function(index) {
$scope.otherItems.splice(index, 1);
};
});
Sorry if it's off from what you're asking for... the question was a little vague, so I'm guessing at some of the functionality.