I am currently learning to code with Angular.js and I am doing a project where I load informations with Ajax queries.
To make it short, I have three levels of datas
-- Skills (competences)
--- Indicators (indicateurs)
---- Results (resultats)
I have a first request that tracks Skills and Indicator for the current skill.
HTML
<div ng-controller="AdminController" data-ng-init="listAllCompetences()">
<!-- Level 1 -->
<div data-ng-repeat="competence in competences" class="content level1 topcompetence" id="competence_{{competence.Competence.id}}">
<div class="level1_title_box">
<h3 class="h3 titre_competence" ng-hide="editorEnabled">{{$index + 1}} - {{competence.Competence.description}} </h3>
</div>
<p><b>Indicateurs de performances : </b></p>
<!-- Level 2 -->
<div data-ng-repeat="indicateur in competence.Indicateur" class="content level2" id="indicateur_{{indicateur.id}}">
<div class="level2_title_box">
<h4>{{$index + 1}} - {{indicateur.description}}</h4>
</div>
<p><b>Results : </b></p>
<p><a ng-click="listAllRestulatsByIndicateurId(indicateur.id)" href="javascript:void(0);">Click here to show results</a></p>
<!-- Level 3 -->
Level 3 shoudl be displayed there...
<!-- End Level 3 -->
<pre>{{resultatsAttendusCallback}} {{resultatsAttendusCallback.length}}</pre>
</div>
<!-- End Level 2 -->
</div>
<!-- End Level 1-->
</div>
When I click on listAllRestulatsByIndicateurId(indicateur.id); function, there is an Ajax request that get all the results for the given indicator.
The point where I have not figured it out yet is how am I supposed to know where to output this since I can have alot of Indicators.
My Angular function
$scope.listAllRestulatsByIndicateurId = function(indicateurID) {
console.log(indicateurID);
var req_resultats_by_indicateur = {
method: 'POST',
url: '../api/resultatAttendus/listByIndicateur',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
indicateur_id: indicateurID
}
}
console.log(req_resultats_by_indicateur);
$http(req_resultats_by_indicateur).then(function successCallback(callback) {
if(callback.data.status == 'success') {
$scope.resultatsAttendusCallback = callback.data.data;
console.log(callback);
}
if(callback.data.status == 'error') {
console.log(callback)
}
});
}
Note : The use of callback, may be bad as I named it. It is the returned array from my ajax call.
This line $scope.resultatsAttendusCallback = callback.data.data; give me the array of results with the indicator ID as parent.
But when I execute this function, all my {{resultatsAttendusCallback}} or Results spaces are writing the array. I just want to get the result to be printed in the indicator ID I clicked.
Is there any way to attribute any sort of ID or Class name to those element so I could know on wich element to work and get the angular callback working?
You can pass the current indicateur to the function (The object itself, not its ID) and attach the data directly to that object, then in the view, you'll need to display the returned data that was attached to the object by the ajax request:
<!-- Level 1 -->
<div data-ng-repeat="competence in competences" class="content level1 topcompetence" id="competence_{{competence.Competence.id}}">
<div class="level1_title_box">
<h3 class="h3 titre_competence" ng-hide="editorEnabled">{{$index + 1}} - {{competence.Competence.description}} </h3>
</div>
<p><b>Indicateurs de performances : </b></p>
<!-- Level 2 -->
<div data-ng-repeat="indicateur in competence.Indicateur" class="content level2" id="indicateur_{{indicateur.id}}">
<div class="level2_title_box">
<h4>{{$index + 1}} - {{indicateur.description}}</h4>
</div>
<p><b>Results : </b></p>
<p><a ng-click="listAllRestulatsByIndicateurId(indicateur)" href="javascript:void(0);">Click here to show results</a></p>
<!-- Level 3 -->
Level 3 shoudl be displayed there...
<!-- End Level 3 -->
<pre ng-if="indicateur.resultatsAttendusCallback">{{indicateur.resultatsAttendusCallback}} {{indicateur.resultatsAttendusCallback.length}}</pre>
</div>
<!-- End Level 2 -->
</div>
<!-- End Level 1-->
JS:
$scope.listAllRestulatsByIndicateurId = function(indicateur) {
console.log(indicateur.id);
var req_resultats_by_indicateur = {
method: 'POST',
url: '../api/resultatAttendus/listByIndicateur',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
indicateur_id: indicateur.id
}
}
console.log(req_resultats_by_indicateur);
$http(req_resultats_by_indicateur).then(function successCallback(callback) {
if(callback.data.status == 'success') {
indicateur.resultatsAttendusCallback = callback.data.data;
console.log(callback);
}
if(callback.data.status == 'error') {
console.log(callback)
}
});
}
I'm not following your question 100% but I think I know what you are tyring to do. Your callback.data.data is coming back as a JSON string, but you want just a subset of it.
Here is what I do
$scope.resultatsAttendusCallback = MyModel.build(callback.data.data);
...........
angular.module('lodgicalWebApp')
.factory('MyModel',['Column',function(Column){
function MyModel(title, columns){
this.title = title;
this.columns = Column.build(columns); //Column is another Model
}
MyModel.build = function(data){
// //console.log("building variables: " + data);
var models= [];
angular.forEach(data, function(v,k){
models.push(new MyModel(v.name, v.cols));
})
return models;
}
return MyModel;
})
This allows me to return complex JSON and map it to a real object in my code. You don't have to do the forEach in the build either, I do this when my JSON returns a list of MyModels rather than a single one. From there you can add your own ID, attributes, do any data transforms etc.
Related
So this works with static data, but when I push data with a $http this autocomplete does not work. The data pushes to the empty array of airport_list but something is happening when I try to use airport_list in for the autocomplete. Not sure what is is. I can only find answers which pertain to static data.
This is updated per everyones help.
Here is the controller
app.controller('selectCtrl', function($scope, $http) {
$scope.airport_list = null;
$http({
url: 'someUrl.com',
method: 'GET'
})
.then((response) => {
angular.forEach(response.data.airports, function(value, key) {
$scope.airport_list = response.data.airports;
})
$scope.airports = $scope.airport_list;
});
$scope.selectAirport = function(string) {
$scope.airport = string;
$scope.hidelist = true;
};
})
Here is the template
<div class="control">
<div>
<input
type="text"
name="airport"
id="airport"
ng-model="airport"
ng-change="searchFor(airport)"
placeholder="From..."
/>
<div class="airport-container-dropdown" ng-hide="hidelist">
<div
class="airport-list"
ng-repeat="airport in airports"
ng-click="selectAirport(airport)"
>
{{ airport.name }}
</div>
</div>
</div>
</div>
I really would like to do this without using bootstrap typeahead.
Thank you for looking at this.
I have made changes as recommended by below answers and the $http request is feeding into the autocomplete as a whole list but searching by name does not work and clicking on name sets [object, object]
this would be the code which is specific to that functionality.
$scope.searchFor = function(string) {
$scope.hidelist = false;
const output = [];
angular.forEach($scope.airport_list, function(airport) {
if (airport[0].toLowerCase().indexOf(string.toLowerCase(airport)) >=
0) {
output.push(airport);
}
});
$scope.airports = output;
};
$scope.selectAirport = function(string) {
$scope.airport = string;
$scope.hidelist = true;
};
Try this:
$scope.airport_list = response.data.airports;
What I am seeing is that you have an array: $scope.airport_list = [];
When you make your http request, you push what I would understand to be an array of airports into that array. So you end up with your airport array from the backend at the first position of $scope.airport_list, vs. $scope.airport_list being the actual list.
For your search method, you should change the following:
In your HTML:
ng-change="searchFor(airport.name)"
In your JS:
I've renamed your function and changed the input variable to be more clear. You were passing in a full airport, but treating it as a string. You need to compare your provided airport name to that of the airports in the array. So you iterate over the array, and compare each element's name property to what you pass in.
$scope.searchFor = function(airportName) {
$scope.hidelist = false;
const output = [];
angular.forEach($scope.airport_list, function(airport) {
if (airport.name.toLowerCase() === airportName) {
output.push(airport);
}
});
$scope.airports = output;
console.log($scope.airports);
};
I have provided minimal changes to your code to implement this, however I suggest you look at this SO post to filter drop down data more appropriately.
Angularjs Filter data with dropdown
If you want to simply filter out what is displayed in the UI, you can try this in your HTML template. It will provide a text field where you supply a partial of the airport name. If at least one character is entered in that box, the list will display on the page, with the appropriate filtering applied. This will avoid having to call functions on change, having a separate array, etc.
<input type="text" name="airport" id="airport" ng-model="airportSearch.name" placeholder="From..." />
<div class="airport-container-dropdown" ng-hide="!airportSearch.name">
<div class="airport-list"
ng-repeat="airport in airport_list | filter:airportSearch"
ng-click="selectAirport(airport)">
{{ airport.name }}
</div>
</div>
In an Index-gsp, I want to be able to select an arbitrary number of lines and then by clicking a link send all those lines to a controller for processing e.g. creating new objects of a different kind.
I've no idea how selection can be done or how to collect these selected lines in a GSP. Maybe I should use a checkbox on each line if that's possible?
It's a list of products which is displayed using a modified index.gsp.
Each product-line has a checkbox in front.
What I want is to make a list of the products that are checked an then transmit this list to a controller.
a part of this index.gsp:
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
<li><g:link class="create" action="createOffer"><g:message code="default.new.label" args="[entityName]" params="toOffer" /></g:link></li>
</ul>
</div>
<div id="list-prodBuffer" class="content scaffold-list" role="main">
<h1><g:message code="default.list.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<table>
<thead>
<tr>
<td> Välj</td>
<td> ID</td>
</tr>
</thead>
<tbody>
<g:each in="${prodBufferList}" status="i" var="prodBuffer">
<tr class="${ (i % 2) == 0 ? 'even': 'odd'}">
<td><g:checkBox name="toOffer" value="${prodBuffer.id}" checked="false" /></td>
<td>${prodBuffer.id}</td>
So this not an ordinary form, just a list where I want to use a link to transmit it to the controller.
I'm a beginner and have no idea how to do it.
You can collect all necessary data from page using javascript, and then send all data to your controller for processing.
There are a lot of ways to do it.
For example send via JQuery:
<script>
//some code
var items = [1,2,3];
//some code
$('#add-location').click(function () {
$.ajax({
type: "POST",
url: "${g.createLink(controller:'myController', action: 'myControllerMethod')}",
data: {items: items},
success: function (data) {
console.log(data)
}
});
});
</script>
I will answer this but have to slow down since it feels like i am beginning to write your project:
In gsp you will need to have a hidden field followed by a check box amongst data you are trying to capture, checkbox should contain all the data elements required to build your output.
<g:hiddenField name="userSelection" value=""/>
<g:checkBox name="myCheckBox" id='myCheckBox' value="${instance.id}"
data-field1="${instance.field1}" data-field1="${instance.field1}"
checked="${instance.userSelected?.contains(instance.id)?true:false}" />
In the java script segment of the page you will need to add the following
This will then auto select selection and add to javascript array
// Customized collection of elements used by both selection and search form
$.fn.serializeObject = function() {
if ($("[name='myCheckBox']:checked").size()>0) {
var data=[]
$("[name='myCheckBox']:checked").each(function() {
var field1=$(this).data('field1');
var field2=$(this).data('field2');
data.push({id: this.value, field1:field1, field2:field2 });
});
return data
}
};
Most importantly will your data sit across many different gsp listing pages if so you will need to hack pagination:
//Modify pagination now to capture
$(".pagination a").click(function() {
var currentUrl=$(this).attr('href');
var parsedUrl=$(this).attr('href', currentUrl.replace(/\&userSelection=.*&/, '&').replace(/\&userSelection=\&/, '&'));
var newUrl=parsedUrl.attr('href') + '&userSelection=' + encodeURIComponent($('#userSelection').val());
window.location.href=newUrl
return false;
});
Then in the controller parse the JSON form field and make it into what you want when posted
def u=[]
def m=[:]
if (params.userSelection) {
def item=JSON.parse(params.userSelection)
item?.each {JSONObject i->
// When field1 is null in JSON set it as null properly
if (JSONObject.NULL.equals(i.field1)) {
i.field1=null
}
if (resultsGroup) {
if (!resultsGroup.contains(i.id as Long)) {
u << i
}
} else {
u << i
}
}
m.userSelected=item?.collect{it.id as Long}
m.results=u
}
return m
I have two objects, programme and section, each programme has some sections
I have two services, the first service shows all programmes, and the second service shows all sections for one programme, the problem is : I can't show each sections for one programme using the code of programme as a parameter, this is my code :
var app=angular.module("myApp",[]);
app.controller("myController",function($scope,$http){
$scope.programme=null;
$scope.section=null;
$http.get("/programmes").success(function(response) {
$scope.programme = response;
});
//getting sections for one programme using code
$scope.getSectionByCode = function(code)
{
$http.get("/listsections/"+code).success(function(data){
$scope.section = data;
});
};
});
HTML
<div class="padd" ng-app="myApp"
ng-controller="myController">
<ul>
<li ng-repeat="x in programme"
ng-init="getSectionByProgCode(x.code)" >
{{x.titre}}
<ul>
<li ng-repeat="s in section">{{s.title }}</li>
</ul>
</li>
</ul>
</div>
the result of this code is :
programme 1
Section title 4
Section title 5
programme 2
Section title 4
Section title 5
but im looking for this result :
programme 1
Section title 1
Section title 2
Section title 3
programme 2
Section title 4
Section title 5
Note : when I check my browser log, I find that the service of section get the results properly for each programme.
First of all I would advise you to read about how to structure your app properly, e.g. place http calls in services, store data in services, etc.
Now - your problem is that you do 2 asynchronous calls and place both results on the same variable: $scope.section, so obviously both ng-repeat divs will show the same data.
One way to fix that is you could make the $scope.section variable an array and store by index:
ng-init="getSectionByProgCode(x.code, $index)"
And in your controller:
$scope.section = [];
$scope.getSectionByCode = function(code, index)
{
$http.get("/listsections/"+code).success(function(data){
$scope.section[index] = data;
});
};
HTML function name and controller function name don't match, I'm guessing it's a typo of yours.
A different way to solve it (preferable by me and fellow commentators), is to store your section data under the programme itself:
<li ng-repeat="x in programme"
ng-init="getSectionByProgCode(x)" >
{{x.titre}}
<ul>
<li ng-repeat="s in x.section">{{s.title}}</li>
</ul>
</li>
And in your controller:
$scope.getSectionByProgCode = function(programme)
{
$http.get("/listsections/" + programme.code).success(function(data){
programme.section = data;
});
};
I am building a relatively simply blog page that uses the WP Rest Api and AngularJs to show the data on the front-end.
On my home page I want to return the title, followed by the featured image, followed by the excerpt. I have pulled the title and excerpt in fine it seems that in the JSON the featured image is a media Id. What is the best way to pull this data in on the fly?
I have seen various things around the internet that use PHP functions but I think the best way to do it is within a angular controller, just looking for some advice on exactly what the controller would be
List View HTML
<ng-include src=" dir + '/form.html?v=2' "></ng-include>
<div class="row">
<div class="col-sm-8 col-lg-10 col-lg-push-1 post">
<div class="row-fluid">
<div class="col-sm-12">
<article ng-repeat="post in posts" class="projects">
<a class="title" href="#/post/{{post.slug}}"><h2>{{post.title.rendered}}</h2></a>
<p ng-bind-html="post.excerpt.rendered | to_trusted"></p>
</article>
</div>
</div>
</div>
</div>
Controller
.controller('listPage',['$scope','Posts', function($scope,Posts){
$scope.refreshPosts = function(){
Posts.query(function(res){
$scope.posts = res;
});
};
$scope.refreshPosts();
// CLEARFORMFUNCTION
$scope.clear = function(){
$scope.$root.openPost = false;
jQuery('#save').modal('hide');
};
// SAVEMODALOPEN/COSE
$scope.openSaveModal = function(){
jQuery('#save').modal('show');
}
$scope.closeSaveModal = function(){
jQuery('#save').modal('hide');
}
// DATEFUNCTION
$scope.datify = function(date){
$scope.date = newDate(date);
return $scope.date.getDate()+'/'+$scope.date.getMonth()+'/'+$scope.date.getYear();
};
}])
You could also modify the JSON response with PHP. This returns just what I need and is very fast (Using _embed is very slow in my experience)
I have the following code in a plugin (used for adding custom post types), but I imagine you could put it in your theme's function.php file.
php
add_action( 'rest_api_init', 'add_thumbnail_to_JSON' );
function add_thumbnail_to_JSON() {
//Add featured image
register_rest_field( 'post',
'featured_image_src', //NAME OF THE NEW FIELD TO BE ADDED - you can call this anything
array(
'get_callback' => 'get_image_src',
'update_callback' => null,
'schema' => null,
)
);
}
function get_image_src( $object, $field_name, $request ) {
$size = 'thumbnail'; // Change this to the size you want | 'medium' / 'large'
$feat_img_array = wp_get_attachment_image_src($object['featured_media'], $size, true);
return $feat_img_array[0];
}
Now in your JSON response you should see a new field called "featured_image_src": containing a url to the thumbnail.
Read more about modifying responses here:
http://v2.wp-api.org/extending/modifying/
And here's more information on the wp_get_attachment_image_src() function
https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
**Note: Don't forget <?php ?> tags if this is a new php file!
Turns out, in my case, there is a new plugin available that solves this without having to make a secondary request. See my recent Q:
WP Rest API + AngularJS : How to grab Featured Image for display on page?
If you send the ?_embed param to the query, it will return more information in the response, like images, categories, and author data.
const result = await axios.get(`/wp-json/wp/v2/my-post-type?_embed`);
// Featured Image
result.data._embedded['wp:featuredmedia'][0].source_url;
// Thumbnail
result.data._embedded['wp:featuredmedia'][0]['media_details']['sizes']['medium']['source_url']
I am using ng-repeat to create a table of data:
<div class="divTable" ng-repeat="expense in exp.expenses | filter:exp.query">
<div>{{expense.amount | ldCurrency : true}}</div>
...
</div>
A couple of the cells that I am creating are being modified through an Angular filter. In the example above, I am changing the integer to a currency. So the original 4 is changed to $4.00. When I filter the entire list with my exp.query, it does not modify the exp.query search term through the ldCurrency.
The means that if I search on $4, it will not find it, because the backing data is 4, even though $4 is on the page.
I know this is confusing, with the two types of filters that I am talking about here.
How can I search on the data that is being shown on the page and not on the backing data?
You have to create you own filter. What you want to do to is a bad idea, because you are melding the view layer and the model layer.
A example of a filter.
The html:
<input ng-model="query" ng-trim="true">
<span>Finding: </span><span>{{ query }}</span>
<div ng-repeat="product in products | productsFilter: query">
<strong>{{ $index }}</strong>
<span>{{ product.name }}</span>
<span>{{ product.price | currency: '$'}}</span>
</div>
The custom filter:
.filter('productsFilter', [function () {
// Private function: It removes the dollar sign.
var clearQuery = function (dirtyQuery) {
var index = dirtyQuery.indexOf('$');
if (index === -1)
return dirtyQuery;
return dirtyQuery.substr(index+1, dirtyQuery.length-index)
};
// The Custom filter
return function (products, query) {
if (query === '') return products;
var newProducts = [];
angular.forEach(products, function (product) {
var cleanQuery = clearQuery(query);
var strProductPrice = '' + product.price;
var index = strProductPrice.indexOf(cleanQuery);
if (index !== -1) {
newProducts.push(product);
}
});
return newProducts;
};
}]);
The key is in the angular.forEach. There I decide if the product will belong to the new filtered collection. Here you can do the match you want.
You can find the complete example in full plucker example and see a lot of filters in the a8m's angular-filter