Angular display message if list is empty - angularjs

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

Related

ng-if not working with ng-repeat and radio button

I have the following html template:
<div ng-app ng-controller="Ctrl">
<div class="cont">
<table>
<tr><th ng-repeat="column in columns">{{column}}</th></tr>
<tr ng-repeat="(i, row) in people">
<td ng-repeat="(j, column) in columns">
<input type="radio" name="select" ng-if="column === 'id'">
<span>{{row[column]}}</span>
</td>
</tr>
</table>
</div>
</div>
Then I define my Ctrl as follow
function Ctrl($scope) {
$scope.selectedId = 'aaaaa';
$scope.columns = ['id','name', 'age'];
$scope.people=[
{
'id':'aaaaa', 'name':'rachit', 'age':11
},
{
'id':'bbbbb', 'name':'gulati', 'age':12
},
{
'id':'ccccc', 'name':'rocks', 'age': 13
}
]
}
https://jsfiddle.net/en91zuxb/
it's showing radio input for all the item, while my intention is to only show the radio button on the first column, because the 1st column is the id column... ...
It seems that your angular version is old, this was an error related to angularjs < 1.2. Try upgrading your angularjs version or use ng-show instead.
Following code worked for me.
<input type="radio" name="select" ng-show="column == 'id' ">

How to populate a row values into textbox when click event using angularjs

I am beginning level in angularJS. Now i am trying to populate a table records into my input fields when table row click event(). How to make this any suggestions please.
Thanks in advance.:-)
Note: I didn't using any json data for showing records into table.
Try using events. Here is a simple demo of how you can select a clicked element from a table:
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope) {
$scope.select = function(e) {
$scope.selected = e.toElement.innerText;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<table ng-click="select($event)">
<tr>
<td>aaa1</td>
<td>bbb1</td>
</tr>
<tr>
<td>AAA2</td>
<td>BBB2</td>
</tr>
</table>
Selected <input ng-model="selected" />
</div>
you may use follow the workflow
<table>
<tr ng-repeat="item in recordsArr track by $index">
<td ng-click="doCallFunction( $index)"> {{item.id}}</td> <td> {{item.name}}</td>
</tr>
</table>
// just think of your records
post if any other info required

How to send data from a table generated with a form inside a ng-repeat

I have a table generated with a ng-repeat directive.
Each cell can be editable and each row has a submit button.
The submit button must send only the data of the affected row to the controller instead of sending the complete table. Then the controller will send it to a database. So if my table is huge and has a lot of rows, I prefer to just send one row to the database instead of the complete table.
Most of the time I use <form name="myForm" ng-submit="sendMyData()"> to send data from the view to the controller but in this case I have multiple forms (one per each row).
My problem is that I have no idea how to identify each row generated by the ng-repeat.
I am using AngularJS Material.
You don't need forms to do this. I'll demonstrate how to achieve this using sample data:
Controller:
app.controller("MyController", function($scope) {
$scope.persons = [
{ id: 1, name: "Bob" },
{ id: 2, name: "Alice" }
];
$scope.submitPerson = function(person) {
// do something to person - send to backend etc...
};
});
View:
<div ng-controller="MyController">
<table>
<tr ng-repeat="person in persons">
<td>{{person.name}}</td>
<td><button ng-click="submitPerson(person)">Submit</button></td>
</tr>
</table>
</div>
By clicking the submit button within each row - the individual person object will be passed to the submitPerson function in your controller where you can send it to the backend or do anything else.
You can try something like below just to submit one row at a time and also change in any number of rows and tick box which rows to be updated at once by clicking Submit ALL button.
var app = angular.module('app',[]);
app.controller('noduesaccountsmodalcontroller',function($scope){
$scope.nodueaccountassets = [{'name':'x'},{'name':'a'},
{'name':'b'},{'name':'c'},{'name':'d'}];
$scope.init= function(){
};
$scope.selectedItems =[];
$scope.rowSubmit = function(row){
$scope.submittedRow = row;
};
$scope.submit = function(acc){
angular.forEach($scope.nodueaccountassets,function(emp){
if(emp.selected){
$scope.selectedItems.push(emp.name);
}
});
};
});
<script data-require="jquery#*" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
<script data-require="angularjs#1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
<body ng-app="app" ng-controller="noduesaccountsmodalcontroller" ng-init="init()">
<table class="table">
<thead>
<tr>
<th>item</th>
<th>received</th>
</tr>
</thead>
<tbody ng-repeat="emp in nodueaccountassets track by $index">
<tr>
<td><input type="text" ng-model="emp.name"/></td>
<td>
<input type="checkbox" ng-model="emp.selected" value="{{emp.name}}" />
</td>
<td> <button type="button" value="submit" ng-click="rowSubmit(emp)">Submit</button></td>
</tr>
</tbody>
</table>
{{selectedItems}}
Submitted row --- {{submittedRow}}
<button type="button" ng-click="submit()" value="submit">Submit ALL</button>
</body>

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.

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

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.

Resources