Can anyone direct me how to display values of array in separate columns with ng-repeat? Please note that fields are dynamically generated and I cannot hardcode name of fields like tran.id, or tran.firstname....
thanks
My html is:
<tr ng-repeat="tran in trans">
<td>{{ tran }}</td>
</tr>
My JS code is:
$scope.displayTrans = function(){
$http.get("model/selecttrans.php")
.then(function(response) {
console.log(response.data);
$scope.trans = response.data;
});
}
and my PHP code is:
<?php
$sql = "SELECT * FROM trans";
$stmt = $conn->prepare($sql);
$stmt->execute();
$total = $stmt->rowCount();
if ($total > 0 ) {
while ($row = $stmt->fetchObject()) {
$output[] = $row;
}
} else {
$output = 'No data';
}
echo json_encode($output);
I am getting following output in my console:
[…]
0: {…}
"$$hashKey": "object:15"
email: null
firstname: "Aziz"
id: "19"
lastname: "Virani"
password: "12345"
__proto__: Object { … }
1: {…}
"$$hashKey": "object:16"
email: "test#test.edu"
firstname: "Test"
id: "32"
lastname: "Result"
password: "test"
__proto__: Object { … }
length: 2
__proto__: Array []
and following output in my browser:
{"id":"19","lastname":"Virani","password":"12345","firstname":"Aziz","email":null}
{"id":"32","lastname":"Result","password":"test","firstname":"Test","email":"test#test.edu"}
Can any one suggest me how can I display output in separate columns as mentioned below:
id | lastname | password
32 | Result | test
Please ignore validation here like password should be hashed or md5 etc.....
I can easily get data by typing {{ tran.id }} or {{ tran.firstname }} but these fields are dynamically generated and i cannot hardcode fields name....
thank you
Aziz
So I figured out the way to display output....
Earlier I used nested ng-repeat with (key, value) with table row property but here on some other blog i found that nested ng-repeat works with table data so I updated my code and everything works cool....
Related
I have a block of code that updates document data
const { serverTimestamp } = firebase.firestore.FieldValue;
// The function firebase.firestore.Timestamp.fromDate(data.dob) seems to produce this behaviour
const fsData = {
...
dob: firebase.firestore.Timestamp.fromDate(data.dob),
doj: firebase.firestore.Timestamp.fromDate(data.doj),
...
}
try {
console.log(`fsData`, fsData)
await db.collection(FIRESTORE_COLLECTIONS.CLIENT_COLLECTION).doc(clientId).update({
...fsData,
updatedAt: serverTimestamp(),
});
console.log(`fsData`, fsData)
return true
} catch (e) {
console.log(`e`, e)
}
After the call to this function, It executes up until first console.log(fsData, fsData)
Then nothing happens, There is no response or error messages coming up. Then after a few minutes...
After experimenting, It was found that firebase.firestore.Timestamp.fromDate(data.dob) is causing this. Because, whenever i remove those two data, it was working.
The data that's being passed to this function Date type, This works well with creating documents but has issues when updating.
and here's the object right before passing to update
fsData
Object { doj: {…}, dob: {…}, firstName: "Amaya", lastName: "Gilbert", address: "Dolor quaerat quos l", cycle: "24", phone: "+123123123123" }
address: "Dolor quaerat quos l"
cycle: "24"
dob: Object { seconds: 1626449262, nanoseconds: 0 }
doj: Object { seconds: 1626535662, nanoseconds: 0 }
firstName: "Amaya"
lastName: "Gilbert"
phone: "+123123132123"
<prototype>: Object { … }
clientContext.tsx:128:20
#firebase/firestore: Firestore (8.7.0): Connection WebChannel transport errored: appears in console as warning.
Other functions, create and delete works well.
Refer the below code, I am getting
0: {status: "DRAFT", $$hashKey: "object:282"}
1: {status: "SUBMITTED", $$hashKey: "object:283"}
2: {status: "APPROVED", $$hashKey: "object:284"}
3: {status: "REJECTED", $$hashKey: "object:285"}
4: {status: "RESUBMIT", $$hashKey: "object:286"}
5: {status: "APPROVAL_NR", $$hashKey: "object:287"}
length: 6
__proto__: Array(0)
as a response in "statusdata", I am using ng-repeat to display these data as a dropdown. I want to rename the last "approval_nr" in the view.
From angular docs: https://docs.angularjs.org/api/ng/directive/ngRepeat
You can use $last.
$last boolean true if the repeated element is last in the iterator.
You can also check this one: Different class for the last element in ng-repeat.
I would implement it like the following:
function renameIfLast(name, isLast) {
if (isLast) {
return name.toUpperCase(); // do you rename logic here <<
} else {
return name;
}
}
<h1 ng-repeat="x in records">{{renameIfLast(x.status, $last)}}</h1>
I am trying to retrieve objects from an array and select them individually. When an object is returned my tables cells are fill with that object i.e([object Object]), instead of the properties of that object filling the table. I know I probably need to destructure the returned array to retrieve the individual objects. However, I cannot find a way to destructure the array and send the objects to my html page. This is my code:
app.get('/lastName/:Name', function(req, res) {
var newArr = foo.lookupByLastName(req.params.Name);
res.type('text/html');
res.render('last', {LName: newArr});
}
Console.log(newArr) returns full array:
[{id:1, firstName: 'John', lastName: 'Doe'},
{id:1, firstName: 'James', lastName: 'Smith'},
{id:1, firstName: 'Jane', lastName: 'Doe'},
{.... ...]
I am trying to get something in return like this:
{id:1, firstName: 'John', lastName: 'Doe'}
{id:2, firstName: 'James', lastName: 'Smith'}
{id:3, firstName: 'Jane', lastName: 'Doe'}
Inside my html page I am trying to iterate over the returned objects and place each object in a single row and the corresponding properties in the other cells of a table.
<table border=1>
<tr>
{{#each LName}}
<td>
{{this}}
</td>
{{/each}}
</tr>
</table>
I found a temporary solution. However, with this solution I now need to create a loop that would give me the output for the res.render(). I create a loop thinking I could place the string in res.render but was given an error. This is what I did:
app.get('/lastName/:Name', function(req, res) {
var newArr = foo.lookupByLastName(req.params.Name);
res.type('text/html');
res.render('last', {Per1: newArr[0], Per2: newArr[1], Per3:newArr[3] });
}
This is the look I would have used if it worked:
text ="{";
for (;newArr[i];)
{text+="Per"+i+":"+ "newArr["+i+"],";
i++;}
text+='}';
With this loop res.render would look like:
app.get('/lastName/:Name', function(req, res) {
var newArr = foo.lookupByLastName(req.params.Name);
res.type('text/html');
res.render('last', text);
}
I could use some help on how i could store an array to the session storage:
So i have a json object that returns this
Object
Objectauthority: "USER_ROLE"
email: "1#1"
enabled: true
firstName: "1"
id: 1
lastName: "1"
password: "$2a$10$qPjtVDxkCh3KaE2mr0.ZeuyyjceLy7JPVmelttVf7uekSQq01fZ9u"
but it has also returns a client list
clientList: Array[7]
city: "Brussel"
company: "John Doe's coffee shop"
country: "Belgium"
...
This is the angularjs code
$http.get('/api/getuser').success(function(data) {
$scope.userdata = data;
console.log(data)
//Here we add all the stuff we need to the sessionStorage
$sessionStorage.userid = data.id;
$sessionStorage.userEmail = data.email;
$sessionStorage.sampleString = "This is a sample string";
//But how can i store the array to the sessionStorage ?
//Is something like this possible ?
$sessionStorage.clientArray[] = data.clientList[]
})
Ok i have it working i only needed to remove the [] So something like this works :
$sessionStorage.clientArray = data.clientList
I have spent doing such a straight forward thing. I just want to do a CRUD operation on a user model using nodejs, mongoose, restify stack. My mongo instance is on mongolab.
The user should contain a "loc" field . User schema is as follows :
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var userSchema = new Schema( {
email_id : { type: String, unique: true },
password: { type: String},
first_name: String,
last_name: String,
age: String,
phone_number: String,
profile_picture: String,
loc: {
type: {},
coordinates: [Number]
}
});
userSchema.index({loc:'2d'});
var User = mongoose.model('user', userSchema);
module.exports = User;
the rest api used to post is as follows :
create_user : function (req, res, next) {
var coords = [];
coords[0] = req.query.longitude;
coords[1] = req.query.latitude;
var user = new User(
{
email_id : req.params.email_id,
password: req.params.password,
first_name: req.params.first_name,
last_name: req.params.last_name,
age: req.params.age,
phone_number: req.params.phone_number,
profile_picture: req.params.profile_picture,
loc: {
type:"Point",
coordinates: [1.0,2.0] // hardcoded just for demo
}
}
);
user.save(function(err){
if (err) {
res.send({'error' : err});
}
res.send(user);
});
return next();
},
Now when i do a POST call on curl -X POST http://localhost:3000/user --data "email_id=sdass#dfAadsfds&last_name=dass&age=28&phone_number=123456789&profile_picture=www.jakljf.com&longitude=1.0&latitude=2.0"
I get the following error
{
error: {
code: 16804
index: 0
errmsg: "insertDocument :: caused by :: 16804 location object expected, location array not in correct format"
op: {
email_id: "sdass#dfAadsfdsadkjhfasvadsS.com"
password: "sdass123DadakjhdfsfadfSF45"
first_name: "shaun"
last_name: "dass"
age: "28"
phone_number: "123456789"
profile_picture: "www.jakljf.com"
loc: {
coordinates: [2]
0: 1
1: 2
-
type: "Point"
}-
_id: "55efc95e0e4556191cd36e5e"
__v: 0
}-
}-
}
The location field is giving problems as the POST call works just fine if i remove the loc field from model
Below are the hits/trials I did :
1) Change userSchema.index({loc:'2d'}); to userSchema.index({loc:'2dsphere'});
2) Changing loc schema to everything given in Stackoverflow. I would like to know the right way to define this though.
3) Passing the hardcode 2d array but still it says Location object expected, location array not in correct format" what format is required for this ?
Any help in this regard is greatly appreciated. Thanks.
MongoDB 2d index requires the legacy coordinates pairs format, which is just an array of coordinates like [1, 2].
If you need GeoJSON support, please use the 2dsphere index.
userSchema.index({loc:'2dsphere'});
If you are using Spring Boot make sure you set the index type to 2DSphere:
#GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) GeoJsonPoint location;