ng-Repeat going through 3 arrays with different properties - arrays

I am making a table where I can show data from 3 arrays (2 has identical properties, 1 different).
<table class="table table-striped table-condensed table-hover">
<thead>
<tr ng-bind-html="head">
</tr>
</thead>
<tbody>
<tr ng-repeat="item in book[bigCurrentPage]">
<td ng-bind="{{row.item1}}"></td>
<td ng-bind="{{row.item2}}"></td>
<td ng-bind="{{row.item3}}"></td>
<td ng-bind="{{row.item4}}"></td>
<td ng-bind="{{row.item5}}"></td>
<td ng-bind="{{row.item6}}"></td>
<td ng-bind="{{row.item7}}"></td>
</tr>
</tbody>
</table>
$scope.row = { item1: "item.code", item2: "item.name" };
$scope.row = { item1: "item.num", item2: "item.name", item3: "item.category", item4: "item.location.name", item5: "item.toloc.name", item6: "item.department", item7: "item.adm", item8: "item.person.name" };
First array has 7 properties (num,name,category,location,department,adm,person) and other 2 arrays has 2 properties (code,name)
The problem lies in that when I use 2 of cells in a row, then other cells are still visible without any content. Is there a way to have one table which can ngRepeat arrays with different properties and property count?
Solution:
<table class="table table-striped table-condensed table-hover">
<thead>
<tr ng-bind-html="head"></tr>
</thead>
<tbody ng-include=activeTable>
</tbody>
</table>
<script type="text/ng-template" id="assets.html">
<tr ng-repeat="item in book[bigCurrentPage]">
<td ng-bind=item.num></td>
<td ng-bind=item.name></td>
<td ng-bind=item.category></td>
<td ng-bind=item.location.name></td>
<td ng-bind=item.department></td>
<td ng-bind=item.adm></td>
<td ng-bind=item.person.name></td>
</tr>
</script>
<script type="text/ng-template" id="persons.html">
<tr ng-repeat="item in book[bigCurrentPage]">
<td ng-bind=item.code></td>
<td ng-bind=item.name></td>
</tr>
</script>
<script type="text/ng-template" id="locations.html">
<tr ng-repeat="item in book[bigCurrentPage]">
<td ng-bind=item.code></td>
<td ng-bind=item.name></td>
</tr>
</script>
$scope.assetshow = function () {
$scope.asse = true;
$scope.pers = false;
$scope.loca = false;
$scope.bigTotalItems = $scope.copy.data.assets.length;
$scope.book = PageItems($scope.copy.data.assets, 17);
console.log($scope.bigCurrentPage)
$scope.row = { item1: "item.num", item2: "item.name", item3: "item.category", item4: "item.location.name", item5: "item.toloc.name", item6: "item.department", item7: "item.adm", item8: "item.person.name" };
$scope.head = '<th>Asset code</th><th>Asset name</th><th>Category</th>'
+ '<th>Location</th><th>Department</th><th>Administration</th><th>Person in charge</th>';
$scope.activeTable = "assets.html";
},
$scope.locationshow = function () {
$scope.asse = false;
$scope.pers = false;
$scope.loca = true;
//produce pages.
$scope.bigTotalItems = PageItems($scope.copy.data.locations);
$scope.book = PageItems($scope.copy.data.locations, 17);
$scope.head = '<th>Kods</th><th>Nosaukums</th>';
$scope.row = { item1: "item.code", item2: "item.name" };
$scope.activeTable = "locations.html";
},
$scope.personshow = function () {
$scope.asse = false;
$scope.pers = true;
$scope.loca = false;
//produce pages.
$scope.bigTotalItems = PageItems($scope.copy.data.persons);
$scope.book = PageItems($scope.copy.data.persons, 17);
$scope.head = '<th>Kods</th><th>Nosaukums</th></tr>';
$scope.row = { item1: "item.code", item2: "item.name" };
$scope.activeTable = "persons.html";
};
By changing activeTable string value table content changes.
DEMO: plnkr

Related

How to display array in table using ng-repeat

Unlike other JSON data , I have simple arrays of two.
$scope.land = [ elephant, tiger, zebra ];
$scope.water = [ fish , octopus, crab];
I want to put up the array in table using ng-repeat. I tried this but data is not showing in proper format.
<table class="table table-dark">
<thead>
<tr>
<th scope="col">LAND</th>
<th scope="col">WATER</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="l in land track by $index">
<td>{{l}}</td>
<td>fish</td>
</tr>
</tbody>
</table>
EDIT:-
err in data showing vertical in table
enter image description here
new data :-
$scope.land = [ 'tiger in zoo', 'tiger in jungle', 'zebra'];
$scope.water = [ 'fish in pond'];
I think you are trying to display elements of two arrays next to each other (correct me if i'm wrong).
Here is a simple way to do that :
angular.module('myApp', [])
.controller('TestCtrl', function ($scope) {
$scope.land = [ 'tiger in zoo', 'tiger in jungle', 'zebra'];
$scope.water = [ 'fish in pond'];
$scope.combined = [];
var maxLength = ($scope.land.length > $scope.water.length) ? $scope.land.length : $scope.water.length;
//length is to be determined
for(var index = 0; index < maxLength ; index++) {
$scope.combined.push({
land: ($scope.land[index]) ? $scope.land[index] : '',
water: ($scope.water[index]) ? $scope.water[index] : ''
});
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="TestCtrl">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">LAND</th>
<th scope="col">WATER</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in combined">
<td>{{item.land}}</td>
<td>{{item.water}}</td>
</tr>
</tbody>
</table>
</div>

Lazy loading without scrolling

<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-condensed">
<thead>
<tr style="text-align: left!important;">
<th width="4%">Part#</th>
<th width="5%">Description</th>
<th width="3.8%">{{title}}</th>
</tr>
</thead>
<tbody>
<tr ng-if="show" ng-repeat="item in dataElemet |orderBy:orderByField:reverseSort" class="PartData">
<td>
<div ng-if="item.isWarningIconShow == true"><img ng-src="../cim/images/projIcons/warning.png"/></div><span ng-bind="item.number"></span>
</td>
<td ng-bind="item.description"></td>
<td ng-bind="item.Number"></td>
<td ng-bind="item.prodCategory"></td>
</tr>
<tr ng-if="show" ng-repeat="item in dataElemet |orderBy:orderByField:reverseSort" class="PartData">
<td>
<div ng-if="item.isWarningIconShow == true"><img ng-src="../cim/images/projIcons/warning.png"/></div><span ng-bind="item.number"></span>
</td>
<td ng-bind="item.description"></td>
<td ng-bind="item.prodCategory"></td>
</tr>
</tbody>
</table>
$scope.intializePartsData = function(objId, dataType){
$scope.loading = true;
$scope.dataElemet = null;
$scope.dataElemet2 = null;
if($scope.show == true && $scope.title == "Show Non Prod"){
objectService.getParts(objId).then(function(parts){
if(dataType == "Elemet") {
$scope.dataElemet = parts.data;
$scope.partsOriginalElemet = angular.copy($scope.dataElemet);
} else {
$scope.dataElemet2 = parts.data;
$scope.partsOriginalElemet2 = angular.copy($scope.dataElemet2);
}
});
} else {
objectService.getPartsProduction(objId).then(function(parts){
if(dataType == "Elemet") {
$scope.dataElemet = parts.data;
$scope.partsOriginalElemet = angular.copy($scope.dataElemet);
} else {
$scope.dataElemet2 = parts.data;
$scope.partsOriginalElemet2 = angular.copy($scope.dataElemet2);
}
});
}
$scope.loading = false;
}
$scope.showHideParts = function(dataType){
$scope.intializePartsData($scope.objId, dataType);
if($scope.show == true && $scope.title == "Show Non Prod"){
$scope.title = "Hide Non Prod";
$scope.show = false;
}else{
$scope.title = "Show Non Prod";
$scope.show = true;
}
}
This is my code its work fine normally . But When there is more data it takes time. So I want to implement lazy loading but as client not interested in scrolling, i need to to do lazy loading without scroll. Is it any way to do lazy loading in angularjs without any event from user.

angular angular-table dynamic header name

I use angular and angular-table to display multiple tables on the same page.
I need to create dynamic table with dynamic header and dynamic content.
Plunkr her
This is a working example with non dynamic header but I don't find how to make dynamic
The controller :
angular.module('plunker', ['ui.bootstrap',"angular-table","angular-tabs"]);
function ListCtrl($scope, $dialog) {
$scope.cols= ['index','name','email'];
$scope.list = [
{ index: 1, name: "Kristin Hill", email: "kristin#hill.com" },
{ index: 2, name: "Valerie Francis", email: "valerie#francis.com" },
...
];
$scope.config = {
itemsPerPage: 5,
fillLastPage: true
};
}
HTML
<!-- this work -->
<table class="table table-striped" at-table at-paginated at-list="list" at-config="config">
<thead></thead>
<tbody>
<tr>
<td at-implicit at-sortable at-attribute="name"></td>
<td at-implicit at-sortable at-attribute="name"></td>
<td at-implicit at-sortable at-attribute="email"></td>
</tr>
</tbody>
</table>
<!-- this fail ... -->
<table class="table table-striped" at-table at-paginated at-list="list" at-config="config">
<thead></thead>
<tbody>
<tr>
<td ng-repeat='col in cols' at-implicit at-sortable at-attribute="{{col}}"></td>
</tr>
</tbody>
</table>
I am missing some think or it is not possible with this module ?
Did you know another module where you can have dynamic header and pagination ? ( i try also ngTable but have some bug issu with data not being displayed )
Through the below code, you can generate dynamic header
<table class="table table-hover table-striped">
<tbody>
<tr class="accordion-toggle tblHeader">
<th ng-repeat="(key, val) in columns">{{key}}</th>
</tr>
<tr ng-repeat="row in rows">
<td ng-if="!$last" ng-repeat="col in key(row)" ng-init="val=row[col]">
{{val}}
</td>
</tr>
</tbody>
</table>
Angular Script
var app = angular.module('myApp', []);
app.controller('myControl', function ($scope, $http) {
$http.get('http://jsonplaceholder.typicode.com/todos').success(function (data) {
$scope.columns = data[0];
$scope.rows = data;
}).error(function (data, status) {
});
$scope.key = function (obj) {
if (!obj) return [];
return Object.keys(obj);
}
});

How to set checkAll for in angularjs ng-repeat table

I have a complex ng-repeat table,which I need to set the select all function for the checkbox of each row.He is my code:
JS:
$scope.getCCGenie = function(){
CCGenieService.transactionList().then(function(list){
$scope.CreditCardsList = list;
$scope.CorporateCardsNum = $scope.CreditCardsList.transactionInfo.corpCardCount;
$scope.PersonalCardsNum = $scope.CreditCardsList.transactionInfo.personalCardCount;
});
}
$scope.hasExpenseTypeLabel = false;
$scope.getCCGenie();
$scope.checkAllcorp = function(copcards) {
console.log(copcards);
if (copcards.selectedAllcorp) {
copcards.selectedAllcorp = true;
$scope.hasSelectedExpense = true;
} else {
copcards.selectedAllcorp = false;
$scope.hasSelectedExpense = false;
}
angular.forEach('copcards.transactions',function(v,k){
v.corpisSelected = copcards.selectedAllcorp;
})
};
// I need to loop the selected table to reset each corpisSelected,assign them the new value selectedAllcorp. but how?
};
Html:
<ul>
<li ng-repeat='copcards in CreditCardsList.transactionInfo.corporateCards'>
<span class='creditcardsubtitle'>{{copcards.cardName}} (****{{copcards.cardNumber}})</span>
<table class='table col-xs-12 col-md-12 col-lg-12' border="0" cellpadding="10" cellspacing="0" width="100%">
<thead>
<th class='text-center'><input type="checkbox" ng-model="copcards.selectedAllcorp" ng-change="checkAllcorp(copcards)"/></th>
<th class='text-left'>Date</th>
<th class='text-left'>Merchant</th>
<th class='text-right'>Amount</th>
<th class='text-center'>Expense Type</th>
<th class='text-left'><input type="checkbox" ng-model="selectedAllBill" ng-change="checkAllBillable()" style='margin-right:3px;'/> Billable</th>
<th class='text-center'>Attachment</th>
<th class='text-center'>Description</th>
</thead>
<tbody><tr ng-show='noExpense'><td colspan="8" align="center" style="text-align: center;" >No any Personal Cards!</td>
</tr>
<tr ng-repeat='componentObject in copcards.transactions' ng-class="{'selectedrow':componentObject.corpisSelected}">
<td type='checkbox' class='text-center'><input type='checkbox' ng-model='componentObject.corpisSelected' class='deletebox'/>{{componentObject.corpisSelected}}</td>
<td class='text-left tdwidth'>{{componentObject.transactionDateString | parseDateFormat | date}}</td>
<td class='text-left tdwidth'>{{componentObject.merchant}}</td>
<td class='text-right tdwidth'>${{componentObject.amount}}</td>
<td class='text-center tdwidth'><smart-expense-type></smart-expense-type></td>
<td class='text-left tdwidth'><input type='checkbox' ng-model='componentObject.isBillable'/></td>
<td class='text-center tdwidth'>{{componentObject.hasImage}}</td>
<td class='text-center tdwidth'><input type='text'ng-model='componentObject.description'/></td>
</tr>
</tbody>
</table>
</li>
</ul>
As you can see, I use nasted ng-repeat the loop data, there may be many tables over there, I want each table has his own selectAll function which works independently. Now the code is partly working fine, the problem is if you check the single checkbox in row, the checkall function will not work.I know I need to do something like this:
$scope.checkAllcorp = function() {
if (this.selectedAllcorp) {
$scope.hasSelectedExpense = true
this.selectedRow = true;
this.corpisSelected = true;
} else {
$scope.hasSelectedExpense = false;
this.selectedRow = false;
this.corpisSelected = false;
}
angular.forEach('selectedTable',function(v,k){
v.corpisSelected = this.selectedAllcorp;
})
};
But how can I access this 'selectedTable'?
Pass copcards to your function and then operate on that object instead of this:
ng-change="checkAllcorp(copcards)"
And then in your controller:
$scope.checkAllcorp = function(copcards) {
if (copcards.selectedAllcorp) {
$scope.hasSelectedExpense = true
copcards.selectedRow = true;
copcards.corpisSelected = true;
} else {
$scope.hasSelectedExpense = false;
copcards.selectedRow = false;
copcards.corpisSelected = false;
}
angular.forEach('selectedTable',function(v,k){
v.corpisSelected = copcards.selectedAllcorp;
})
};
you can pass the object you are on by calling it thru the function itself like this
ng-change="checkAllcorp(copcards)
and then use it as a parameter for you checkAllcorp function instead of this in you function
example :
<div ng-repeat="name in names"><button ng-click="msg(name)"></button></div>
controller scope :
$scope.msg = function(name) { alert(name.first)};

Pagination gets "stuck"?

I have a simple table application that displays dummy data with columns like name, number, date, etc. You can sort it by columns, and it is paginated.
However, when I use the pagination, some of the rows get stuck at the top. It kind of gets sorted alphabetically by the column I intended, but when the page changes (pretend page 4), the rows underneath the first few top rows change, while the very top rows don't budge.
Here is a functional plunker:
HTML:
<pagination class="pagination pagination-sm" items-per-page="itemsPerPage" total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()"></pagination>
<table class="table table-hover">
<tr>
<th ng-repeat="key in keys" ng-click="sorting.predicate=key;">
{{key}}
</th>
</tr>
<tr ng-repeat="row in data | startFrom:currentRow | orderBy:sorting.predicate:sorting.reverse | limitTo:itemsPerPage">
<td ng-repeat="key in keys">
{{row[key]}}
</td>
</tr>
</table>
Angular:
app.filter('startFrom', function () {
return function (input, start) {
start = +start;
return input.slice(start);
};
});
$scope.data = [{
"id": 0,
"age": 30,
"name": "Trina Pace",
"gender": "female",
"email": "trinapace#darwinium.com",
"phone": "+1 (874) 414-2654"
},etc..];
$scope.keys = Object.keys($scope.data[0]);
$scope.totalItems = $scope.data.length;
$scope.itemsPerPage = 25;
$scope.currentPage = 1;
$scope.currentRow = 0;
$scope.pageChanged = function () {
$scope.currentRow = ($scope.currentPage - 1) * $scope.itemsPerPage;
console.log('current row: ' + $scope.currentRow);
console.log('items per page: ' + $scope.itemsPerPage);
};
$scope.sorting = {predicate:'name',reverse:false};
Am I missing, and/or doing something incorrectly?
Move startFrom:currentRow to after orderBy:sorting.predicate:sorting.reverse.
You want to filter after you sort.
http://plnkr.co/edit/7BbgJ4TMU0pSY8gyDLef?p=preview
<table class="table table-hover">
<tr>
<th ng-repeat="key in keys" ng-click="sorting.predicate=key;">
{{key}}
</th>
</tr>
<tr ng-repeat="row in data | orderBy:sorting.predicate:sorting.reverse | startFrom:currentRow | limitTo:itemsPerPage">
<td ng-repeat="key in keys">
{{row[key]}}
</td>
</tr>
</table>

Resources