I tried this student manager in HTML+ java last time. So now I want to try in angularJS. When I finished my code, it doesn't seem to work.
There are no error in console so I don't know where I went wrong. Can someone help me to figure out what the error is here?
HTML
<div ng-controll="MyController">
<form role="form" ng-submit="saveStudent()">
<div class="form-group mt-2">
<label for="" class="form-label">Student Name: </label>
<input type="text" required placeholder="Enter student name" class="form-control" ng-model="name">
</div>
<div class="form-group mt-2">
<label for="" class="form-label">Student DOB: </label>
<input type="date" placeholder="mm/dd/yyyy" class="form-control" ng-model="birth">
</div>
<div class="form-group mt-2">
<label for="" class="form-label">Student Gender: </label>
<input type="radio" name="sex" value="male" ng-model="sex" id="male">
<label for="male">Male</label>
<input type="radio" name="sex" value="female" ng-model="sex" id="female">
<label for="female">Female</label>
</div>
<div class="form-group mt-2">
<label for="" class="form-label">Student Language: </label>
<label for="" ng-repeat="language in languagesList">
<input type="checkbox" value="{{language.name}}" ng-checked="checkboxModel.indexOf(language) > -1" ng-click="addLanguage(language)">{{language}}
</label>
</div>
<div class="form-group mt-2">
<label for="">Student Class: </label>
<select name="student_class" class="form-control" ng-model="studentclass">
<option value="">---Select---</option>
<option value="{{ item }}" ng-repeat="item in classList">{{ item }}</option>
</select>
</div>
<div class="form-group mt-2 pb-3">
<button class="btn btn-danger" type="submit">Add</button>
</div>
</form>
<br><br><br>
<div class="card-table " style="margin-top: 20px;">
<h2 style="text-align: center;">Student List</h2>
<div class="content">
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>DOB</th>
<th>Gender</th>
<th>Language</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in studentList track by $index">
<td>{{ item.name }}</td>
<td>{{ item.studentclass }}</td>
<td>{{ item.birth | date}}</td>
<td>{{ item.sex }}</td>
<td>
<div ng-repeat="checkbox in item.checkboxModel">
{{ checkbox }}
</div>
</td>
<td>
<a href="#" class="btn btn-danger" ng-click="removeStudent($index)" style="color: blue;">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
Angular JS
var app = angular.module('MyApp', []);
app.controller('MyController', ['$scope', function ($scope) {
$scope.studentList = [];
$scope.classList = ['C2108G1',
'C2108G2',
'C2108G3',
'C2108G4',
'C2108G5',
'C2108L'
];
$scope.languagesList = [
'English',
'Vietnamese',
'Japanese',
'Chinese',
'French'
];
$scope.checkboxModel = [];
$scope.addLanguage = function (language) {
var index = $scope.checkboxModel.indexOf(language);
if (index > -1) {
$scope.checkboxModel.splice(index, 1);
} else {
$scope.checkboxModel.push(language);
}
}
$scope.saveStudent = function () {
var item = {
'name': $scope.name,
'birth': $scope.birth,
'sex': $scope.sex,
'studentclass': $scope.studentclass,
'checkboxModel': $scope.checkboxModel,
}
if ($scope.currentIndex >= 0) {
$scope.studentList[$scope.currentIndex] = item;
$scope.currentIndex = -1;
} else {
$scope.studentList.push(item);
}
}
$scope.currentIndex = -1;
$scope.removeStudent = function (index) {
var option = confirm('Are you sure you want to remove this student?');
if (!option) return;
$scope.studentList.splice(index, 1);
}
}]);
Related
Here is html code:
<div class="filters">
<div class="row">
<div class="col-md-3 filter">
<div class="form-group">
<label>Name</label>
<input type="text" value="" id="filter-by-name" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-3 filter">
<div class="form-group">
<label>Supplier</label>
<input type="text" value="" id="filter-by-supplier" class="form-control" data-id="" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>From Date</label>
<input type="number" min="0" value="" id="filter-by-fromdate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>To Date</label>
<input type="number" min="0" value="" id="filter-by-todate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>PriceNo</label>
<select id="filter-by-priceno" class="form-control">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
<table class="iblock table table-striped table-bordered table-hover table-order-items">
<thead>
<tr class="group-process">
<th>Name</th>
<th>Supplier</th>
<th>Fromdate</th>
<th>Todate</th>
<th colspan="3">PriceNo</th>
</tr>
</thead>
<tbody>
<tr class="odd group-process" data-id="2354031" style="background-color: rgb(255, 213, 128);">
<td>SEALING WASHER</td>
<td>
<select name="product-supplier_id[2354031-3-2]" class="form-control">
<option value="3" selected="">AGCO</option>
<option value="8">MAZZ</option>
<option value="9">Tomchuk</option>
<option value="88">ATTL</option>
</select>
</td>
<td><input type="text" name="product-delivery-from[2354031-3-2]" class="form-control text-right" value="0" placeholder="0"></td>
<td><input type="text" name="product-delivery-to[2354031-3-2]" class="form-control text-right" value="13" placeholder="13"></td>
<td>
<select name="product-price_no[2354031-3-2]" class="form-control text-right">
<option value="1">Standart</option>
<option value="2" selected="">Express</option>
<option value="3">Special Price</option>
</select>
</td>
</tr>
</tbody>
</table>
Also here is my Jquery code:
$("[id^=filter-by]").on('keyup change', function () {
var match = true;
var nameValue = $.trim($('#filter-by-name').val()).toLowerCase();
var supplierValue = $.trim($('#filter-by-supplier').val()).toLowerCase();
var fromdateValue = parseInt($('#filter-by-fromdate').val());
var todateValue = parseInt($('#filter-by-todate').val());
var pricenoValue = $('#filter-by-priceno').find(":selected").text();
$groupProcessingTable.find('tbody tr:not(:last-child)').each(function () {
if (nameValue != "") {
if (!($(this.children[0]).find('a').text().toLowerCase().indexOf(nameValue) > -1)) {
match = false;
}
if (supplierValue != "") {
if (!($(this.children[1]).find('option').filter(':selected').text().toLowerCase().indexOf(supplierValue) > -1)) {
match = false;
}
if (!isNaN(fromdateValue)) {
if (!(parseInt($(this.children[2]).find('input[name^=product-delivery-from]').val()) >= fromdateValue)) {
match = false;
}
if (!isNaN(todateValue)) {
if (!(parseInt($(this.children[3]).find('input[name^=product-delivery-to]').val()) <= todateValue)) {
match = false;
}
}
if (pricenoValue != "") {
if (!($(this.children[4]).find('a').text().toLowerCase().indexOf(pricenoValue) > -1)) {
match = false;
}
}
if (match) {
$(this).css("background-color", yellow);
} else {
$(this).css("background-color", '' );
}
});
});
My filter which contains inputs and selects has to work as Logical AND.
When all inputs and selects are true, this row have yellow background; otherwise this row's background color wil be transparent.
Please, help!
Thank You for all answers.
I tried to write Jquery code for solving using events, filter(), each(), flags.
I am expecting to get code for filtering table rows (by changing their background-color) with select and inputs in Jquery.
I've rewritten then code a bit, and for the test reason I have removed :not(:last-child) from $("#groupProcessingTable tbody tr")
The code will now only check each filter if i filter has a value.
$("#groupProcessingTable tbody tr").each(function() {
var match = (nameValue || supplierValue || fromdateValue || todateValue || pricenoValue);
if (nameValue != "") {
match = $(this).find("a").text().toLowerCase().includes(nameValue);
}
if (supplierValue != "") {
match = match && $(this).find("option:selected").text().toLowerCase().includes(supplierValue);
}
if (!isNaN(fromdateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-from]").val()) >= fromdateValue;
}
if (!isNaN(todateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-to]").val()) <= todateValue;
}
if (pricenoValue != "") {
match = match && $(this).find("select[name^=product-price_no] option:selected").text().toLowerCase() == pricenoValue;
}
if (match) {
$(this).css("background-color", "yellow");
} else {
$(this).css("background-color", "");
}
});
Demo
$("[id^=filter-by]").on('keyup change', function() {
var nameValue = $.trim($('#filter-by-name').val()).toLowerCase();
var supplierValue = $.trim($('#filter-by-supplier').val()).toLowerCase();
var fromdateValue = parseInt($('#filter-by-fromdate').val());
var todateValue = parseInt($('#filter-by-todate').val());
var pricenoValue = $('#filter-by-priceno option:selected').text().toLowerCase();
$("#groupProcessingTable tbody tr").each(function() {
var match = (nameValue || supplierValue || fromdateValue || todateValue || pricenoValue);
if (nameValue != "") {
match = $(this).find("a").text().toLowerCase().includes(nameValue);
}
if (supplierValue != "") {
match = match && $(this).find("option:selected").text().toLowerCase().includes(supplierValue);
}
if (!isNaN(fromdateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-from]").val()) >= fromdateValue;
}
if (!isNaN(todateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-to]").val()) <= todateValue;
}
if (pricenoValue != "") {
match = match && $(this).find("select[name^=product-price_no] option:selected").text().toLowerCase() == pricenoValue;
}
if (match) {
$(this).css("background-color", "yellow");
} else {
$(this).css("background-color", "");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="filters">
<div class="row">
<div class="col-md-3 filter">
<div class="form-group">
<label>Name</label>
<input type="text" value="" id="filter-by-name" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-3 filter">
<div class="form-group">
<label>Supplier</label>
<input type="text" value="" id="filter-by-supplier" class="form-control" data-id="" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>From Date</label>
<input type="number" min="0" value="" id="filter-by-fromdate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>To Date</label>
<input type="number" min="0" value="" id="filter-by-todate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>PriceNo</label>
<select id="filter-by-priceno" class="form-control">
<option value=""></option>
<option value="1">Standart</option>
<option value="2">Express</option>
<option value="3">Special Price</option>
</select>
</div>
</div>
</div>
</div>
<table id="groupProcessingTable" class="iblock table table-striped table-bordered table-hover table-order-items">
<thead>
<tr class="group-process">
<th>Name</th>
<th>Supplier</th>
<th>Fromdate</th>
<th>Todate</th>
<th colspan="3">PriceNo</th>
</tr>
</thead>
<tbody>
<tr class="odd group-process" data-id="2354031" style="background-color: rgb(255, 213, 128);">
<td>SEALING WASHER</td>
<td>
<select name="product-supplier_id[2354031-3-2]" class="form-control">
<option value="3" selected="">AGCO</option>
<option value="8">MAZZ</option>
<option value="9">Tomchuk</option>
<option value="88">ATTL</option>
</select>
</td>
<td><input type="text" name="product-delivery-from[2354031-3-2]" class="form-control text-right" value="0" placeholder="0"></td>
<td><input type="text" name="product-delivery-to[2354031-3-2]" class="form-control text-right" value="13" placeholder="13"></td>
<td>
<select name="product-price_no[2354031-3-2]" class="form-control text-right">
<option value="1">Standart</option>
<option value="2" selected="">Express</option>
<option value="3">Special Price</option>
</select>
</td>
</tr>
</tbody>
</table>
I want to display two columns from two different tables.
I have table "offices" and "cash_desks"
Table "offices" have the column "name" which I want to display.
Table "cash_desks" have the columns "name" and "office_id"(which refers to offices id).
I want to display offices name and cash_desks name in HTML table.
Here is my HTML code:
<table class="table">
<tr ng-repeat="cash_desks in items" ng-click="editItem(cash_desks)">
<td>{{cash_desks.name}}</td>
<td>{{offices.name}}</td>
</tr>
</table>
<label><input type="checkbox" ng-model="myVar">Редактирай офис</label>
<div ng-show="myVar">
<div class="form">
<form ng-submit="saveItem(cash_desksForm.$valid)" name="cash_desksForm">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="offices_name">Код на валута</label>
<input type="text" class="form-control" required ng-model="activeItem.name" placeholder="Офис.." />
</div>
<div class="form-group">
<label for="value_name">Локация на офис</label>
<input type="text" class="form-control" id="password" ng-model="activeItem.location" />
</div>
</div>
</div>
<button class="btn btn-primary" ng-disabled="cash_desksForm.$invalid" type="submit">Save</button>
<!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
</form>
</div>
</div>
And Angular Code:
app.controller("CashDesksCtrl", function($rootScope, $scope){
$scope.items = [];
$scope.editMode = false;
$scope.activeItem = false;
$scope.refresh = function () {
$scope.items = [];
window.defaultAdapter.query("SELECT offices.name,cash_desks.name FROM offices LEFT JOIN cash_desks ON offices.id = cash_desks.office_id LIMIT 8", { type: Sequelize.QueryTypes.SELECT})
.then(cash_desks => {
$scope.items = cash_desks;
$scope.$apply();
})
};
You have same key (name) from your select , try to use alias to make them differents.
<table class="table">
<tr ng-repeat="itemin items" ng-click="editItem(cash_desks)">
<td>{{item.cash_desk_name}}</td>
<td>{{item.office_name}}</td>
</tr>
</table>
<label><input type="checkbox" ng-model="myVar">Редактирай офис</label>
<div ng-show="myVar">
<div class="form">
<form ng-submit="saveItem(cash_desksForm.$valid)" name="cash_desksForm">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="offices_name">Код на валута</label>
<input type="text" class="form-control" required ng-model="activeItem.name" placeholder="Офис.." />
</div>
<div class="form-group">
<label for="value_name">Локация на офис</label>
<input type="text" class="form-control" id="password" ng-model="activeItem.location" />
</div>
</div>
</div>
<button class="btn btn-primary" ng-disabled="cash_desksForm.$invalid" type="submit">Save</button>
<!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
</form>
</div>
</div>
app.controller("CashDesksCtrl", function($rootScope, $scope){
$scope.items = [];
$scope.editMode = false;
$scope.activeItem = false;
$scope.refresh = function () {
$scope.items = [];
window.defaultAdapter.query("SELECT offices.name as office_name,cash_desks.name as cash_desk_name FROM offices LEFT JOIN cash_desks ON offices.id = cash_desks.office_id LIMIT 8", { type: Sequelize.QueryTypes.SELECT})
.then(cash_desks => {
$scope.items = cash_desks;
$scope.$apply();
})
};
Whilst debugging; the id for each row is found when the edit button is clicked but it seems to get stuck at that point. The data for the said row will not populate the modal. I would appreciate any help at all! Here is a picture in chrome. I have exhausted all of my ideas. I do have an error for localhost failed to load resource for an image folder, could this be causing conflict? I don't see how.
HTML
<div class="tab-pane" id="admin">
<br>
<div class="container">
<table id="admin_table" class="display">
<thead>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Platform</th>
<th>Score Phrase</th>
<th>Score</th>
<th>Release Year</th>
<th>Release Month</th>
<th>Release Day</th>
<th>Editors Choice</th>
<th>Edit</th>
<th>x</th>
</tr>
<tbody id="admin_table_body">
</tbody>
</table>
</div> <br><br><br><br>
</div>
</div>
>Modal
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Edit</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" method="post">
<div class="form-group">
<label class="col-sm-4 control-label"><strong>ID:</strong></label>
<input type="text" style="height:32px;" name="id" id="id" disabled/>
<label for="id" class="error"></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Title:</strong></label>
<input type="text" style="height:32px;" id="title" name="title"/>
<label for="title" class="error" ></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Genre:</strong></label>
<input type="text" style="height:32px;" id="genre" name="genre"/>
<label for="genre" class="error" ></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Platform:</strong></label>
<input type="text" style="height:32px;" id="platform" name="platform"/>
<label for="platform" class="error" ></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Score:</strong></label>
<input type="text" id="score" name="score" style="height:32px;"/>
<label for="score" class="error"></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Score Phrase:</strong></label>
<input type="text" id="score_phrase" name="score_phrase" style="height:32px;"/>
<label for="score_phrase" class="error"></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Release Year:</strong></label>
<input type="text" id="release_year" name="release_year" style="height:32px;"/>
<label for="release_year" class="error"></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Release Month:</strong></label>
<textarea id="release_month" name="release_month" style="height:32px;"></textarea>
<label for="release_month" class="error"></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Release Day:</strong></label>
<textarea id="release_day" name="release_day" style="height:32px;"></textarea>
<label for="release_day" class="error"></label>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"><strong>Editors Choice:</strong></label>
<textarea id="editors_choice" name="editors_choice" style="height:32px;"></textarea>
<label for="editors_choice" class="error"></label>
</div>
</form>
</div>
Main.js
var rootURL ="http://localhost:4006/GamesAPI/api/games";
var currentGame;
//when the DOM is ready
$(document).ready(function(){
findAll();
//findById();
$(document).on("click","#admin_table_body a",function(){findById(this.id);});
// $(document).on("click","#addButton",function(){addGame();});
// $(document).on("click","#deleteButton",function(){deleteGame();});
});
var findAll=function(){
console.log('findAll');
$.ajax({
type: 'GET',
url: rootURL,
dataType: "json", // data type of response
success: renderList
});
};
var findById = function(id)
{
console.log('findById: '+id);
$.ajax({
type: 'GET',
url: rootURL + '/' + id,
dataType: "json",
//Gets stuck here
success: function(data){
//$('#btnDelete').show();
console.log('findById success: ' +data.title);
currentGame = data;
renderDetails(currentGame);
}
});
};
function renderList(data){
list = data.games;
console.log("renderList");
$('#admin_table_body tr').remove();
$.each(list, function(index, games){
$('#admin_table_body').append('<tr><td>' +games.title+'</td><td>'+games.genre+'</td><td>'
+games.platform+'</td><td>' +games.score_phrase+'</td><td>'
+games.score+'</td><td>'+games.release_year+'</td><td>'+games.release_month+'</td><td>'
+games.release_day+'</td><td>'+games.editors_choice+'</td><td>\n\
Edit</td>\n\
<td id="'+games.id+'"><button type="button" id="deleteButton" class="btn btn-success">Delete</button></td></tr>');
});
$('#admin_table').DataTable();
// $('gameList').append('<div class="row">');
//The rest of this function is to populate a different client page
output='<div class="row">';
$.each(list, function(index,games){
var img="pics/"+games.picture;
output+=('<div class="col-sm-6 col-md-4 col-lg-3"><div class="card"><img src='+'"'+img+'"'+
'height="150"><p>Title: '+games.title+'</p><p>Genre: '+games.genre+'</p><p>Platform: '+games.platform+
'</p><p>Score: '+games.score+' '+games.score_phrase+'</p></div></div>');
// $('#gameList').append('<div class="col-sm-6 col-md-4 col-lg-3"><div class="card">'+game.title+'</div></div>');
});
// $('#gameList').append('</div>');
output+='</div>';
$('#productList').append(output);
};
var renderDetails = function(games)
{
$('#id').val(games.id);
$('#title').val(games.title);
$('#url').val(games.url);
$('#platform').val(games.platform);
$('#score').val(games.score);
$('#score_phrase').val(games.score_phrase);
$('#genre').val(games.genre);
$('#pic').attr('src', 'pics/' + games.picture);
$('#editors_choice').val(games.editors_choice);
$('#release_year').val(games.release_year);
$('#release_month').val(games.release_month);
$('#release_day').val(games.release_day);
};
DatabaseMethod.php
function getGame($id) {
$query = "SELECT * FROM games WHERE id = '$id'";
try {
global $db;
$games = $db->query($query);
$game = $games->fetch(PDO::FETCH_ASSOC);
header("Content-Type: application/json", true);
echo $query;
echo json_encode($game);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
Your server-side PHP method is outputting data which is not valid JSON.
Remove
echo $query;
from your code, since it's just left over from debugging. This is preventing jQuery from seeing the whole response as JSON and parsing it accordingly.
I am writing a small program in Vue.js, but I am getting stuck with an error.
I want the X button to remove each item in a list. I used splice(index,1), but the item isn't being removed. What am I missing?
My HTML:
<div id="app" class="container">
<div class="row">
<div class="col-md-4">
<h1>Add Student</h1>
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" v-model="newStudent.name" class = "form-control" placeholder="Student Name">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" id="address" v-model="newStudent.address" class = "form-control" placeholder="Address">
</div>
<button class="btn btn-success" v-on:click = "addStudent">Add</button>
</div>
<div class="col-md-8">
<h1>All Students</h1>
<table class="table table-striped">
<thead>
<tr>
<td>Name</td>
<td>Address</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<tr v-for = "student in students">
<td>{{student.name}}</td>
<td>{{student.address}}</td>
<td><button type="button" class="btn btn-danger" v-on:click="deleteStudent($index)">X</button></td>
</tr>
</tbody>
</table>
</div>
</div> <!--End of Row-->
<br>
<br>
<div class="row">
<div class="col-md-12">
<pre>{{ $data | json }}</pre>
</div>
</div>
</div>
My JS:
<script>
new Vue({
el: "#app",
data: {
newStudent: {name: "", address: ""},
students: []
},
methods: {
addStudent: function(){
var name = this.newStudent.name.trim();
var address = this.newStudent.address.trim();
if(name && address){
this.students.push({name: name, address: address});
this.newStudent = {name: "", address: ""};
$("#name").focus();
}
},
deleteStudent: function(index){
this.students.splice(index,1)
}
}
});
</script>
The $index variable was removed in Vue 2. So, you're not passing the index in correctly.
Specify the index variable in the v-for and pass that to the deleteStudent method instead:
<tr v-for="student, index in students">
<td>{{student.name}}</td>
<td>{{student.address}}</td>
<td>
<button
type="button"
class="btn btn-danger"
v-on:click="deleteStudent(index)"
>X</button>
</td>
</tr>
This is covered in the documentation on list rendering in Vue.
This is a project I'm doing to learn Angular JS. It's a single page web application that allows to log contacts and edit them. For some reason that I can't figure out, my web page freezes any time I post data to Firebase. Can anyone help with this?
This my javascript file
'use strict';
angular.module('myContacts.contacts', ['ngRoute','firebase'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/contacts', {
templateUrl: 'contacts/contacts.html',
controller: 'contactsCtrl'
});
}])
.controller('contactsCtrl', ['$scope','$firebaseArray', function($scope, $firebaseArray) {
var ref = firebase.database().ref();
$scope.contacts = $firebaseArray(ref);
//console.log($scope.contacts);
$scope.showAddForm = function(){
$scope.addFormShow = true;
}
$scope.hide = function(){
$scope.addFormShow = false;
}
$scope.addFormSubmit = function(){
console.log('adding contact');
//Assign values
if($scope.name){var name = $scope.name}else {name=null;}
if($scope.email){var email = $scope.email}else {email=null;}
if($scope.company){var company = $scope.company}else {company=null;}
if($scope.mobile_phone){var mobile_phone = $scope.mobile_phone}else { mobile_phone=null;}
if($scope.home_phone){var home_phone = $scope.home_phone}else {home_phone=null;}
if($scope.work_phone){var work_phone = $scope.work_phone}else {work_phone=null;}
if($scope.street_address){var street_address = $scope.street_address}else {street_address=null;}
if($scope.city){var city = $scope.city}else {city=null;}
if($scope.province){var province = $scope.province}else {province=null;}
if($scope.postal_code){var postal_code = $scope.postal_code}else {postal_code=null;}
//Build Object
$scope.contacts.$add({
name:name,
email:email,
company:company,
phone:[
{
mobile:mobile_phone,
home:home_phone,
work:work_phone
}
],
address:[
{
street_address:street_address,
city: city,
province: province,
postal_code: postal_code
}
]
}).then(function(ref){
var id = ref.key();
console.log('added contact with id: '+ id);
//clear form
clearFields();
hide form
$scope.addFormShow = false;
//send message
$scope.msg ="contact Added";
});
}
//Clear $scope Fields
function clearFields(){
console.log('Clearing All Fields');
$scope.name = '';
$scope.email = '';
$scope.company = '';
$scope.mobile_phone = '';
$scope.home_phone = '';
$scope.work_phone = '';
$scope.street_address = '';
$scope.city = '';
$scope.province = '';
$scope.postal_code = '';
}
}]);
and here is the HTML file
<div class="row" ng-controller="contactsCtrl">
<div class="large-10 columns">
<!--<div data-alert ng-show="msg" class="alert-box">{{msg}}</div>-->
<form ng-submit="addFormSubmit()" ng-show="addFormShow">
<h3>Add Contact</h3>
<!--Add Form-->
<div class="row">
<div class="large-6 columns">
<label>Name:
<input type="text" ng-model="name" placeholder="Contact Name" required/>
</label>
</div>
<div class="large-6 columns">
<label>Email:
<input type="text" ng-model="email" placeholder="Contact Email" required/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Company:
<input type="text" ng-model="company" placeholder="Company Name"/>
</label>
</div>
<div class="large-6 columns">
<label>Work Phone:
<input type="text" ng-model="work_phone" placeholder="Work Phone"/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Mobile Phone:
<input type="text" ng-model="mobile_phone" placeholder="Mobile Phone" />
</label>
</div>
<div class="large-6 columns">
<label>Home Phone:
<input type="text" ng-model="home_phone" placeholder="Mobile Phone"/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Street Address:
<input type="text" ng-model="street_address" placeholder="Street Name"/>
</label>
</div>
<div class="large-6 columns">
<label>City:
<input type="text" ng-model="city" placeholder="City"/>
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Province:
<input type="text" ng-model="state" placeholder="Province"/>
</label>
</div>
<div class="large-6 columns">
<label>Postal Code:
<input type="text" ng-model="postal_code" placeholder="Postal Code"/>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<input type="submit" value="Add Contact" class="button"/>
</div>
</div>
</form>
<h3>Your Contacts (3)</h3>
<table>
<thead>
<tr>
<th width="200px">Name</th>
<th width="200px">Company</th>
<th width="25%">Email</th>
<th width="25%">Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts">
<td>{{contact.name}}</td>
<td>{{contact.company}}</td>
<td>{{contact.email}}</td>
<td><a class="button tiny" ng-click="showEditForm(contact)">Edit</a><a class="button tiny alert" ng-click="removeContact(contact)">Delete</a></td>
</tr>
</tbody>
</table>
</div>
<div class="small-12 large-2 columns">
<a class="button large" ng-click="showAddForm()" ng-hide="addFormShow">+</a>
<a class="button large" ng-click="hide()" ng-show="addFormShow">-</a>
</div>
</div>
While checking your code I have the feeling that you are probably not initializing the firebase module.
So you should do it first
Config firebase.initializeApp() or it will not work
.config(function () {
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
});
Check this example, please use your own firebase initialization info to make it work I made your code work on muy test.
Example: https://jsfiddle.net/moplin/zLozccap/