$scope variable being copied, not referenced in Kendo-UI window - angularjs

I have a situation where I am passing a $scope variable into a Kendo-UI window, and any changes being made
All of this sits in one controller.
So in my main controller, I have a $scope.searchTerm variable which I initialise to "Enter search term"..
in my HTML, I call peopleSearch.open() as below on my main controller
<td><span>GSP</span><span ng-click="peopleSearch.open()" style="font-size:8pt;" class="glyphicon glyphicon-plus-sign"></span><td>
This simply opens this kendo-ui window declared below, and covered by the same controller:
<div kendo-window="peopleSearch" k-title="'Person Search'"
k-width="900" k-height="500" k-visible="false"
k-content="{ url: 'parts/peopleSearch.html' }"
k-on-open="peopleSearchVisible = true" k-on-close="peopleSearchVisible = false"
k-modal="true"></div>
The Kendo-UI window template, again, covered by the same controller is as below. Note that there is an input bound to $scope.searchTerm. When the window opens, the input is initialised with "Enter search term"...so it is obviously bound the $scope.searchTerm variable.
<section>
<div class="myAlerts">
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)" >{{alert.msg}}</alert>
</div>
<div>People Search</div><br/>
<div>{{searchTerm}}</div>
<input type="text" ng-model="searchTerm" style="width:400px;"</input><span> Enter surname or part of surname to search the Active Directory</span>
<br/><br/>
<button ng-click="performSearch()" class="form-control" style="width:400px;">Search</button>
<br/>
<br/>
<div>
<div>{{searchTerm}}</div>
<table class="table table-bordered table-condensed" style="cell-padding:0px;cell-spaing:0px;font-size:10pt;">
<thead>
<tr>
<th>Name</th>
<th>Initials</th>
<th>Title</th>
<td>Office</td>
<th>Email</th>
<th>Company</th>
<th>Department</th>
<th>Telephone</th>
<th>Mobile</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="people in peopleResults">
<td>{{people.displayName[0]}}</td>
<td>{{people.initials[0]}}</td>
<td>{{people.title[0]}}</td>
<td>{{people.physicalDeliveryOfficeName[0]}}</td>
<td>{{people.mail[0]}}</td>
<td>{{people.company[0]}}</td>
<td>{{people.department[0]}}</td>
<td>{{people.telephoneNumber[0]}}</td>
<td>{{people.mobile[0]}}</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</section>
However, when I start typing and press perform the search:
$scope.performSearch = function()
{
prPeopleService.findPeople($scope.searchTerm).then(function(people) {
$scope.peopleResults = people;
$scope.addAlert('success', $scope.peopleResults.length + ' people located');
},
function(response) {
$scope.addAlert('danger', 'There was an issue trying to look people up from the Active Directory...');
});
};
The $scope.searchTerm being passed in as a parameter is the initial "Enter Search Term"...and not the value being entered into the input field...which is bound to $scope.searchTerm....as if it took a copy to start with, then decided to detach itself.

Related

How Refresh a list after push? AngularJS/ DB

Hi Sir,
I would know how i can refresh my List after add , edit or delete operation with AngularJS .
i Post my Controller And my HTML code , it work but i must refresh all page for see the changes in DB. Ty for all request.
CONTROLLER:
angular.module('myApp').controller('controllerFilm', function(service , $scope ) {
var vm = this;
vm.allFilm = function() {
service.allFilm().then(
function(data){//success
vm.allFilm = data; // accounts list
},function(err){//error
console.log("errore di chiamata allFilm");
});
}
HTML:
<div ng-controller="controllerFilm as cf">
<button class="button" ng-click="cf.allFilm()">FILM MANAGER</button>
<br>
<br>
<div class="scroll">
<table class="blueTable">
<thead>
<tr>
<th>ID</th>
<th>TITLE</th>
<th>RELASE YEAR</th>
<th>LENGTH</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
</thead>
<tbody>
<tr ng-repeat =
"film in cf.allFilm | orderBy : '-filmId' | limitTo : 10">
<th>{{film.filmId}}</th>
<th>{{film.title}}</th>
<th>{{film.relaseYear}}</th>
<th>{{film.length}}</th>
<th align="center"> <button
ng-click="cf.editFilm(film)">EDIT</button></th>
<th align="center"> <button
ng-click="cf.deleteFilm(film)">DELETE</button></th>
</tr>
</tbody>
</table>
PS : i don't post my function edit and delete..
Although changes should automatically be applied as AngularJs has two way binding but sometimes, it does cause this issue. try calling below scope method after setting data to the list.
$scope.$apply();
P.S: This is a way of forcefully executing digest cycle to reflect changes right away.

Angular loop through table row and check for checked checbox

I have a table that has a list from database with checkbox on each row, checkbox will used for ex. during deletion of the record.
So what i am trying to achieve is that when i clicked the delete button, angular will loop each row in table and check the checkbox whether is checked, if yes the please proceed to delete. Dont have any idea how to do this. Someone please give some related example.
Here is my code
index.html
<button class="ui red labeled icon button right floated" type="button" data-content="Delete selected item(s)" id="delete" ng-click="deleteSeleted()"><i class="trash icon"></i>Delete</button>
<div class='container-table'>
<table class="ui fixed single line celled table striped sortable compact" style="width:2000px" id="mytable">
<thead>
<tr>
<th class="width-checkbox"><input type="checkbox" ng-model="matin.selectedAll" /></th>
<th class="width-120">Item</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in data">
<td><input type="checkbox" ng-checked="matin.selectedAll"></td>
<td>{{x.item}}</td>
</tr>
</tbody>
</table>
</div>
<tr ng-repeat="x in data">
<td><input type="checkbox" ng-model="x.selected"></td>
<td>{{x.item}}</td>
</tr>
Angular Js Code
for (var k = 0; k < $scope.data.length; k++)
{
if($scope.data[k].selected==true)
{
//Perform your desired thing over here
var val=$scope.data[k].item //to getData
}
}
Please find the fiddler link which i have created to select all the items in the table and on delete print which ever is checked https://jsfiddle.net/dfL1L944/3/
var appModule = angular.module('Module', []);
appModule.controller("DataController", function($scope) {
$scope.data = [{"name":"Alex"},{"name":"Juni"}]
$scope.deleteAll = false;
$scope.deleteSeleted = function(){
$scope.data.forEach(function(x) {
console.log(x.name);
});
}
$scope.selectedAll = function(){
$scope.data.forEach(function(x) {
if($scope.deleteAll){
x.deleted = true;
}
else{
x.deleted = false;
}
});
}
});
HTML Code
<div ng-app="Module"> <div ng-controller="DataController">
<button class="ui red labeled icon button right floated" type="button" data-content="Delete selected item(s)" id="delete" ng-click="deleteSeleted()"> <i class="trash icon"></i>Delete</button> <div class='container-table'> <table class="ui fixed single line celled table striped sortable compact" style="width:200px" id="mytable"> <thead>
<tr>
<th width="20px">
<input type="checkbox" ng-model="deleteAll" ng-click="selectedAll()" /></th>
<th>Item</th>
</tr> </thead> <tbody>
<tr ng-repeat="x in data">
<td width="2px"><input type="checkbox" ng-checked="x.deleted"></td>
<td>{{x.name}}</td>
</tr> </tbody> </table> </div>
</div> </div>
You could use $filter (you would need to insert $filter as a dependency)
$scope.data: [ // assuming your model looks like this
{
Id: "my_id",
Name: "my_name",
checked: true
}
];
var myFilteredValues = $filter('filter')($scope.data, { $: true });
And in your HTML
<tr ng-repeat="x in data">
<td><input type="checkbox" ng-model="x.checked"></td>
<td>{{x.Name}}</td>
</tr>
You can add a .toDelete property to every x in your data array, for example, and bind the checkboxes like this:
<input type="checkbox" ng-model="x.toDelete">
Afterwards you can create a button under the table:
<input type="button" ng-click="deleteRows();" value="Delete rows">
...And then create a function on your controller's scope (a newbie-friendly function):
$scope.deleteRows = function(){
var i=0;
for(i=0; i<data.length; i++){
if(data[i].toDelete){
//send your server requests here and do
//your stuff with the element if needed
data.splice(i, 1); //remove the element from the array
i--; //decrement the value of i so it stays the same
//(array is now shorter than it used to be)
}
}
}
You can also keep the information on what row is to be deleted in a separate array on the same scope but this solution seems simpler to me.
Regarding the "Check All" checkbox at the top, you can create a function and bind it with ng-click to a button or any other element and use that function to simply iterate through all the elements and set the .toDelete property to true for each one of them.

Populate and update a table with data from a different table

My site allows for a user to search for a term which returns a table of associated songs. When the "Add Track" button in a particular row is clicked after the search, the respective track name and trackId are added to the table "playlist". The problem I am having is that once "Add Track" is clicked within a different row, the data from that row is not added to the "playlist" table, but rather it just replaces the previous information. I need to be able to generate a cumulative table. Any help would be great and thanks in advance!
<body ng-app>
<body ng-app>
<div ng-controller="iTunesController">
{{ error }}
<form name="search" ng-submit="searchiTunes(artist)">
<input type="search" required placeholder="Artist or Song" ng-model="artist"/>
<input type="submit" value="Search"/>
</form>
<div class="element"></div>
<table id="SongInfo" ng-show="songs">
<thead>
<tr>
<th>Album Artwork</th>
<th>Track</th>
<th></th>
<th>Track Id</th>
<th>Preview</th>
<th>Track Info</th>
<th>Track Price</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="song in songs">
<td><img ng-src="{{song.artworkUrl60}}"
alt="{{song.collectionName}}"/>
</td>
<td>{{song.trackName}}</td>
<td><button ng-click="handleAdd(song)">Add Track</button></td>
<td>{{song.trackId}}</td>
<td>Play</td>
<td>View Track Info</td>
<td>{{song.trackPrice}}</td>
</tr>
</tbody>
</table>
<table id="playlist">
<thead>
<tr>
<th>Playlist</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="song in addedtracks">
<td>{{song.trackName}}</td>
<td>{{song.trackId}}</td>
</tr>
</tbody>
</table>
</div>
</body>
itunes_controller.js
var iTunesController = function($scope, $http){
$scope.searchiTunes = function(artist){
$http.jsonp('http://itunes.apple.com/search', {
params: {
'callback': 'JSON_CALLBACK',
'term': artist,
limit: 5,
}
}).then(onSearchComplete, onError)
}
$scope.handleAdd = function(song) {
// this song object has all the data you need
console.log("handle add ", song)
$scope.addedtracks = [{song:'trackName', song:'trackID'}]
$scope.addedtracks.push(song)
}
var onSearchComplete = function(response){
$scope.data = response.data
$scope.songs = response.data.results
}
var onError = function(reason){
$scope.error = reason
}
}
I saw some issues with your code. First the code below
$scope.addedtracks = [{song:'trackName', song:'trackID'}]
$scope.addedtracks.push(song)
Acording to your html, you are passing the song object to the handleAdd. So just remove the first line from code above. After that step, declare addedtracks array before handleAdd like below
$scope.addedtracks = [];
Modify the ng-repeat for the playlist like below:
<tr ng-repeat="song in addedtracks track by $index">
<td>{{song.trackName}}</td>
<td>{{song.trackId}}</td>
</tr>
And that's it. Note that I used track by $index because ngRepeat does not allow duplicate items in arrays. For more information read Tracking and Duplicates section.
Finally this is working plunker

Angular display message if list is empty

I'm displaying subwebs that the current user has access to, in a sharepoint app part.
I've managed to hide or show a message saying "There is no sites avaiable for you" if my app.js returns 0 in the scope.array.
BUT, the message is displaying before the list has finished loading and when the list has loaded the message dissapears. How can I fix that?
I have a ng-repeat in a table with textbox for filtering:
<input type="text" ng-model="text" placeholder="Filter..." />
<div ng-controller="MainCtrl">
<table>
<thead>
<tr>
<th>
Site
</th>
<th>
Created
</th>
</tr>
</thead>
<tbody ng-repeat="site in sites | filter:text">
<tr>
<td>
{{site.title}}
</td>
<td>
{{site.created | date:dateFormat}}
</td>
</tr>
</tbody>
</table>
<div ng-show="!(sites| filter:text).length">There is no sites avaiable for you.</div>
</div>
I understand that the ng-show is outside the table where ng-repeat is executed, but I've tried inserting the ng-show div inside the tbody but then if no sites exist the table is never executed.
Also, how can I disable the filter textbox if the array is empty? Tried the same attributes as ng-show but in ng-disabled, but that doesnt work.
Wait for your success callback to determine there are no results before showing the message.
//view
<div ng-controller="MainCtrl">
<div ng-show="loading">Loading..</div>
<input ng-hide="noSites" ng-model="text" placeholder="Filter...">
//table ..
<div ng-show="noSites">
There are no sites available for you.
</div>
</div>
//controller
app.controller('MainCtrl', function($scope) {
//initialize
$scope.loading = true
$scope.noSites = false
//sharepoint code to get sites
.success(function(data) {
if (data.length != 0) {
$scope.sites = data
} else {
$scope.noSites = true
}
$scope.loading = false
})
})
<input type="text" ng-model="text" placeholder="Filter..." />
put this inside the <div ng-controller="MainCtrl">.. div
and modify this
<div ng-show="!sites.length" style="display:none">There is no sites avaiable for you.</div> // added `display:none

Table "jumps" when url is changed, angular js

I have a table with items. When a row is clicked, detailed item information shows upp in a fixed div next to the table.
The div next to the table is hooked up to a parameter in the url. So when I click an item, the url changes the url to /project/:itemID
The issue is that when a row is clicked, the table "jumps" to another position. The scroll changes position. I want it to stay the same place.
This is my controller which changes the url:
$scope.goToProduct = function(id){
var path = $location.path();
var splitUrl = path.split('/');
var project = splitUrl[1];
console.log(project);
$location.path(project + '/' + id);
}
This is my view:
<input type="text" class="form-control" placeholder="Search">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Code</th>
<th>Namn</th>
<th>Manufactor</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products | filter:query" ng-click="goToProduct(product.pr_id)">
<td>{{product.pr_usercode}}</td>
<td>{{product.pr_title}}</td>
<td>{{product.pr_producer}}</td>
<td>{{product.pr_description}}</td>
</tr>
</tbody>
</table>
</div>
Any idea how to solve this?
Solved. Ui-router had a handy directive. Just add autoscroll="false". Docs: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#wiki-autoscroll

Resources