Dynamic JSON handling in angularJS - angularjs

I've got two JSON arrays, one for headers and the other for data. I'm handling the headers, and I'm trying to display the data with respect to those headers using nested ng-repeat, but it results in empty rows.
The JSON arrays and the html code for displaying the data are pasted below.
Please help me.
$scope.data=[{'first_name':'ruth','last_name':'vick','email':'ruthvick#gmail.com','isMarried':'no','nick_name':'ruthu'},{'first_name':'rahul','last_name':'kumar','email':'rahul#gmail.com','isMarried':'no','nick_name':'rahul'},{'first_name':'vicky','last_name':'gupta','email':'vicky#gmail.com','isMarried':'no','nick_name':'vicky'}]
$scope.headerAll=[{'field':'first_name', 'displayName':'First name','type':'required'},{'field':'last_name', 'displayName':'Last Name','type':'required'},{'field':'email', 'displayName':'Email','type':'required'},{'field':'isMarried', 'displayName':'marital Status','type':'optional'},{'field':'nick_name', 'displayName':'Nick Name','type':'optional'}]
<div>
<table class="table table-bordered table-hover">
<thead class="wrapper">
<tr>
<th ng-repeat="data in header">
<div class="col-md-9">{{data.displayName}}</div>
<div class="col-md-1">
<button href="" ng-click="deleteColumn(data.field,$index)"><span class=" glyphicon glyphicon-trash pull-right"> </span></button>
</div>
<div class="dropdown col-md-1" >
<button class="glyphicon glyphicon-pencil dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" ng-click = "toPoint($index);">
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1" ng-repeat="optionalHeader in optional">
<li role="presentation" ng-repeat="dataEdit in headerAll"><a role="menuitem" tabindex="-1" href="" ng-click="editColumn(data.field,dataEdit.field,$index)">{{dataEdit.displayName}}</a></li>
</ul>
</div>
</th>
<th><div class="dropdown" >
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
Add Columns
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1" ng-repeat="optionalHeader in optional">
<li role="presentation" ng-repeat="optionalHeader in optional"><a role="menuitem" tabindex="-1" href="" ng-click="addColumn(optionalHeader.field)">{{optionalHeader.displayName}}</a></li>
</ul>
</div>
</th>
</tr>
</thead>
<tbody >
<tr class="active" ng-repeat="row in data">
<td ng-repeat="fields in headerAll">
{{row.fields.field}}
</td>
</tr>
</tbody>
</table>
</div>
Here, row.fields.field is creating empty rows.

Try to access with braces and not the dot operator.
data:
<div ng-repeat="obj in data">
{{ obj["first_name"]}}
{{ obj["last_name"]}}
</div>
header
<div ng-repeat="obj in headerAll">
{{ obj["field"]}}
{{ obj["displayname"]}}
</div>

Related

Better way of doing this <tr> ng-repeat?

I am generating from the database a sidebar, based on types of books. For each type, I generate a tab to show listings of books in that type. It works well for other cases with less records, but I am getting a delay of some seconds in this case. Any ideas on how to improve performance of this?
Layout:
#* Sidebar *#
<div class="col-sm-3" ng-if="ShowSidebar" ngCloak>
<ul class="list-group sidebar-nav-v1" id="sidebar-nav">
#* Books *#
<li class="list-group-item list-toggle">
<a data-toggle="collapse" data-parent="#sidebar-nav" href="#collapse0">
<i class="fa fa-sort-alpha-asc"> </i> Books
</a>
<ul id="collapse0" class="collapse1 accordion-body collapse">
<li ng-repeat="bookTopic in ListBookTopics" style="text-align:justify">{{bookTopic.topic_en}}</li>
</ul>
</li> #* End of Books *#
#* Series *#
<li class="list-group-item list-toggle">
<a data-toggle="collapse" data-parent="#sidebar-nav" href="#collapse1">
<i class="fa fa-sort-alpha-asc"> </i> Series (Collections)
</a>
<ul id="collapse1" class="collapse">
<li ng-repeat="seriesTopic in ListSeriesTopics" style="text-align:justify">{{seriesTopic.topic_en}}</li>
</ul>
</li> #* End of Series *#
</ul>
</div> #* End of Sidebar v2*#
#* Main Area *#
<div class="col-sm-9" ng-if="ShowMainArea" ngCloak>
<div class="tab-v3">
<div class="tab-content">
#* Generate Books Tabs to list books by type *#
<div class="tab-pane fade in" id="{{booksTopic.id}}" ng-repeat="booksTopic in ListBookTopics" ng-cloak>
<div class="table-search-v1 panel panel-dark margin-bottom-50">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-book"></i> {{booksTopic.topic_en}} </h3>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-search"></i></div>
<input type="text" class="form-control" placeholder="Search..." ng-model="searchParamBooksTopics">
</div>
</div>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th class="center" rowspan="2">Title</th>
<th class="center" rowspan="2">Author</th>
<th class="center" rowspan="1">Number</th>
<th class="center" rowspan="1">Type</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in ListBooksInBooks | filter: { title: searchParamBooksTopics }" ng-if="row.title.length>1 && row.topics_id == booksTopic.id">
<td style="text-align:justify;"> {{row.title}}</td>
<td>{{row.author}}</td>
<td class="center">{{row.number}}</td>
<td class="center">{{row.topics_id}}</td>
</tr>
</tbody>
</table>
</div> #* End of table-responsive *#
</div> #* End of table-search-v1 panel panel-dark margin-bottom-50 *#
</div> #* End of Tab Content 1 - Books *#
</div> #* End of tab-content *#
</div> #* End of tab-v3 *#
</div> #* End of col-sm-9 - Main Area *#
Angular - Used Lists:
$scope.ListBookTopics = #Html.Raw( new JavaScriptSerializer().Serialize( Model.ListBookTopics ) )
$scope.ListBooksInBooks = #Html.Raw( new JavaScriptSerializer().Serialize( Model.ListBooksInBooks ) );
The line that is causing the delay is this one:
<tr ng-repeat="row in ListBooksInBooks | filter: { title: searchParamBooksTopics }" ng-if="row.title.length>1 && row.topics_id == booksTopic.id">
Any faster way of looping the list of books and getting all books of that type?

Change collapse data-target and id automatically

Inside an ng-repeat I have a table in which I display information using Bootstap collapse, however I need the id to be changed with the loop in order for the data to be displayed in the right place, with my current code it keeps displaying in the same row with each click, here is my code :
<tr ng-repeat=" p in projetsListe ">
<td>{{p.NomProjet}}</td>
<td>{{calcul(p.IdProjet)}}</td>
<td>{{calculTotal(p.IdProjet)}}</td>
<td>{{calcul(p.IdProjet)-calculTotal(p.IdProjet)}}</td>
<td>
<a href="#" ng-click="myFunction(p.IdProjet)" data-toggle="collapse" data-target="#d">
<i class="fa fa-eye"></i>
<span>Voir les détails</span> <i class="fa fa-fw fa-caret-down"></i></a>
<ul id="d" class="collapse">
<table id= "mytable" class="table table-hover" style="display : none;">
<thead>
<tr>
<th>Tache</th>
<th><center>Charge estimée</center></th>
<tr ng-repeat="t in tempnorep">
<td>{{t.NomTache}}</td>
<td><center>{{t.TempsPrevu}} jours<center></td>
</tr>
</table>
</ul>
</td>
</tr>
I believe you wanted to do something like:
<tr ng-repeat=" p in projetsListe ">
<td>{{p.NomProjet}}</td>
<td>{{calcul(p.IdProjet)}}</td>
<td>{{calculTotal(p.IdProjet)}}</td>
<td>{{calcul(p.IdProjet)-calculTotal(p.IdProjet)}}</td>
<td>
<a href="#" ng-click="myFunction(p.IdProjet)" data-toggle="collapse" ng-attr-data-target="#{{p.IdProjet}}">
<i class="fa fa-eye"></i>
<span>Voir les détails</span> <i class="fa fa-fw fa-caret-down"></i></a>
<ul ng-attr-id="{{p.IdProjet}}" class="collapse">
<table id= "mytable" class="table table-hover" style="display : none;">
<thead>
<tr>
<th>Tache</th>
<th><center>Charge estimée</center></th>
<tr ng-repeat="t in tempnorep">
<td>{{t.NomTache}}</td>
<td><center>{{t.TempsPrevu}} jours<center></td>
</tr>
</table>
</ul>
</td>
</tr>

angular-bootstrap 2 paginations, issue in second pagination

image showing paginations
I have item list with many tables in which initaily i show headers by loop each table header works as accordion and on click any table header one request send to get data for that header and tbody populate accordingly.
I have applied pagination on headers and its working fine but pagination on any tbody not working.
Below I have mentioned HTML code.
<div class="col-md-12">
<!-- start table one -->
<div class="table table-responsive series-tbl series-tbl-m">
<table data-ng-repeat="series in serieses track by series._id" class="table table-striped" id="headingOne">
<thead class="table-blue">
<tr >
<th width="5%"><input ng-click="checkAllTalks(series._id,series.Selected)" ng-model="series.Selected" selected="{{series.Selected}}" type="checkbox"></th>
<th width="28%">{{series.title}}</th>
<th width="22%">{{series.totalCount || 0}} talks</th>
<th width="15%">{{ series.totalDuration || 0 | timestring }}</th>
<th width="15%">${{series.totalPrice || 0}}</th>
<th width="18%" class="th-action-icon">
<!-- <i class="fa fa-plus"></i> -->
<a ng-click="openUpdateSeriesModal('md',series._id)" href="#"><i class="fa fa-pencil"></i></a>
<i class="fa fa-trash"></i>
<a role="button" ng-click="showTalks(series._id)" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"><i class="fa fa-caret-down"></i></a>
</th>
</tr>
</thead>
<tbody id="series-{{series._id}}" class="panel-collapse collapse in dn allSeriesTalks" role="tabpanel" aria-labelledby="headingOne">
<tr ng-repeat='talk in seriesTalks track by talk._id'>
<td><input ng-click="checkUncheckTalk(talk._id,talk.Selected)" ng-model="talk.Selected" selected="{{talk.Selected}}" type="checkbox"></td>
<td>{{talk.title}}</td>
<td>{{talk.duration || 0 | timestring }}</td>
<td>{{bytesToMB(talk.size) || 0}} mb</td>
<td>${{talk.price || 0}}</td>
<td class="action-item">
<!-- <i class="fa fa-plus icon-gray"></i> -->
<a ng-click="openUpdateTalkModal('md',talk._id)" href="#"><i class="fa fa-pencil icon-gray" ></i></a>
<a ng-click="deleteTalk(talk._id)" href="javascript:void(0);"><i class="fa fa-trash icon-gray"></i></a>
</td>
</tr>
<tr>
<td colspan="6">
<div data-ng-if="totalTalkCount> 10" class="col-md-12">
<div pagination total-items="totalTalkCount"
ng-model="currentTalkPageNo"
max-size="maxSize"
class="pagination-sm pull-right"
boundary-links="true"
ng-change="talkPageChanged(currentSeriesId)"></div>
</div>
<div data-ng-if="totalTalkCount == 0" class="col-md-2">
<div>No record Found.</div>
</div>
</td>
</tr>
</tbody>
</table>
</div> <!-- end table one -->
</div>
<div class="col-md-12">
<div pagination total-items="totalItems"
ng-model="currentPage"
max-size="maxSize"
class="pagination-sm pull-right"
boundary-links="true"
ng-change="pageChanged()"></div>
</div>
And below controller code
//first it is working properly
vm.currentPage = 1;
vm.maxSize = 5;
vm.pageChanged = function () {
console.log('page changed:', vm.currentPage);
vm.getSeriesList();
//$scope.getMembers();
};
//second its not working
vm.currentTalkPageNo = 1;
vm.talkPaginate = false;
vm.talkPageChanged = function (seriesId) {
console.log('page talk changed:', vm.currentTalkPageNo);//each time it shows 1 page no
vm.talkPaginate = true;
vm.showTalks(seriesId);
//$scope.getMembers();
};
remove below code
data-ng-if="totalTalkCount == 0"
in pagination parent div to
data-ng-class="{ 'dn':totalTalkCount <= 10}"
And use
ng-model="$parent.currentTalkPageNo"
instead of
ng-model="currentTalkPageNo"
css
.dn{display:none;}

Angular js table filter not working for nested objects

I have array of object like -
$scope.objs = [
{
'name': 'test',
'id': '1',
'config': {
'processID': 3,
'hName': 'host'
}
},
{
'name': 'test2',
'id': '12',
'config': {
'processID': 32,
'hName': 'host2'
}
}
];
I have a table representation for it and I want to add filter on each column --
<table>
<thead>
<tr>
<th>
Name
<span class="dropdown" dropdown on-toggle="toggled(open)">
<a href class="dropdown-toggle" dropdown-toggle>
<i title="filter" class="operation fa fa-filter"></i>
</a>
<ul id="filterPopup" class="dropdown-menu">
<li><input ng-model="search.name" type="text" placeholder="filter by Name"/></li>
</ul>
</span>
</th>
<th>
ID
<span class="dropdown" dropdown on-toggle="toggled(open)">
<a href class="dropdown-toggle" dropdown-toggle>
<i title="filter" class="operation fa fa-filter"></i>
</a>
<ul id="filterPopup" class="dropdown-menu">
<li><input ng-model="search.id" type="text" placeholder="filter by ID"/></li>
</ul>
</span>
</th>
<th>
ProcessID
<span class="dropdown" dropdown on-toggle="toggled(open)">
<a href class="dropdown-toggle" dropdown-toggle>
<i title="filter" class="operation fa fa-filter"></i>
</a>
<ul id="filterPopup" class="dropdown-menu">
<li><input ng-model="search.config.processID" type="text" placeholder="filter by pId"/></li>
</ul>
</span>
</th>
<th>
ProcessID
<span class="dropdown" dropdown on-toggle="toggled(open)">
<a href class="dropdown-toggle" dropdown-toggle>
<i title="filter" class="operation fa fa-filter"></i>
</a>
<ul id="filterPopup" class="dropdown-menu">
<li><input ng-model="search.config.hname" type="text" placeholder="filter by hname"/></li>
</ul>
</span>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="obj in objs | filter:search">
<td>
{{obj.name}}
</td>
<td>
{{obj.id}}
</td>
<td>
{{obj.config.processID}}
</td>
<td>
{{obj.config.hName}}
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="7">- END OF DATA -</td>
</tr>
</tfoot>
</table>
It works good for name and ID , but gives some strange behaviour for nested objects like - config.hname and config.processID . for them it works for the very first time and after that it does not work.
Any Idea on this?
Well, AngularJS filters only work on arrays (array of objects in your case.) and not on objects. Refer this question

pushing empty data into nested array in angular js

I'm kinda new to angular and would appreciate any assistance. I am creating a simple reporting system which has following structure
A Report can have many Subreports
and Subreports can have many Tasks.
Here's the my code structure
var Report = {
"ReportId":1,
"ReportDate":"02/05/2015",
"SubReport":[
{
"SubId": 1,
"ProjectName": "An ice sculpture",
"ProjectDeliverable":"My Deliverable1",
"ProjectTarget":"My Target1",
"Task":[
{
"TaskPerformed": "Task Performed 1 of 1",
"TimeSpent": 3,
"ResultAchieved": "Result 1 of 1" ,
"PlannedActivity": "Planned Activity 1 of 1"
}]
}]
};
<table class="table table-bordered table-condensed">
<tbody ng-repeat="subrep in Report.SubReport">
<tr>
<td>
<table class="table table-bordered table-condensed">
<thead ng-show="$index==0">
<tr>
<th >ACTION</th>
<th>Project Name</th>
<th>Project Deliverable</th>
<th>Target</th>
<th>Task Performed</th>
<th>Time Spent(Hrs)</th>
<th>Result Achieved</th>
<th>Planned Activity</th>
<th >ACTION</th>
</tr>
</thead>
<tbody>
<tr ng-show="$index==0" ng-repeat="tsk in subrep.Task">
<td>
<div class="btn-group">
<a class="btn btn-info" href="" ng-click="addNew()" ng-
show="$parent.$index==0" title="New SubReport" >
<i class="fa fa-plus"></i>
</a>
<a class="btn btn-green" href="" title="Edit SubReport" data-
toggle="modal" data-target="#">
<i class="fa fa-pencil-square-o"></i>
</a>
<a class="btn btn-danger" title="Delete SubReport" href="">
<i class="fa fa-trash-o"></i>
</a></div>
</td>
<td>{{subrep.ProjectName}}</td>
<td>{{subrep.ProjectDeliverable}}</td>
<td>{{subrep.ProjectTarget}}</td>
<td>{{tsk.TaskPerformed}}</td>
<td>{{tsk.TimeSpent}}</td>
<td>{{tsk.ResultAchieved}}</td>
<td>{{tsk.PlannedActivity}}</td>
<td>
<div class="btn-group">
<a class="btn btn-info" href="" data-toggle="modal" title="New
Task" data-target="#NewTask">
<i class="fa fa-plus"></i>
</a>
<a class="btn btn-green" href="tsk={{$index}}/sub={{$parent.
$index}}" title="Edit Task" data-toggle="modal" data-target="#defaultmodal">
<i class="fa fa-pencil-square-o"></i>
</a>
<a class="btn btn-danger" title="Delete Task" href="">
<i class="fa fa-trash-o"></i>
</a>
</div>
</td>
</tr>
<tr ng-show="$index>0" ng-repeat="tsk in subrep.Task">
<td colspan="4"></td>
<td>{{tsk.TaskPerformed}}</td>
<td>{{tsk.TimeSpent}}</td>
<td>{{tsk.ResultAchieved}}</td>
<td>{{tsk.PlannedActivity}}</td>
<td>
<div class="btn-group">
<a class="btn btn-green" href="tsk={{$index}}/sub={{$parent.
$index}}" title="Edit Task" data-toggle="modal" data-target="#defaultmodal">
<i class="fa fa-pencil-square-o"></i>
</a>
<a class="btn btn-danger" title="Delete Task" href="">
<i class="fa fa-trash-o"></i>
</a>
</div>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
my Factory which attempts to save a subreport in an existing Report
fac.AddSubReport = function (data) {
Report.SubReport.push(data);
return Report;
}
return fac;
enter code here
I have two challenges..
When I get the data from a txt file I can't get to display the my table with a single header as below :
SubId | ProjName | TaskId | TaskName
Abc. 1. Task1-1
.......... 2. Task1-2
.......... 3. Task1-3
Proj2. 1. Task2-1
......... 2. Task2-2
Here I used nested ng-repeat but I seem to iterate over the tasks using ng-show first line of subreport info if $parent.$index=0, but it keeps creating a new table for the subreport.
How do I add/push a subreport to a report without having a task
I would appreciate any assistance. I'm using my phone else would have posted some of my code.
Notice how i have used the ng-repeat on the Task so as to show only subreports with task index =0. I only want to have the subreport information displayed only on the first task. I am using a Modal to save the subreporrt, but the problem is that no subreport is displayed since as at the time of saving a subreport it doesnt have a task- henc its not displayed.
I would want to know the best approach to saving a subreport first before adding a task to it and how i can get the information to be displayed correctly on the table

Resources