Angularjs ng-options for an object - angularjs

I have a form that collects information such as a singer's first and last name, 2 guitarists first and last names and a drummer's first and last name. I'm attempting to push these values into select tag with ng-options.
I can successfully get in the guitarists names to show up in the select drop down, but I can't seem to get the singer and the drummer to show up.
<select ng-model="band" ng-options="band.firstName for firstName in band"></select>
$scope.band = {"singer": {
"firstName": "Dave",
"lastName": "Grohl"
},"guitarists": [
{
"firstName": "Chris",
"lastName": "Shiflett"
},
{
"firstName": "Pat",
"lastName": "Smear"
}],"drummer": {
"firstName": "Taylor",
"lastName": "Hawkins" }};
Here's a fiddle: http://jsfiddle.net/rfT7f/17/
Any insight would be appreciated.

With ngOptions, you have to use a consistent object (or array) structure like
$scope.band = [
{
"firstName": "Dave",
"lastName": "Grohl",
"role": "singer"
},
{
"firstName": "Chris",
"lastName": "Shiflett",
"role": "guitarist"
},
{
"firstName": "Pat",
"lastName": "Smear",
"role": "guitarist"
},
{
"firstName": "Taylor",
"lastName": "Hawkins",
"role": "drummer"
}
];
Then you can do cool things like group options together in your select:
<select ng-model="bandMember"
ng-options="artist.firstName group by artist.role for artist in band">
</select>
Edit:
Forgot to mention your ngModel should point to where you want to store the selected option, in this case $scope.bandMember

Related

How to select adjacent rows in React?

I am working on project with React + Typescript as tech stack.
I have one requirement where I need to select adjacent rows in table.
I have array of object just like
let arrayOfObj = [
{
"firstname": "fname",
"lastname": "lname"
},
{
"firstname": "fname1",
"lastname": "lname1"
},
{
"firstname": "fname2",
"lastname": "lname2"
},
{
"firstname": "fname3",
"lastname": "lname3"
},
{
"firstname": "fname4",
"lastname": "lname4"
},
{
"firstname": "fname5",
"lastname": "lname5"
},
]
In my current application I am able to click on any row able to get data which is present at that row.
Now I want to select the adjacent rows just like if I click on row with
{
"firstname": "fname4",
"lastname": "lname4"
}
And I press shiftKey + row with data
{
"firstname": "fname1",
"lastname": "lname1"
}
Then rows from fname4 & lname 4 to fname1 &lname1 should be selected.
And Vica versa if I select row with data fname1 &lname1 and again press shift + row with data fname3 &lname3 then it should select data from fname1 &lname1 to fname3 &lname3.

How to pass the values to point json object in ReactJs

I am new to ReactJS, I would like to fetch the Firstname based on active language selected using React
language file.Json
{"activelanguage":"English",
"English":
{
"id":1,
"first_name": "First name",
"last_name": "Last name",
"email": "Email",
"password": "Password",
"mobile": "Mobile Phone"
},
"தமிழ்":{
"id":2,
"first_name": "முதல் பெயர்",
"last_name": "கடைசி பெயர்",
"email": "மின்னஞ்சல்",
"password": "கடவுச்சொல்",
"mobile": "கைபேசி எண்"
}
}
What i have used in the ReactCode;
var activelanguage="English";
var firstName=languagelist.{activelanguage}.first_name;
Thanks in advance
var firstName=languagelist[activelanguage].first_name;
See this post for details
If you want to point to the property of an object, you can use strings. For example:
var activeLanguage="English";
var firstName = languagelist[activeLanguage]?.first_name || defaultCase; // <- just add a default case.

How to render multiple rows for a a only a given column in react using react tables?

I want to display tabular data of the format,
[
{
"user1":{
"firstName": ["abcd"],
"lastName":[ "xyz"],
},
"user2": {
"firstName": "qwer",
"lastName": "nmas"
},
"others": [
{
"firstName": "sadas",
"lastName": "sadas"
},
{
"firstName": "cccc",
"lastName": "nmcs"
}
]
}
]
I want to render this data using react rows. How do I render others which is an array ?
Assuming your array is assigned to variable data:
data[0].others.map(item => <p>{`${item.firstname} ${item.lastName}`}</p>)
If your data array contains more than 1 item, you can map it too:
data.map(d => d.others.map(item => <p>{`${item.firstname} ${item.lastName}`}</p>))

Why the if statement is not accepting bracket-notation instead of dot-notation?

var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}];
function lookUpProfile(firstName, prop){
for (var i = 0; i < contacts.length; i++){
if (firstName === contacts[i].firstName) {
if (contacts[i][prop]) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
lookUpProfile("Akira", "likes");
This code runs perfectly. but if i use if (firstName === contacts[i] [firstName]) instead of if (firstName === contacts[i].firstName), it doesn't work. Why? Since either dot or bracket notation both can be used for accessing the objects property.
The brackets are for variables. If you want to use them with String literals, you could, but you have to quote them: contacts[i]['firstName']. But unless the property name contains restricted characters (such as another dot or a space), that is just silly.
Note that you already used it with i (which is a variable) to get the i-th element (and not a property called i).

how to map the columnNames to table data and show only that data in uigrid

i get columnNames dynamically with object inside of an property,i just want to show only that property which is inside of that object.
this is my columnNames
$scope.columnNames=[
{"field":"firstName"},
{"field":"lastName"},
{"field":"id"},//i can get "id" or "name" or anything it comes dynamically
{"field":"employed"}
];
and i get the data like this
$scope.gridOptions.data = [
{
"firstName": "Cox",
"lastName": "Carney",
"id": "{'id':'1','name':'Syed'}",
"employed": true
},
{
"firstName": "Lorraine",
"lastName": "Wise",
"id": "{'id':'2','name':'Rasheed'}",
"employed": false
},
{
"firstName": "Nancy",
"lastName": "Waters",
"id": "{'id':'3','name':'Emir'}",
"employed": false
}
];
when i run this am getting the whole object "{'id':'3','name':'Emir'}" in id column but i want only that id property value to show in that column.
and this is my plunker http://plnkr.co/edit/AMeTxuMMBmfVt9f0t7HZ?p=preview
See this plnkr
added the following code to parse the id from json to id field
angular.forEach($scope.gridOptions.data, function(value, key) {
var parsedId = angular.fromJson(value.id.replace(/'/g, '"'));
value.id = parsedId.id;
});
also, seems like your json is ' delemited instead of ", so I had to replace the string so JSON could be parsed but if you are getting proper JSON form server you don't need to do that.
Hope this helps
Try using cellTemplate in $scope.columnNames.
Just add cellTemplate: '<span>{{COL_FIELD[col.field] to $scope.columnNames like this:
$scope.columnNames=[
{"field":"firstName"},
{"field":"lastName"},
{"field":"id", cellTemplate: '<span>{{COL_FIELD[col.field]}}</span>'}, //this cellTemplate works for "id" or "name" or anything
{"field":"employed"}
];
Since you get $scope.columnNames dynamically, you can add cellTemplate like this:
$scope.columnNames[2].cellTemplate='<span>{{COL_FIELD[col.field]}}</span>';
See this plnkr

Resources