How do you create complex AngularJS data bindings from Firebase? - angularjs

I working on a basic AngularJS application with a Firebase backend. Part of the database looks like...
Student
- FirstName
- LastName
- Schedule
-- Course1
-- Course2...
I have a page displaying an html table of students with ng-repeat. Here's the code for my table...
<table>
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Youth/Adult</th>
<th>Courses Selected?</th>
<th>...</th>
</thead>
<tbody>
<tr ng-repeat="(id, student) in students">
<td>{{ student.firstName }}</td>
<td>{{ student.lastName }}</td>
<td>{{ student.type }}</td>
<td>{{ student.hasSelectedCourses }}</td>
<td>...</td>
</tr>
</tbody>
The controller connecting the data and table together is really straight forward. It's just grabbing the array of students from Firebase and chucking it into $scope.
$scope.students = Students;
I'm getting students' first and last names, so I know the repeat is working in general. But I'm having difficulty with one particular column I'd like to display - the Courses Selected? column. I'd like to intelligently fill that with a boolean (probably an icon or something) based on the existance of the Schedule hanging off the Student in the database. I thought of adding something like this to my controller (don't laugh - still learning JavaScript). This didn't work. The error was that "student" was undefined.
$scope.student.hasSelectedCourses = !($scope.student.schedule === null);
I think I could do this another way where I actually store hasSelectedCourses as a boolean in the database, but I'm concerned about the integrity of that field and getting into a situation where a student's selected courses and that field are not in sync with each other.
Is there a way to accomplish this with a bit of logic rather than data storage or is storing this in the database the way to go?

Assuming student.schedule is null if no courses selected (which is the normal pattern with firebase):
<tbody>
<tr ng-repeat="(id, student) in students">
<td>{{ student.firstName }}</td>
<td>{{ student.lastName }}</td>
<td>{{ student.type }}</td>
<td>
<span ng-if="student.schedule">Yes<span>
<span ng-if="!student.schedule">No<span> <!-- these spans could be icons or whatever you want -->
</td>
<td>...</td>
</tr>
</tbody>
Alternatively
<td>
{{student.schedule ? 'Yes' : 'No'}}
</td>
A side note: if you aren't already it's well worth it to include AngularFire with your app. The live 3-way binding is a good way to impress.

Related

Laravel : How do i get the names, Phone numbers and other details using loop in blade view

This is a json format from my database. How do i get a tabulated information of the names and phone numbers etc.
I will create a simple example with minimal table
<table>
<thead>
<tr>
<th>Car type</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{ $post->Cartype }}</td>
<td>{{ $post->email }}</td>
</tr>
#endforeach
</tbody>
</table>
If you want car types and banks as seperate entities, you have to declare the $banks and $carTypes as 2 arrays and get the necessary data for the arrays from the database.
After that you can then pass those arrays to the view. You can use the below code to pass multiple arrays to the view.
return view('yourview', compact('banks ','carTypes '));
After that in your view you can render your values using the foreach loop to get the values.
foreach($banks as $bank){
// Your code
}
foreach($carTypes as $carType){
// Your code
}
Hope this helps you :)

Add row functionality in AngularJS table similar to Microsoft Word tables

When we hover our mouse between two rows of a Microsoft Office word table.
It displays a "+" icon, clicking on it inserts a new row in that table.
I want to achieve the same functionality with AngularJS with in a HTML table.
example:
--------------------
Row 1 | Value
-------------------<"+" Icon>
Row 2 | Value
--------------------
In most cases we have an Icon with each row which enables us to Add a row just below it.But then user will not be able to add row just below the header.
I found the Microsoft Word way very user friendly,so,I am interested in adding that feature in HTML table with AngularJS.
<table>
<tr ng-repeat="step in steps">
<td>{{ step.name }}</td>
<td>{{ step.description }}</td>
<td><button ng-click="addRow($index)">Add</button></td>
</tr>
</table>
$scope.addRow = function(index) {
// Code
$scope.steps.splice(index+1,1,{ step.name :"" , step.descrption:""})
//Code
}
Can't you just add a header row and a special button with the following?
<thead>
<tr>
<td class="add-row"><button ng-click="addRow(-1)">Add</button></td>
</tr>
</thead>
(I added a class to that td just to be able to position it out of the table flow...)

Minimise repetition of bindings in view

I'm new to Angular, as will probably become apparent!
I've created a view with a few tables of data to show users different statistics. A simple example would be:
<div class="table-container">
<table class="percs">
<thead>
<tr>
<td>Stat Name</td>
<td>Stat Value</td>
<td>Stat bar</td>
</tr>
</thead>
<tbody>
<tr>
<td>Stat 1</td>
<td>{{ statPercs.stat1 }}</td>
<td><uib-progressbar value="statPercs.stat1"></uib-progressbar></td>
</tr>
<tr>
<td>Stat 2</td>
<td>{{ statPercs.stat2 }}</td>
<td><uib-progressbar value="statPercs.stat2"></uib-progressbar></td>
</tr>
</tbody>
</table>
</div>
I'm using the progressbar just for a small visual representation of the data. However if my understanding so far is correct, although each row in the table only references one value it will incur 2 $$watchers by being referenced in 2 separate tags.
Is it possible to achieve this with just one binding per value (i.e. "statPercs.stat1", "statPercs.stat2", etc) but still be able to re-use that value in multiple columns of the table?
Or even better still, one binding for the object itself (i.e. "statPercs") and still be able to use the values contained in that object in multiple places in the table?

Adding Pagination to AngularJS Table

So I am getting some dynamic data and am able to get it to display, but I now want to add some pagination, I have seen that there are modules that can do this but with my limited experience with Angular I haven't been able to get one to work. Below is my table in my HTML, and basically in my Javascript I am running a REST POST call that returns the data and display it in the table below, however if I wanted to use pagination instead of displaying the whole table, how would I do so, I'm not trying to do anything fancy but I have tried some Jquery plugins in addition to the angularjs modules have not figured it out, I thought there may be a way to do this with a simple table like shown below, if anyone has any advice I'd appreciate it.
<table class="table table-striped">
<tr>
<th><b>ID</b></th>
<th><b>Score</b></th>
<th><b>Number</b></th>
</tr>
<tr ng-repeat="data in restCtrl.results">
<td>{{ data.id }}</td>
<td>{{ data.score }}</td>
<td>{{ data.number }}</td>
</tr>
</table>
You can have use ng-repeat="data in restCtrl.results | filter : paginate" with $resource:
http://plnkr.co/edit/79yrgwiwvan3bAG5SnKx?p=preview

Pass values dynamically across controllers for corresponding views - AngularJS

I have a page wherein I display a list of items in a table. There is a field named 'Details', after clicking which goes to the view displaying details of the particular item. Values being displayed on 'details.html' are correct, but I am using a static value of the item. I want to get the id of the clicked item so as to display its details. Both the html pages in views are having two different controllers, which I don't want to merge. So, how can I pass the values from one controller to another on the fly? Kindly Help!
<table class="table table-hover">
<tr>
<th>DOCTOR</th>
<th>SPECIALITY</th>
<th>CITY</th>
<th>CLASS</th>
</tr>
<tr ng-repeat="doc in doctors">
<td>{{ doc.contactName }}</td>
<td>{{ doc.speciality }}</td>
<td>{{ doc.townName }}</td>
<td>{{ doc.class }}</td>
<td>&gt</td>
</tr>
for this I use controller MenuCtrl. On click this should go to next page which uses PrecallCtrl. I want to display following thing on it.
<h2>{{contactid}}</h2>
getting contactid id is enough for me right now. rest is working.
You can use $routeParams for this. What you need to do is while defining your route for detail page add a named group argument such as
$routeProvider.when ('/details/:contactId', configObject).
When you now navigate to the details view the url that you should build should look like #/details/24.
On the details controller use the $routeParams collection to retrieve the contactId using $routeParams.contactId

Resources