I have an array of object and I am running ngFor on that, I want to show different view for each iteration of ngFor
const HEROES = [
{id: 1, name:'Superman'},
{id: 2, name:'Batman'},
{id: 5, name:'BatGirl'},
{id: 3, name:'Robin'},
{id: 4, name:'Flash'}
];
Like Superman should be default view than when I click next batman should be shown the next will be batgirl and so on.
Do you have any idea?
You can show the index without using the ngFor by following these steps.
Create Default View Component to show the Name
Create Component to show next name and use ngIf to hide/show that component.
Use a variable and iterate with that.
I have created a small prototype which can explain the steps mentioned above.
https://stackblitz.com/edit/angular-for-loop
Related
I am working in angular 6 here i want to push multiple values into array which gives me the following error
here is my code
this._model.NomineeList.push(
{
'FirstName': this._nomineemodel.FirstName,
'CNIC': this._nomineemodel.CNIC,
'MiddleName': this._nomineemodel.MiddleName,
'LandlineNumber': this._nomineemodel.LandlineNumber,
'LastName': this._nomineemodel.LastName,
'MobileNumber': this._nomineemodel.MobileNumber,
'PermanentAddress': this._nomineemodel.PermanentAddress,
'PresentAddress': this._nomineemodel.PresentAddress,
'RelationId': this._nomineemodel.RelationId,
'RelationName': this._nomineemodel.RelationName,
'UPermanentAddress': '',
'UPresentAddress': ''
});
How to push into array using angular 6.
The error is self explanatory, the kind on model you are inserting in your array they are not identical. there are two possible reasons.
1) Either you haven't initiated the array with empty values. or
2) The model you are inserting is missing mandatory properties or the new model having few extra properties or spell mistake in properly name.
Check this stackblitz example.
I am converting a shopping basket to an immutable structure.
Is there an easy way with immutablejs to see if an immutable object already exists within an immutable list EXCEPT for one object property 'quantity' which could be different? List example:
[{
id: 1,
name: 'fish and chips',
modifiers: [
{
id: 'mod1',
name: 'Extra chips'
}
],
quantity: 2
},{
id: 2,
name: 'burger and chips',
modifiers: [
{
id: 'mod1',
name: 'No salad'
}
],
quantity: 1
}]
Now, say I had another object to put in the list. But I want to check if this exact item with modifiers exists in the list already? I could just do list.findIndex(item => item === newItem) but because of the possible different quantity property then it wont work. Is there a way to === check apart from one property? Or any way to do this without having to loop through every property (aside from quantity) to see if they are the same?
Currently, I have an awful nested loop to go through every item and check every property to see if it is the same.
Well this should work-
list.findIndex(item => item.delete("quantity").equals(newItem.delete("quantity"))
The equals method does deep value comparison. So once you delete the quantity, you are comparing all values that matter.
PS: please ignore code formatting, I am on SO app.
PPS: the above code is not optimal, you should compare a pre-trimmed newItem inside the arrow function instead of trimming it there.
Suppose I have created a custom element as follow:
<chat-container messages="{{ messages }}"></chat-container>
messages is an array containing chat messages related to any particular thread or contact.
Now, in my app.js, I have this kind of message array which contains all messages by thread identifiers. How can I pass the messages related to any particular thread identifier(i.e name='Rajat') to <chat-container> element? I have tried nearly all combinations and still nothing is making sense.
app.messages = [
{name: 'Rajat', messages: [{id: 1,message: 'Hello'},{id: 3,message: 'Hello 2'}]},
{name: 'Himesh', messages: [{id: 2,message: 'Hello'}]},
{name: 'David', messages: [{id: 4,message: 'Hello'}]}
]
I tried to do:
<chat-container messages="{{ app.messages.filter(function(e) { return e.name === 'Rajat'; })[0].messages }}"></chat-container>
and it did not work.
Polymer doesn't support such complex binding expression. Move the filter code to a function and call that function from the binding expression. Binding expressions only support .``!, ()
<chat-container messages="{{filter('name', 'Rajat')}}"></chat-container>
where the parent element has a function named filter()
I am building a CSV file parser through node and Angular . so basically a user upload a csv file , on my server side which is node the csv file is traversed and parsed using node-csv
. This works fine and it returns me an array of object based on csv file given as input , Now on angular end I need to display two table one is csv file data itself and another is cross tabulation analysis. I am facing problem while rendering data, so for a table like
I am getting parse responce as
For cross tabulation we need data in a tabular form as
I have a object array which I need to manipulate in best possible way so as to make easily render on html page . I am not getting a way how to do calculation on data I get so as to store cross tabulation result .Any idea on how should I approach .
data json is :
[{"Sample #":"1","Gender":"Female","Handedness;":"Right-handed;"},{"Sample #":"2","Gender":"Male","Handedness;":"Left-handed;"},{"Sample #":"3","Gender":"Female","Handedness;":"Right-handed;"},{"Sample #":"4","Gender":"Male","Handedness;":"Right-handed;"},{"Sample #":"5","Gender":"Male","Handedness;":"Left-handed;"},{"Sample #":"6","Gender":"Male","Handedness;":"Right-handed;"},{"Sample #":"7","Gender":"Female","Handedness;":"Right-handed;"},{"Sample #":"8","Gender":"Female","Handedness;":"Left-handed;"},{"Sample #":"9","Gender":"Male","Handedness;":"Right-handed;"},{"Sample #":";"}
There are many ways you can do this and since you have not been very specific on the usage, I will go with the simplest one.
Assuming you have an object structure such as this:
[
{gender: 'female', handdness: 'lefthanded', id: 1},
{gender: 'male', handdness: 'lefthanded', id: 2},
{gender: 'female', handdness: 'righthanded', id: 3},
{gender: 'female', handdness: 'lefthanded', id: 4},
{gender: 'female', handdness: 'righthanded', id: 5}
]
and in your controller you have exposed this with something like:
$scope.members = [the above array of objects];
and you want to display the total of female members of this object, you could filter this in your html
{{(members | filter:{gender:'female'}).length}}
Now, if you are going to make this a table it will obviously make some ugly and unreadable html so especially if you are going to repeat using this, it would be a good case for making a directive and repeat it anywhere, with the prerequisite of providing a scope object named tabData (or whatever you wish) in your parent scope
.directive('tabbed', function () {
return {
restrict: 'E',
template: '<table><tr><td>{{(tabData | filter:{gender:"female"}).length}}</td></tr><td>{{(tabData | filter:{handedness:"lefthanded"}).length}}</td></table>'
}
});
You would use this in your html like so:
<tabbed></tabbed>
And there are ofcourse many ways to improve this as you wish.
This is more of a general data structure/JS question than Angular related.
Functional helpers from Lo-dash come in very handy here:
_(data) // Create a chainable object from the data to execute functions with
.groupBy('Gender') // Group the data by its `Gender` attribute
// map these groups, using `mapValues` so the named `Gender` keys persist
.mapValues(function(gender) {
// Create named count objects for all handednesses
var counts = _.countBy(gender, 'Handedness');
// Calculate the total of all handednesses by summing
// all the values of this named object
counts.Total = _(counts)
.values()
.reduce(function(sum, num) { return sum + num });
// Return this named count object -- this is what each gender will map to
return counts;
}).value(); // get the value of the chain
No need to worry about for-loops or anything of the sort, and this code also works without any changes for more than two genders (even for more than two handednesses - think of the aliens and the ambidextrous). If you aren't sure exactly what's happening, it should be easy enough to pick apart the single steps and their result values of this code example.
Calculating the total row for all genders will work in a similar manner.
I am implementing server side pagination in AngularJS following the code at laravel-tricks
I pass pageNumber to the API and it returns the total number of results and other data on that page in the results. The data is then displayed in the table.
How can I generate a list of hrefs for pages dynamically when the results are received ?
Note 1: I have tried ngTable, but it doesn't update the table when the data is changed though it does update the model. Hence resorted to manual pagination since I need a basic pagination only
Note 2: I am not using Bootstrap there for pagination provided by AngularUI is not of any use.
You can do something like this:
//Generate pagination buttons, each with the ng-click directive
<button ng-repeat="pageNumber in [1, 2, 3, 4, 5]" ng-click="goToPage(pageNumber)">
<span ng-bind="pageNumber"></span>
</button>
Then have a goToPage function in your controller like this:
$scope.goToPage = function(pageNumber) {
$scope.main.page = pageNumber;
$scope.loadPage();
};
NOTE: I just used [1, 2, 3, 4, 5] as an example, but you may want to generate that array in your controller.