Retrieve data without _id and _ref from coucgdb

96 views Asked by At

I have a database with lots of document and i am using field type to define it as a table. I want to populate angularjs ui-grid with the JSON data coming in value. So i have created a view:

function(doc) {
 if(doc.type === 'userTable'){
   emit(doc._id, {userName:doc.userName,fName:doc.fName});
 }
}

When i hit the url http://127.0.0.1:5984/rpt_db/_design/Dsgn_userjson/_view/Vw_userjson/ it gives me :

{"total_rows":1,"offset":0,"rows":[
{"id":"f43a147cd5c961fcfe4e1da1b800013a",
"key":"f43a147cd5c961fcfe4e1da1b800013a",
"value":{"userName":"rp670249","fName":"Ranjeeth"}}
]}

Now in my client code i need to do a loop and make data as JSON.

result.rows.forEach(function(item) {
  var temp = { "id": item.value.userName, usrName: item.value.fName};
  console.log(temp);
  //Update data of grid using temp variable.
});

Is there an easy way so that i can directly use what's coming as part of couchdb JSON output which can be used as is in angular ui-grid.

1

There are 1 answers

0
Kathir On BEST ANSWER

You can refer column values using json dot notation in the columnDefs. Since your actual values are inside "value". you can create column definition like this,

 $scope.myData = {"total_rows":1,"offset":0,"rows":[
{"id":"f43a147cd5c961fcfe4e1da1b800013a",
"key":"f43a147cd5c961fcfe4e1da1b800013a",
"value":{"userName":"rp670249","fName":"Ranjeeth"}}
]}
 $scope.gridOptions = {
   data : $scope.myData.rows,
   columnDefs : [
     {name:"id",field:"value.userName"},
     {name:"usrName",field:"value.fName"}
     ]
 };

Here's a working plnkr.

http://plnkr.co/edit/juLZeT6I0LOlek4QnMSP?p=preview