bootstrap table is not working properly - angularjs

ok, I have a issue with my bootstrap table. I have a Employee Modal with 2 tabs. The first tab is the table with the current Employees. When you select any Employee it takes you to the 2nd tab which has the Employee details. There is a input field on top of the table that allows searching, and if the Employee is not in there by the typed name, the user can hit Enter and it takes you to the details tab to create a new entry for that Employee .When I select a Employee and it takes me to the details tab, I want to be able to go back to the table and select a new Employee if need be. problem is that Employee is a empty row on the table. i need to reopen the modal for it to reappear.
plunkr
<div class="container">
<form class="form-horizontal" ng-submit="submitEmployee()" enctype="multipart/form-data">
<tabset>
<tab heading="List" select="ClearEmployeeModalFields();">
<div class="col-xs-12" style="margin-top:20px;width:initial">
<div style="overflow: auto;height:190px;max-width:520px;min-width:520px" id="scrollAreaCustomers">
<table class="table" style="">
<tr>
<th style="font-weight: bold;">Name</th>
</tr>
<tr>
<input type="text" placeholder="New Employee" ng-model="selectedEmployee.EmployeeFirstName" ng-enter="data.static = true" />
</tr>
<tr ng-repeat="employee in employeeArray | filter:selectedEmployee.EmployeeFirstName" class="pointer">
<td ng-click="setSelectedEmployee(employee);data.static = true">{{employee.EmployeeFirstName}} {{employee.EmployeeLastName}}</td>
</tr>
</table>
</div>
</div>
<!--End col-xs-12-->
</tab>
<tab heading="Details" active="data.static">
<div class="col-xs-12" style="margin-top:20px">
<div class="inline-fields" style="">
<label style="margin-left:-11px">Employee Id:</label>
<input style="width:100px" ng-model="selectedEmployee.CompanyEmployeeId" type="text">
<label style="margin-left:100px">Email:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeEmail" type="email">
</div>
<div class="inline-fields">
<label style="margin-left:0px">First Name:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeFirstName" type="text">
<label style="margin-left:57px">Title:</label>
<select style="width:150px" ng-model="selectedEmployee.EmployeeTitle">
<option value="" selected="selected">Select</option>
<option value="Manager" ng-model="selectedEmployee.EmployeeTitle">Manager</option>
<option value="Admin" ng-model="selectedEmployee.EmployeeTitle">Admin</option>
<option value="OfficeBitch" ng-model="selectedEmployee.EmployeeTitle">Office Bitch</option>
</select>
</div>
<div class="inline-fields">
<label style="margin-left:1px">Last Name:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeLastName" type="text">
<label style="margin-left:66px">PM:</label>
<select style="width:150px" ng-model="selectedEmployee.EmployeeIsPM" ng-options="o.v as o.n for o in [{ n: 'No', v: false }, { n: 'Yes', v: true }]">
<option value="true">True</option>
<option value="false">False</option>
</select>
</div>
<div class="inline-fields">
<label style="margin-left:0px">Cell Phone:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeCellPhone" type="text" ui-mask="{{'(999) 999-9999'}}">
<label style="margin-left:46px">Super:</label>
<select style="width:150px" ng-model="selectedEmployee.EmployeeIsSuper" ng-options="o.v as o.n for o in [{ n: 'No', v: false }, { n: 'Yes', v: true }]">
<option value="true">True</option>
<option value="false">False</option>
</select>
</div>
<div class="inline-fields">
<label style="margin-left:-14px">Office Phone:</label>
<input style="width:150px" ng-model="selectedEmployee.EmployeeOfficePhone" type="text" ui-mask="{{'(999) 999-9999'}}">
</div>
</div>
</tab>
</tabset>
<!--End Tab Content-->
<br />
<div class="col-xs-12" style="margin-top: 220px;position:absolute">
<input style="margin-left: 3px; width: 70px" ng-click="updateEmployee(selectedEmployee)" type="button" value="Update" go-click="#" />
<input style="margin-left:285px;width:70px" type="submit" value="Save" go-click="#" />
<input style="margin-left: 20px; width: 70px" type="button" ng-if="true" data-dismiss="modal" value="Exit" go-click="#" />
</div>
Controller
//Sync Employee Table with Input Fields "New Employee Modal
$scope.setSelectedEmployee = function (employee) {
$scope.selectedEmployee = employee;
}
//Activate tab on selection
$scope.data = { static: false }
//GET Employees
EmployeeGet.query().then(function (data) {
$scope.employeeArray = data;
}, function (reason) {
errorMngrSvc.handleError(reason);
});
//Clear Employee Search Input Fields
$scope.ClearEmployeeModalFields = function () {
$scope.selectedEmployee.EmployeeCompanyId = '';
$scope.selectedEmployee.EmployeeFirstName = '';
$scope.selectedEmployee.EmployeeLastName = '';
$scope.selectedEmployee.EmployeeTitle = '';
$scope.selectedEmployee.EmployeeCellPhone = '';
$scope.selectedEmployee.EmployeeOfficePhone = '';
$scope.selectedEmployee.EmployeeEmail = '';
$scope.selectedEmployee.CompanyEmployeeId = '';
$scope.selectedEmployee.EmployeeIsSuper = '';
$scope.selectedEmployee.EmployeeIsPM = '';
};

I think the problem is when you are trying to do the object assignment. In Javascript, primitive types are copied by value and reference types are copied by reference.
$scope.setSelectedEmployee = function (employee) {
$scope.selectedEmployee = employee;
};
So when you deleting the $scope.selectedEmployee using $scope.ClearEmployeeModalFields function, it will delete the object in the array of $scope.employeeArray since it shares the same reference.
Try to replace your function with this to copy instead of pointing to same data:
$scope.setSelectedEmployee = function (employee) {
angular.extend($scope.selectedEmployee, employee);
};

Related

How to Increment Dynamic field ngModel name in Angularjs..?

Here when i click Add Product Button it creates a text field. But I want to create create different name for each text box in ng-model="column.product_cgst".
like column.product_cgst-1, column.product_cgst-2.
<form>
<div ng-repeat="column in columns" style="margin-bottom: 10px;">
<div class="form-group">
<input type="text" name="product_name" ng-model="column.product_name" required placeholder="Product Name" class="form-control"
id="userName">
</div>
<div class="form-group">
<select name="units" ng-model="column.units" class="form-control">
<option value="">Select Units</option>
<option ng-repeat="units in allunits" value="{{units.unit_id}}">
{{units.unit_name}}
</option>
</select>
</div>
<div class="form-group">
<input id="cgst" type="text" ng-model="column.product_cgst" placeholder="Enter CGST" required class="form-control">
</div>
<div class="form-group">
<input id="sgst" type="text" ng-model="column.product_sgst" placeholder="Enter SGST" required class="form-control">
</div>
<div class="form-group">
<input id="igst" type="text" ng-model="column.product_igst" placeholder="Enter IGST" required class="form-control">
</div>
<button class="remove btn-sm btn-danger" ng-click="removeColumn($index)">x</button>
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit">
Submit
</button>
<button class="addfields btn btn-info waves-effect waves-light" ng-click="addProduct()">Add Product</button>
</div>
</form>
Controller:
$scope.columns = [];
$scope.addProduct = function () {
var newItemNo = $scope.columns.length + 1;
$scope.columns.push({ 'colId': 'col' + newItemNo });
};
$scope.removeColumn = function (index) {
$scope.columns.splice(index, 1);
if ($scope.columns.length() === 0 || $scope.columns.length() == null) {
alert('no rec');
$scope.columns.push = [{ "colId": "col1" }];
}
};
You can use $index in the HTML to increment your ng-model dynamically instead of doing in controller.
Using $index in the interpolation with ngModel will not work. Try something like below
column.product_cgst[$index]
I get the Solutions here,only change in my angularjs controllers.
//Add Dynamic Column
$scope.columns = [{id: 'firstField'}];
$scope.addProduct = function(){
var newItemNo = $scope.columns.length+1;
$scope.columns.push({'id':'field'+newItemNo});
}
//Remove Dynamic Column
$scope.removeColumn = function() {
var itemLast = $scope.columns.length-1;
$scope.columns.splice(itemLast);
};
//Insert Data in Database:-
$scope.productAdd = function(){
$http.post('product/insert_product', $scope.columns)
.success(function(data){
console.log(data);
});
}

Validate form only if one or more field contains a number, Angular.JS

I want to validate an order form with Angular.JS.
The form dynamically adds input fields depending on how many products there are.
The dynamically added input fields are used to determine how many products the user wants to purchase.
I want the form to validate if either one of the fields has a value of minimum 1 (the user has to choose at least one product in order to send the form).
The issue I am having is that I cannot work out how the conditional validation "if one field has a value greater than 0 the form is valid".
If I put in min='1' as an attribute for the input field, both field needs to have a value of 1 in order for the form to work.
If I have a placeholder="0" the input fields has a value and therefore will validate the form as long as the other fields are valid.
My idea which I do not know how to implement (or if it's smart) is to check the products total sum is greater than 0.
Obviously I am a newbie and use a lot of resources online to implement functionality I want to have.
HTML
<form id="orderForm" name="orderForm" role="form" ng-submit="submitForm(orderForm.$valid)" class="orderForm" ng-controller="formController as formCtrl" novalidate>
<table class="table">
<thead>
<tr>
<th scope="col">Produkt</th>
<th scope="col">Antal</th>
<th scope="col" class="text-right">Pris</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in formCtrl.products">
<td>
<img ng-src="{{product.images.thumb}}" alt="{{product.name}}">
<h6 class="d-inline-block">{{product.name}} <small class="d-block">{{product.description}}</small></h6>
</td>
<td>
<input type="number" class="form-control" ng-model="product.quantity" name="{{'quantity' + product.productId}}" min="0" max="99" required>
</td>
<td class="text-right">
{{product.price | currency : "SEK"}}
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3" class="text-right">
<h5 class="d-inline-block">Totalt: <em ng-bind="sumCalc(product.price) | currency : 'SEK'"></em></h5>
</td>
</tr>
</tfoot>
</table>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="inputEmail">Email</label>
<input name="email" ng-model="formCtrl.email" type="email" class="form-control" id="inputEmail" placeholder="Email" required>
<div ng-show="orderForm.email.$invalid && !orderForm.email.$pristine" class="invalid-feedback">
Vänligen fyll i en korrekt email
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputFirstName">Förnamn</label>
<input name="firstname" ng-model="formCtrl.fName" type="text" class="form-control" id="inputFirstName" placeholder="Förnamn" required>
<div ng-show="orderForm.fName.$invalid && !orderForm.fName.$pristine" class="invalid-feedback">
Vänligen fyll i ditt förnamn
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputLastName">Efternamn</label>
<input name="lastname" ng-model="formCtrl.lName" type="text" class="form-control" id="inputLastName" placeholder="Efternamn" required>
<div ng-show="orderForm.lName.$invalid && !orderForm.lName.$pristine" class="invalid-feedback">
Vänligen fyll i ditt efternamn
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="inputStreet">Gatuadress</label>
<input name="street" ng-model="formCtrl.street" type="text" class="form-control" id="inputStreet" placeholder="Gatuadress" required>
<div ng-show="orderForm.street.$invalid && !orderForm.street.$pristine" class="invalid-feedback">
Vänligen fyll i din gatuaddress
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputZip">Postnr</label>
<input name="zip" ng-model="formCtrl.zip" type="text" class="form-control" id="inputZip" placeholder="Postnummer" required>
<div ng-show="orderForm.zip.$invalid && !orderForm.zip.$pristine" class="invalid-feedback">
Vänligen fyll i ditt postnummer
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputTown">Stad</label>
<input name="town" ng-model="formCtrl.town" type="text" class="form-control" id="inputTown" placeholder="Stad" required>
<div ng-show="orderForm.town.$invalid && !orderForm.town.$pristine" class="invalid-feedback">
Vänligen fyll i din stad
</div>
</div>
</div>
<div> orderForm is {{orderForm.$valid}}</div>
<div class="form-group">
<button type="submit" class="btn btn-default" id="inputSubmitButton" ng-disabled="orderForm.$invalid && inputQuantity.">Skicka</button>
</div>
</form>
controllers.js
var app = angular.module('controllers', []);
app.controller('formController', function($scope) {
this.products = product;
$scope.submitForm = function(isValid) {
// check to make sure the form is completely valid
if (isValid) {
alert('our form is amazing');
}
};
$scope.sumCalc = function() {
var sum = 0;
angular.forEach(product, function(product, index) {
sum += parseInt(product.quantity, 10) * product.price;
});
return sum;
//totalPrice = sum;
};
});
var product = [{
name: 'asd',
productId: 01,
price: 100,
description: 'asd',
specification: "asd",
images: {
full: 'asd',
thumb: 'asd'
},
quantity: 0
},
{
name: "asd",
productId: 02,
price: 100,
description: "asd",
specification: "asd",
images: {
full: 'asd',
thumb: 'asd'
},
quantity: 0
}
];
var app = angular.module('controllers', []);
app.controller('formController', function($scope) {
this.products = product;
$scope.submitForm = function(isValid) {
// check to make sure the form is completely valid
if (isValid) {
alert('our form is amazing');
}
};
$scope.sumCalc = function() {
var sum = 0;
angular.forEach(product, function(product, index) {
sum += parseInt(product.quantity, 10) * product.price;
});
return sum;
//totalPrice = sum;
};
});
var product = [{
name: 'asd',
productId: 01,
price: 100,
description: 'asd',
specification: "asd",
images: {
full: 'asd',
thumb: 'asd'
},
quantity: 0
},
{
name: "asd",
productId: 02,
price: 100,
description: "asd",
specification: "asd",
images: {
full: 'asd',
thumb: 'asd'
},
quantity: 0
}
];
Edit your HTML as following:
<form id="orderForm" name="orderForm" role="form" ng-submit="submitForm(orderForm.$valid)" class="orderForm" ng-controller="formController as formCtrl" novalidate>
<table class="table">
<thead>
<tr>
<th scope="col">Produkt</th>
<th scope="col">Antal</th>
<th scope="col" class="text-right">Pris</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in formCtrl.products">
<td>
<img ng-src="{{product.images.thumb}}" alt="{{product.name}}">
<h6 class="d-inline-block">{{product.name}} <small class="d-block">{{product.description}}</small></h6>
</td>
<td>
<input type="number" class="form-control" ng-model="product.quantity" name="{{'quantity' + product.productId}}" min="0" max="99" required>
</td>
<td class="text-right">
{{product.price | currency : "SEK"}}
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3" class="text-right">
<h5 class="d-inline-block">Totalt: <em ng-bind="sumCalc(product.price) | currency : 'SEK'"></em></h5>
</td>
</tr>
</tfoot>
</table>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="inputEmail">Email</label>
<input name="email" ng-model="formCtrl.email" type="email" class="form-control" id="inputEmail" placeholder="Email" required>
<div ng-show="orderForm.email.$invalid && !orderForm.email.$pristine" class="invalid-feedback">
Vänligen fyll i en korrekt email
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputFirstName">Förnamn</label>
<input name="firstname" ng-model="formCtrl.fName" type="text" class="form-control" id="inputFirstName" placeholder="Förnamn" required>
<div ng-show="orderForm.fName.$invalid && !orderForm.fName.$pristine" class="invalid-feedback">
Vänligen fyll i ditt förnamn
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputLastName">Efternamn</label>
<input name="lastname" ng-model="formCtrl.lName" type="text" class="form-control" id="inputLastName" placeholder="Efternamn" required>
<div ng-show="orderForm.lName.$invalid && !orderForm.lName.$pristine" class="invalid-feedback">
Vänligen fyll i ditt efternamn
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="inputStreet">Gatuadress</label>
<input name="street" ng-model="formCtrl.street" type="text" class="form-control" id="inputStreet" placeholder="Gatuadress" required>
<div ng-show="orderForm.street.$invalid && !orderForm.street.$pristine" class="invalid-feedback">
Vänligen fyll i din gatuaddress
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputZip">Postnr</label>
<input name="zip" ng-model="formCtrl.zip" type="text" class="form-control" id="inputZip" placeholder="Postnummer" required>
<div ng-show="orderForm.zip.$invalid && !orderForm.zip.$pristine" class="invalid-feedback">
Vänligen fyll i ditt postnummer
</div>
</div>
<div class="col-md-4 mb-3">
<label for="inputTown">Stad</label>
<input name="town" ng-model="formCtrl.town" type="text" class="form-control" id="inputTown" placeholder="Stad" required>
<div ng-show="orderForm.town.$invalid && !orderForm.town.$pristine" class="invalid-feedback">
Vänligen fyll i din stad
</div>
</div>
</div>
<div> orderForm is {{orderForm.$valid}}</div>
<div class="form-group">
<button type="submit" class="btn btn-default" id="inputSubmitButton" ng-disabled="orderForm.$invalid || !checkProductSelected()">Skicka</button> <!--Pay attention to the 'ng-disabled' binding-->
</div>
</form>
and modify your Javascript to look like this:
app.controller('formController', function($scope) {
this.products = product;
$scope.submitForm = function(isValid) {
// check to make sure the form is completely valid
if (isValid) {
alert('our form is amazing');
}
};
$scope.sumCalc = function() {
var sum = 0;
angular.forEach(product, function(product, index) {
sum += parseInt(product.quantity, 10) * product.price;
});
return sum;
//totalPrice = sum;
};
//***The following function is added to your original code***
$scope.checkProductSelected = function() {
var sum = 0;
angular.forEach(product, function(product, index) {
sum += parseInt(product.quantity, 10) * product.price;
});
return sum > 0;
}
});
Look for the comments in the code to see what has been added or edited. (On HTML it's after your submit button)

Can't get option value when using Angularjs

I have the following AngularJS code:
angular.module('loggedInApp', ['ui.bootstrap']);
var myapp = angular.module('loggedInApp', ['ui.bootstrap'])
myapp.controller('AddParentController', function ($scope, addParentService) {
var vm = this;
$scope.addParentService = addParentService;
$scope.setName = function (val) {
addParentService.inputParentName = val;
}
$scope.setEmail = function (val) {
addParentService.inputParentEmail = val;
}
$scope.setCarrier = function (val) {
addParentService.inputParentCarrier = val;
}
$scope.setBirthday = function (val) {
addParentService.inputParentBirthday = val;
}
});
myapp.service('addParentService', function () {
var vm = this;
vm.eventObjs = [];
vm.parent = [];
vm.addParent = function () {
alert(vm.inputParentName);
alert(vm.inputParentBirthday);
alert(vm.inputParentEmail);
alert(vm.inputParentCellPhone);
alert(vm.inputParentCarrier);
vm.parent.push({
name: vm.inputParentName, dob: vm.inputParentBirthday,
cell: vm.inputParentCellPhone, carrier: vm.inputParentCarrier,
email: vm.inputParentEmail, active: true, personId: vm.parent.length + 1
});
vm.inputParentName = '';
vm.inputParentDOB = '';
vm.inputParentCellPhone = '';
vm.inputParentCarrier = 0;
vm.inputParentEmail = '';
vm.active = true;
};
vm.buildEventObject = function (titleValue, startValue, personId, choreIdValue) {
vm.eventObjs.push({ title: titleValue, start: startValue, familymemberpersonid: personId, choreId: choreIdValue });
return vm.eventObjs;
}
vm.returnEventObject = function () {
return vm.eventObjs;
}
});
My HTML looks like this:
<div class="row clearfix" ng-controller="AddParentController as parent">
<div class="col-md-6 column">
<form role="form" ng-submit="addParentService.addParent()">
<div class="form-group">
<label for="inputParentName">Name</label><input class="form-control" id="inputParentName" value="" type="text" ng-model="addParentService.inputParentName" />
</div>
<div class="form-group">
<label for="inputParentBirthday">Birthday</label><input class="form-control" id="inputParentBirthday" value="" type="text" ng-model="addParentService.inputParentBirthday" />
</div>
<div class="form-group">
<label for="inputParentCellPhone">Cell Phone</label><input class="form-control" id="inputParentCellPhone" value="" type="text" ng-model="addParentService.inputParentCellPhone" />
</div>
<div class="form-group">
<label for="inputParentCarrier">Phone Carrier</label><br />
<select class="form-control" id="inputParentCarrier">
<option>ATT</option>
<option>Cricket</option>
<option>Sprint</option>
<option>T-Mobile</option>
<option>Verizon</option>
</select>
</div>
<div class="form-group">
<label for="inputParentEmail">E-Mail Address</label><input class="form-control" id="inputParentEmail" value="" type="email" ng-model="addParentService.inputParentEmail" />
</div>
<button class="btn btn-default" type="submit">Submit</button>
</form>
</div>
<br /><br />
<div class="col-md-6 column">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Parent:
</h3>
</div>
<div class="panel-body">
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Cell #</th>
<th>E-Mail</th>
<th>DOB</th>
</tr>
<tr ng-repeat="parent in addParentService.parent">
<td>{{parent.name}}</td>
<td>{{parent.cell}}</td>
<td>{{parent.email}}</td>
<td>{{parent.dob}}</td>
</tr>
</table>
</div>
<div class="panel-footer">
<button class="btn btn-default" type="submit">Save your Family!</button>
</div>
</div>
</div>
</div>
My issue is when I click the submit button I am calling my service that alerts out all the different values I entered. Everything works except the inputParentCarrier value. When it alerts out it says 'undefined'
Seems like this should be an easy fix, but right now I can't see what is wrong.
You are missing ng-model on your select
<select class="form-control" id="inputParentCarrier" ng-model="addParentService.inputParentCarrier">
<option>ATT</option>
<option>Cricket</option>
<option>Sprint</option>
<option>T-Mobile</option>
<option>Verizon</option>
</select>
You need to add ng-model="addParentService.inputParentCarrier" to your select tag
you need to add model for select element as well, like:
<select ng-model="addParentService.inputParentCarrier" class="form-control" id="inputParentCarrier">
<option>ATT</option>
<option>Cricket</option>
<option>Sprint</option>
<option>T-Mobile</option>
<option>Verizon</option>
</select>
You should bind your ng-model to the select, not the label.
<select class="form-control" id="inputParentCarrier" ng-model="addParentService.inputParentCarrier">
<option>ATT</option>
<option>Cricket</option>
<option>Sprint</option>
<option>T-Mobile</option>
<option>Verizon</option>
</select>
change your select tag to this:
<select class="form-control" id="inputCarrier" ng-model="myService.inputParentCarrier">
<option value="att">ATT</option>
....
<select>

AngularJS Form submit

Working on my first small AngularJS App I'm facing problems with a form submit. I worked trough the CodeSchool course and figured the most out by myself, but this form submit thingy... well I just don't get where I'm wrong so that's why it would be nice if you could show me the right solution, so I can go on.
Project: A simple Workout List where you can list all the training sessions you had. This is my HTML, Element 3 is the problem:
<header class="wob-masthead container-fluid">
<div class="row">
<div class="col-md-6" ng-init="tab = 1">
<ul class="nav nav-pills">
<li ng-class="{ active:tab === 1 }"><a href ng-click="tab = 1">Overview</a></li>
<li ng-class="{ active:tab === 2}"><a href ng-click="tab = 2">Stats</a></li>
<li ng-class="{ active:tab === 3 }"><a href ng-click="tab = 3">New</a></li>
</ul>
</div>
<div class="col-md-6">
<form class="navbar-form pull-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
</div>
</div>
</header>
<section class="wob-main mainlist container" id="headjump">
<!--- ==========================================
Element 1: Overview
============================================= -->
<div class="subsite" ng-show="tab === 1">
<div class="headico"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></div>
<h1>WorkoutBuddy</h1>
<div class="table-responsive" ng-controller="ListController as listing">
<table class="table table-hover">
<thead>
<tr>
<th class="col-md-2">Date</th>
<th class="col-md-8">Type</th>
<th class="col-md-1">Repeat</th>
<th class="col-md-1">Time</th>
</tr>
</thead>
<tbody ng-controller="ListController as listing">
<tr ng-repeat="wo in listing.sessions">
<td>{{wo.date | date:'dd/MM/yyyy'}} </td>
<td>{{wo.name}}</td>
<td>{{wo.repeat}}</td>
<td>{{wo.time}} Minutes</td>
</tr>
</tbody>
</table>
</div>
</div>
<!--- ==========================================
Element 2: Stats
============================================= -->
<div class="subsite" ng-show="tab === 2">
<div class="headico"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></div>
<h1>Stats</h1>
<!-- Ende Subsite -->
</div>
<!--- ==========================================
Element 3: New
============================================= -->
<div class="subsite" ng-show="tab === 3">
<div class="headico"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></div>
<h1>New</h1>
<div class="table-responsive" ng-controller="ListController as listing">
<table class="table table-hover">
<thead>
<tr>
<th class="col-md-2">Date</th>
<th class="col-md-8">Type</th>
<th class="col-md-1">Repeat</th>
<th class="col-md-1">Time</th>
</tr>
</thead>
<tbody ng-controller="ListController as listing">
<tr ng-repeat="wo in listing.sessions | limitTo:2">
<td>{{wo.date | date:'dd/MM/yyyy'}} </td>
<td>{{wo.name}}</td>
<td>{{wo.repeat}}</td>
<td>{{wo.time}} minutes</td>
</tr>
</tbody>
</table>
</div>
<form name="WorkoutForm" ng-controller="EntryController as entryCtrl">
<blockquote>
<h3>Last Workout:</h3>
<strong>{{entryCtrl.wo.name}}</strong><br>
<small>am: {{entryCtrl.wo.date}}</small><br>
{{entryCtrl.wo.repeat}} repeats in {{wo.time}} minutes.
</blockquote>
<input ng-model="entryCtrl.wo.date" type="date" placeholder="date" />
<input ng-model="entryCtrl.wo.name" type="name" placeholder="name" />
<input ng-model="entryCtrl.wo.repeat" type="repeat" placeholder="repeat" />
<input ng-model="entryCtrl.wo.time" type="time" placeholder="time" />
<input type="submit" value="Add" />
</form>
<!-- Ende Subsite -->
</div>
</section>
I styled it with Bootstrap and this is my app.js:
(function(){
var app = angular.module('wobuddy', [ ]);
app.controller('ListController', function(){
this.sessions = wos;
});
var wos = [
{
name: 'Squat',
date: '01.01.2015',
repeat: 50,
time: 10
},
{
name: 'Push Ups',
date: '01.01.2015',
repeat: 50,
time: 10
}
];
})();
Switching between the sections using the nav works pretty fine and also printing out the data-elements in the table, but when I push submit nothing happens - really hope you can help me to learn :-)
You need to make an EntryController that will add a new object to the end of the wos collection. Something like this:
app.controller('EntryController', function($scope) {
$scope.wo = {};
$scope.submit = function() {
wos.push($scope.wo);
$scope.wo = {}; // Clear the form fields
};
});
Then your HTML for that section could look something like this:
<form name="WorkoutForm" ng-controller="EntryController">
<blockquote>
<h3>Last Workout:</h3>
<strong>{{wo.name}}</strong><br>
<small>am: {{wo.date}}</small><br>
{{wo.repeat}} repeats in {{wo.time}} minutes.
</blockquote>
<input ng-model="wo.date" type="date" placeholder="date" />
<input ng-model="wo.name" type="name" placeholder="name" />
<input ng-model="wo.repeat" type="repeat" placeholder="repeat" />
<input ng-model="wo.time" type="time" placeholder="time" />
<button ng-click="submit()">Add</button>
</form>
Notice that it's more usual for a controller to communicate data to the template via the $scope object rather than via the controller object itself.
By looking at you form HTML, I think you missed the name attribute inside your form and also ng-submit directive is missing which will gets called after a submit form. Do check form validation inside controller using $valid() method and perform post else give alert to user.
HTML
<form name="workoutForm" ng-controller="ReviewController as reviewCtrl" ng-submit="submit(workoutForm, entryCtrl.wo)">
<blockquote>
<h3>Last Workout:</h3>
<strong>{{entryCtrl.wo.name}}</strong>
<br>
<small>am: {{entryCtrl.wo.date}}</small>
<br> {{entryCtrl.wo.repeat}} repeats in {{wo.time}} minutes.
</blockquote>
<input name="date" ng-model="entryCtrl.wo.date" type="date" placeholder="date" />
<input name="name" ng-model="entryCtrl.wo.name" type="name" placeholder="name" />
<input name="repeat" ng-model="entryCtrl.wo.repeat" type="repeat" placeholder="repeat" />
<input name="time" ng-model="entryCtrl.wo.time" type="time" placeholder="time" />
<input type="submit" value="Add" />
</form>
Controller
$scope.submit = function(workoutForm, item){
if(workoutForm.$valid)
//then make $http.post by sending item values
else
//show error
};
UPDATE
<html ng-app='demoApp'>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<form ng-controller="validationCtrl">
<input type="text" placeholder="Name...." ng-model="user.name"/>
<input type="text" placeholder="Password...." ng-model="user.pass"/>
<input type="text" placeholder="Mobile...." ng-model="user.mo"/>
<input type="submit" ng-click="alldata(user)"/>
</form>
<script>
//This is controller
var app = angular.module('demoApp', []);
app.controller('validationCtrl', function($scope) {
$scope.alldata=function(user)
{
alert(JSON.stringify(user));
}
});
</script>
</body>
</html>
You can also use this method, and
Your form shoud be like
<form method="post" name="sentMessage" id="my_contact" novalidate="novalidate">
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Name</label>
<input class="form-control" id="name" type="text" name="name" placeholder="Name" required="required" data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Email Address</label>
<input class="form-control" id="email" type="email" name="email" placeholder="Email Address" required="required" data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Phone Number</label>
<input class="form-control" id="phone" type="tel" name="phone" placeholder="Phone Number" required="required" data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls mb-0 pb-2">
<label>Message</label>
<textarea class="form-control" id="message" rows="5" name="Message" placeholder="Message" required="required" data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="form-group">
Send
</div>
</form
import jquery as below
npm install jquery using CLI
import * as $ from 'jquery';
send_query() function will be
send_query() {
var data = $("#my_contact").serializeArray();
var indxarr = {};
$.each(data,function(i,v){
indxarr[v['name']] = v['value'];
});
data = JSON.parse(JSON.stringify(indxarr))
//POST YOUR DATA
this.httpClient.post('http://localhost/rajat/ajax/contact_query_submit/', data,httpOptions)
.subscribe(data => {
console.log(data);
});
}
Your backend code will be
public function contact_query_submit(){
if ($this->input->post()) {
$postdata = file_get_contents("php://input");
$_POST = json_decode($postdata,TRUE);
$addArr = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'message' => $this->input->post('message'),
'created' => time()
);
if($this->main_model->create('rjt_contact', $addArr)){
$arr[] = array('type' => 'success', 'msg' => '<p>Your query has been received. <br>We will get back to you soon.</p>');
echo json_encode($arr);
}else{
$arr[] = array('type' => 'warning', 'msg' => '<p>'.$this->db->last_query().'</p>');
echo json_encode($arr);
}
}else{
$arr[] = array('type' => 'warning', 'msg' => '<p>No data post yet</p>');
echo json_encode($arr);
}
}

Angular ng-model not binding to select

I'm pulling a list of accounts from my data base, and using that info to make a select drop down with the name showing, and id as the value. That all works fine, however, once I try to send it to my controller to be posted to my API, there's no to or from accounts. I have the ng-model on both fields, but it doesn't seem to do anything.
Here's my HTML
<form novalidate>
<div ng-controller="newTransactionController">
<div >
<label for="from">From Account</label>
<select name="from" ng-options="acct as acct.label for acct in options" ng-model="transaction.fromAccount" >
<option value="acct.value"> </option>
</select>
</div>
<div>
<label for="to">To Account</label>
<select name="to">
<option ng-repeat="account in accounts" value="{%account._id%}" ng-model="transaction.toAccount">{%account.name%}</option>
</select>
</div>
</div>
<div>
<label for="amount">Amount</label>
<input type="text" name="amount" id="amount" value="" tabindex="1" ng-model="transaction.amount"/>
</div>
<div>
<label for="currency">Currency</label>
<select name="currency" ng-model="transaction.currency">
<option value="USD">USD</option>
<option value="BTC">BTC</option>
</select>
</div>
<div>
<label for="interval">Interval</label>
<input type="text" name="interval" id="interval" value="" tabindex="1" ng-model="transaction.interval"/>
</div>
<div>
<label for="processDate">Process Date</label>
<input type="text" name="processDate" id="processDate" value="" tabindex="1" ng-model="transaction.processDate"/>
</div>
<div>
<input class="button" type="submit" value="Send" ng-click="createTransaction(transaction)"/>
</div>
</form>
And my controller
$http.get('').success(function(data){
$scope.accounts = data;
var options = $scope.options = [];
for(var i=0; i < data.length; i++){
$scope.options.push({label: data[i].name, value: data[i]._id});
}
});
$scope.createTransaction = function(transaction){
var transactions = {
fromAccount: angular.copy(transaction.fromAccount),
toAccount: angular.copy(transaction.toAccount),
amount: angular.copy(transaction.amount),
currency: angular.copy(transaction.currency),
interval: angular.copy(transaction.interval),
processDate: angular.copy(transaction.processDate)
};
$http.post('', transactions).success(function(){console.log('sent')});
};
When I log out transactions in the controller, all I get is: Object {amount: "1", currency: "USD"}
I'm lost. I can't figure out what's missing here.
Found my problem. It was a scope issue with the <div> tag surrounding both of the to and from accounts.

Resources