I have a view as below:
<select ng-model="d" ng-options="k.id as k.name for k in act"></select>
<input type="submit" value="Create" ng-click="CreateOpen(Emp)" class="btn btn-default" />
</div>
My Js is as below:
app.factory('crudServiceUser', function ($http) {
crudUserObj = {};
crudUserObj.Create = function (openact) {
var Emp;
Emp = $http({
method: 'Post',
url: '/Employee/BankAccount/Index',
data: openact
}
).then(function (response) {
return response.data;
});
return Emp;
}
return crudUserObj;
});
app.controller('usersController', function ($scope, crudServiceUser) {
$scope.CreateOpen = function (openact) {
crudServiceUser.Create(openact).then(function (result) {
$scope.Msg = alert(result.EName + " Has Been Created Successfully");
})
}
Here is my action method:
public ActionResult Index(tblOpenAct tblopact)
{
if (ModelState.IsValid)
{
objemp.OpenActBs.Insert(tblopact);
return RedirectToAction("Index");
}
return View(tblopact);
}
But the select option is inserting null to database but if I use textbox then it inserts data.
Try like bellow
<select ng-model="d" >
<option ng-repeat="k in act" value="{{k.id}}">{{k.name }} </option>
</select>
Related
I have a controller that returns an array from my django view, i am trying to display each object in a different line in my html but it just returns json.
my controller.js
mainApp.controller('hsController', function($scope, $http, $compile, $timeout, $location) {
$scope.data = {};
$scope.init = function() {
console.log("angular loaded!")
}
$scope.data.form = {
hs_search: "",
result: {},
};
$scope.data.formStyle = {
hs_search: ""
};
$scope.submitForm = function() {
//console.log($scope.data.form)
var error = 0;
if(!$scope.data.form.hs_search) {
$scope.data.formStyle.hs_search = "is_invalid";
error++;
} else {
$scope.data.formStyle.hs_search = "";
}
if(error==0) {
var jsonCall = $http({
method: 'POST',
url:'/theapp/hs-code-search',
data: $scope.data.form,
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
});
jsonCall.success(function(data, status, headers, config) {
if(data.status==1) {
$scope.data.form.result = data.data
console.log($scope.data.form.result)
}
});
jsonCall.error(function(data, status, headers, config) {
if(data.status==0) {
$.growl.error({ message: data.error});
}
});
console.log($scope.data.form)
} else {
$.growl.error({ message: "Please fill the form correctly!"});
}
}
});
my html
<div class="row">
<div class="col-lg-11 col-md-11 mx-auto">
<h3>Search Result For </h3>
<div ng-repeat="chapter_code in data.form.result">
{[{chapter_code}]}
</div>
<div ng-repeat="chapter_desc in data.form.result">
{[{chapter_desc}]}
</div>
</div>
</div>
Result that am getting
What i wanted instead is just the value of each object shown.
Try to convert the JSON to an Object using JSON.parse() and then you can use ng-repeat
myController.js
if(data.status==1) {
$scope.data.form.result = JSON.parse(data.data);
console.log($scope.data.form.result);
// Console shall return an Object
}
HTML
<div class="row">
<div class="col-lg-11 col-md-11 mx-auto">
<h3>Search Result For </h3>
<div ng-repeat="chapterCode in data.form.result.chapter_code">
{{chapterCode}}
</div>
<div ng-repeat="chapterDesc in data.form.result.chapter_desc">
{{chapterDesc}}
</div>
</div>
</div>
My html:
<div ng-app="APSApp" class="container">
<br />
<br />
<input type="text" placeholder="Search Terms" />
<br />
<div ng-controller="APSCtl" >
<table class="table">
<tr ng-repeat="r in searchTerms" ng-init="searchTerms=getSearchTerms()" >
<td>{{r.DisplayText}} <input type="text" ng-model="r.SearchInput"></td>
</tr>
</table>
</div>
</div>
<script type="text/javascript">
const moduleId = '#Dnn.ModuleContext.ModuleId';
const tabId = '#Dnn.ModuleContext.TabId';
</script>
<script src="/DesktopModules/RazorCart/Core/Content/Scripts/angular.min.js"></script>
<script src="/DesktopModules/MVC/AdvancedProductSearchMVC/Scripts/AdvancedProductSearch.js"></script>
My angular setup:
var aps = angular.module("APSApp", []);
aps.config(function($httpProvider) {
$httpProvider.defaults.transformRequest = function(data) {
return data !== undefined ? $.param(data) : null;
};
});
aps.factory('SearchTerms',
function($http) {
return {
getSearchTerms: function(onSuccess, onFailure) {
const rvtoken = $("input[name='__RequestVerificationToken']").val();
$http({
method: "post",
url: "/DesktopModules/MVC/AdvancedProductSearchMVC/AdvancedProductSearch/GetAPS",
headers: {
"ModuleId": moduleId,
"TabId": tabId,
"RequestVerificationToken": rvtoken
}
}).success(onSuccess).error(onFailure);
}
};
});
aps.controller('APSCtl',
function(SearchTerms, $scope) {
function getSearchTerms() {
$scope.searchTerms = [];
successFunction = function(data) {
$scope.searchTerms = data;
console.log($scope.searchTerms);
};
failureFunction = function(data) {
console.log('Error' + data);
};
SearchTerms.getSearchTerms(successFunction, failureFunction);
}
function doSomethingElse($scope) {}
});
I'm trying to create a single controller with multiple functions. This works if my angular controller looks like this (and I don't use ng-init):
aps.controller('APSCtl',
function(SearchTerms, $scope) {
$scope.searchTerms = [];
successFunction = function(data) {
$scope.searchTerms = data;
console.log($scope.searchTerms);
};
failureFunction = function(data) {
console.log('Error' + data);
};
SearchTerms.getSearchTerms(successFunction, failureFunction);
});
I was just trying to keep related functions in a single controller. What am I doing wrong? Do I actually have to set up a different controller for each function?
You do not have to assign the value in the template, you can just call the function,
<table class="table" ng-init="getSearchTerms()>
<tr ng-repeat="r in searchTerms" >
<td>{{r.DisplayText}} <input type="text" ng-model="r.SearchInput"></td>
</tr>
</table>
you should have a function named getSearchTerms() in your controller to get it called,
aps.controller('APSCtl',
function(SearchTerms, $scope) {
$scope.getSearchTerms() {
$scope.searchTerms = [];
successFunction = function(data) {
$scope.searchTerms = data;
console.log($scope.searchTerms);
};
failureFunction = function(data) {
console.log('Error' + data);
};
SearchTerms.getSearchTerms(successFunction, failureFunction);
}
function doSomethingElse($scope) {}
});
I have the following requirement: I have one drop-down list containing Employee records and which is getting filled on page load (JSON format). In that data list I have one value as Approval Level which is an integer value.
Now I want that when I select employee ,as per his Approval Level value I want that no of drop down list get filled with employee data.
Means when I select employee whose Approval Level is 5, I want 5 drop down list below that get filled with Employee records
HTML:-
<div class="col-md-6">
<select class="form-control crl dropdown" placeholder="Approval Level" name="ApprovalLevel" id="ApprovalLevelDDL" required ng-model="insert.Approval_Level" ng-change="makeArray()">
<option value="">Employee Name</option>
<option value="{{list.Approval_Level}}" ng-repeat="list in EmpList">{{list.Emp_Name}}</option>
</select>
<span style="color:red" class="error" ng-show="myForm.DesignationName.$error.required || myForm.DesignationName.touched.required">
Require
</span>
</div>
</div>
<div class="row col-md-10 col-md-offset-1 mypadd" ng-repeat="o in arr">
<div class="col-md-6">
<label for="DepartmentHead">Approval Head</label>
</div>
<div class="col-md-6">
<select class="form-control crl dropdown" placeholder="Approval Level" name="ApprovalLevel_{{$index}}" id="ApprovalLevelDDL_{{$index}}" required ng-model="selectedItem" ng-change="HeadEmployeeList({{$index}})" ng-options="list.Emp_ID as list.Emp_Name for list in ddlArr">
</select>
</div>
</div>
Angular Controller:-
function EmployeeList() {
$http({
method: "POST",
url: "/Employee/GetEmployeeList/",
datatype: "json",
headers: { "ContentType": "application/json" },
}).then(function (data) {
$scope.EmpList = data.data.EmpList;
$rootScope.DataList = data.data.EmpList;
$scope.edatalist = data.data.EmpList;
console.log($rootScope.DataList);
}).catch(function (data) {
$scope.error = data.data;
console.log($scope.error);
});
};
EmployeeList();
$scope.EmployeeDataList = function () {
$http({
method: "POST",
url: "/Employee/GetEmployeeList/",
datatype: "json",
headers: { "ContentType": "application/json" },
}).then(function (data) {
//$rootScope.DataList = data.data.EmpList;
$scope.edatalist = data.data.EmpList;
console.log($scope.edatalist);
}).catch(function (data) {
$scope.error = data.data;
console.log($scope.error);
});
};
$scope.insert = $rootScope.DataList;
$scope.arr = [];
$scope.newardata = [];
$scope.ddlArr = [];
$scope.makeArray = function () {
console.log($scope.insert);
$scope.arr.length = 0;
$scope.EmpList.Emp_ID = parseInt($scope.EmpList.Emp_ID, 10);
$scope.EmployeeDataList();
for (var i = 0; i <= parseInt($scope.insert.Approval_Level) ; i++) {
$scope.arr.push(i);
$scope.EmpList[i].Emp_ID = parseInt($scope.EmpList[i].Emp_ID, 10);
$scope.ddlArr.push($scope.EmpList[i]);
}
$scope.newardata = $scope.arr;
console.log($scope.arr);
console.log($scope.ddlArr);
}
console.log($scope.newardata);
var Emplist = [];
var EDATA = [];
$scope.HeadEmployeeList = function (id) {
console.log($scope.newardata);
var eid = {};
console.log(Emplist);
var data = parseInt(this.EmpList.Emp_ID);
console.log(data);
var index = Emplist.indexOf(parseInt(this.insert.Emp_ID));
var subindex = EDATA.indexOf(parseInt($scope.EmpList.Emp_ID,10));
console.log(index);
console.log(subindex);
if (index < 0){
Emplist.push(this.insert.Emp_ID);
EDATA.push($scope.EmpList.Emp_ID,10);
}
console.log(Emplist.indexOf(this.insert.Emp_ID));
console.log(Emplist);
console.log(EDATA.indexOf(parseInt($scope.EmpList.Emp_ID,10)));
console.log(EDATA);
$scope.EDATA = EDATA;
};
I have this app that users can select a list of faults from drop downs drilling down three levels, the issue I am having is when they are opening the item to edit, the first drop down populates on page load and has the selected item from the saved data, the second populates the options but does not select the any items, and the third the same.
controller:
.controller("Ctrl", function ($scope, $http) {
var urlString = systems.systems + "/getIssueCodes/" + Id.Id;
$http({
method: 'GET',
url: urlString,
dataType: "json"
}).then(function successCallback(response) {
var data = response;
if (data.data.name1 !== "") {
$scope.Fault = data.data.name1;
f1 = data.data.id1;
$scope.faultChangedLoad(f1, (data.data.id2 === null ? "" : data.data.id2));
$scope.Fault1 = data.data.name2;
}
if (data.data.name2 !== "") {
f2 = data.data.id2;
$scope.fault1ChangedLoad(f2, (data.data.name3 === null ? "" : data.data.name3));
}
if (data.data.name3 !== null) {
f3 = data.data.id3;
}
$scope.Desc = data.data.desc;
});
$scope.faultChangedLoad = function (Fault, selected) {
var urlStr = systems.systems + "/faultCodes/" + Fault;
$http({
method: 'GET',
url: urlStr,
dataType: "json"
}).then(function successCallback(response) {
$scope.fault1Codes = [];
if (response.data.length > 0) {
var ms4 = angular.element(document.querySelector('#Main-select4'));
ms4.removeClass("no-show");
ms4.addClass("item item-select");
var ms6 = angular.element(document.querySelector('#Main-select6'));
if (ms6.hasClass("item")) {
ms6.removeClass("item item-select");
ms6.addClass("no-show");
}
}
else {
var ms41 = angular.element(document.querySelector('#Main-select4'));
if (ms41.hasClass("item")) {
ms41.removeClass("item item-select");
ms41.addClass("no-show");
}
var ms61 = angular.element(document.querySelector('#Main-select6'));
if (ms61.hasClass("item")) {
ms61.removeClass("item item-select");
ms61.addClass("no-show");
}
}
$scope.fault1Codes = response.data;
$scope.Fault1 = selected;
});
}
$scope.fault1ChangedLoad = function (Fault, selected) {
$http({
method: 'GET',
url: systems.systems + "/faultCodes/" + Fault,
dataType: "json"
}).then(function successCallback(response) {
$scope.fault2Codes = [];
if (response.data.length > 0) {
var ms6 = angular.element(document.querySelector('#Main-select6'));
ms6.removeClass("no-show");
ms6.addClass("item item-select");
}
else {
var ms61 = angular.element(document.querySelector('#Main-select6'));
if (ms61.hasClass("item")) {
ms61.removeClass("item item-select");
ms61.addClass("no-show");
}
}
$scope.fault2Codes = response.data;
$scope.Fault2 = selected;
});
}
}
View:
<label class="item item-select" id="Main-select5">
<span class="input-label">Issue Code:</span>
<select ng-model="Fault" ng-change="faultChanged(Fault)">
<option ng-repeat="faultCode in faultCodes" ng-selected="faultCode.text === Fault" value="{{faultCode.value}}">{{faultCode.text}}</option>
</select>
</label>
<label class="no-show" id="Main-select4">
<span class="input-label"></span>
<select ng-model="Fault1" ng-change="fault1Changed(Fault1)">
<option ng-repeat="fault1Code in fault1Codes" ng-selected="fault1Code.text === Fault1" value="{{fault1Code.value}}">{{fault1Code.text}}</option>
</select>
</label>
<label class="no-show" id="Main-select6">
<span class="input-label"></span>
<select ng-model="Fault2">
<option ng-repeat="fault2Code in fault2Codes" ng-selected="fault2Code.text === Fault2" value="{{fault2Code.value}}">{{fault2Code.text}}</option>
</select>
</label>
<label class="item item-input" id="Main-textarea1">
<span class="input-label">Description:</span><input type="text" placeholder="Enter Description" ng-model="Desc">
</label>
I have looked everywhere for a solution, I have tried ng-options but that did not help.
Any help is appreciated,
Thank you
I found a working solution and I want to put it here in case someone else has the same issue.
I could not find any help so I went back to the books, after reading several chapters, I saw (not mentioned in requirements) a bit of code that displayed ng-value instead of value and I tried it, it worked and the data is being displayed.
Thank you
I am trying to save data in sql using fat free framework. i used front end in angularjs. i send data using angular ng-submit button. ajax Post data but not get in fat free please solve this problem. i am new in fat free.
here is my html code:
<form id="userRegister" name="registration" ng-submit="register1(formData)" ng-controller="Ctrl1">
<div class="sf-steps-form sf-radius">
<div class="sf_columns column_3">
<input ng-model="formData.email" id="email" type="email" name="email" placeholder="Email*" data-required="true" >
</div>
<div class="sf_columns column_3">
<input ng-model="formData.password" id="password" type="password" name="password" placeholder="Secret Word*" data-required="true" >
</div>
</div>
<button type="submit" id="sf-next" class="sf-button">Save</button>
</form>
here is my app.js code:
sampleApp.controller("Ctrl1", function($scope, $http) {
$scope.formData = {};
$scope.register1 = function() {
console.log($scope.formData);
$http({
method : 'POST',
url : 'addstep',
data : $scope.formData,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
$scope.errorEmail = data.errors.email;
$scope.errorPassword = data.errors.password;
} else {
$scope.message = data.message;
}
});
};
});
here is my idex.php fat free framework code:
$f3->route('GET|POST /addstep',
function($f3) {
//print_r($f3);
$users = new DB\SQL\Mapper($f3->get('DB'),'user');
$users->copyFrom('POST');
$users->save();
$f3->set('content','step1.htm');
echo View::instance()->render('layout.htm');
}
);
The ajax post data properly but not save in db please help.
Check $f3->get('BODY');
You might need to json_decode;
Most likely the data is sent via PUT
I actually just dealt with this on an application using f3 and angular. If you haven't figured it out I have been pretty successful with this:
I have an angular $http service:
angular.module('myApp')
.service('apiConnector', function apiConnector($http) {
var apiBase = '';
var obj = {};
obj.get = function(q) {
return $http.get(apiBase + q).then(function(results) {
return results.data;
});
};
obj.post = function(q, object) {
return $http.post(apiBase + q, object).then(function(results) {
return results.data;
});
};
obj.put = function(q, object) {
return $http.put(apiBase + q, object).then(function(results) {
return results.data;
});
};
obj.delete = function(q) {
return $http.delete(apiBase + q).then(function(results) {
return results.data;
});
};
return obj;
});
Then I use this service in my angular controllers like so:
angular.module('myApp')
.controller('homeController',function($scope, $state, $stateParams, $timeout, apiConnector){
$scope.user = {};
apiConnector.get('/api/users/'+$stateParams.id)
.then(function(res){
if (res.success) {
$scope.user = res.data;
}
},function(err){
console.log(err);
});
$scope.updateUser = function(user) {
apiConnector.post('/api/users/'+$stateParams.id,user)
.then(function(res){
if (res.success) {
alert('updated');
}
}, function(err){
console.log(err);
});
};
});
Lastly the f3 controller. I am using [maps] for my routes to get a truly restful interface, and my routes use an #id param. I collect data like so:
class Item {
function get($app,$params) {
$id = $params['id'];
$user = new \Models\User();
$user->load(array('id = ?',$id));
echo json_encode($user->cast());
}
function post($app,$params) {
$POST = json_decode(file_get_contents('php://input'));
$id = $params['id'];
$user = new \Models\User();
$user->load(array('id = ?',$id));
$user->copyfrom($POST);
$user->touch('created');
$user->save();
echo json_encode(array('message' => 'Successfully updated user!'));
}
function put() {}
function delete() {}
}
Hope that helps!