How to display values in post { } in following example in vuejs? - javascript

{"status":true,"data":[{"agent_id":7042,"p_id":7039,"post":{"author":83,"created_on":"2017-10-07 14:28:36","sub_category":"Litigation Support","category":"Personal Investigation","budget":"5555","views":"0","status":"986","id":7039,"country":"India","state":"Kerala","quote":{"ref_id":"61","agent_id":7042,"p_id":7039,"created_on":"2017-10-09 10:41:15"}}},{"agent_id":7042,"p_id":7040,"post":{"author":83,"created_on":"2017-10-09 12:06:01","sub_category":"Pre-Matrimonial Investigation","category":"Personal Investigation","budget":"5555","views":"0","status":"984","id":7040,"country":"India","state":"Kerala","quote":{"ref_id":"61","agent_id":7042,"p_id":7039,"created_on":"2017-10-09 10:41:15"}}}]}
<div id="quotes">
<div v-for="post in data" class="mke_">
<card-component v-bind:id="post.p_id" v-bind:ins="post.category" v-bind:country="post.country" v-bind:state="post.state" v-bind:attachment_id="post.attachment_preview">
</card-component>
</div>
this is my vue js code to get the values, The values obtained is in the above format
quotes = new Vue({
'el' : '#quotes',
data : {
posts : [],
has_no_posts : true
},
mounted : function(){
var self = this;
$.ajax({
url : "url",
method : "POST",
dataType : "JSON",
data : {},
success : function(e){
if(e.status == true){
self.data = e.data;
if(e.data.length > 0)
self.has_no_posts = false;
}
}
});
}
});
This is my code to display the same which is getting error. So somebody please help me to didplay the same

You have to be more careful with your data.
First issue: you assign the result of your ajax query to self.data instead of self.posts.
Second issue: every "post" actually contains another "post" object with the actual post properties. So you need to use post.post.category to grab that.
See the below snippet. Note: I replaced your aja call with a setTimeout(0).
Finally, you should turn has_no_posts into a computed property that depends on self.posts.
var e = {
"status": true,
"data": [{
"agent_id": 7042,
"p_id": 7039,
"post": {
"author": 83,
"created_on": "2017-10-07 14:28:36",
"sub_category": "Litigation Support",
"category": "Personal Investigation",
"budget": "5555",
"views": "0",
"status": "986",
"id": 7039,
"country": "India",
"state": "Kerala",
"quote": {
"ref_id": "61",
"agent_id": 7042,
"p_id": 7039,
"created_on": "2017-10-09 10:41:15"
}
}
}, {
"agent_id": 7042,
"p_id": 7040,
"post": {
"author": 83,
"created_on": "2017-10-09 12:06:01",
"sub_category": "Pre-Matrimonial Investigation",
"category": "Personal Investigation",
"budget": "5555",
"views": "0",
"status": "984",
"id": 7040,
"country": "India",
"state": "Kerala",
"quote": {
"ref_id": "61",
"agent_id": 7042,
"p_id": 7039,
"created_on": "2017-10-09 10:41:15"
}
}
}]
};
new Vue({
el: '#quotes',
data: {
posts: [],
has_no_posts: true
},
mounted: function() {
var self = this;
setTimeout(function() {
self.posts = e.data;
self.has_no_posts = e.data.length > 0;
}, 0);
}
});
<script src="https://unpkg.com/vue"></script>
<div id="quotes">
<div v-for="post in posts" class="mke_">
<span v-bind:id="post.p_id">{{post.post.category}} - {{post.post.country}} - {{post.post.state}}</span>
</div>
</div>

Related

How to Accessing fields from custom search results in netsuite?

In the ui, I have created a custom transaction search with vendor lines. How do I access vendor lines values? I have something like this:
var veResults = vendorSearch.run().getRange({
start: 0,
end: 50
});
for(var i = 0; i < veResults.length; i++)
{
var vendorName = veResults[i].getValue({name: 'vendorLine.entityid');
context.response.write(vendorName);
};
If I print veResults, I get the following json:
[
{
"recordType": "vendorpayment",
"id": "210",
"values": {
"employee.entityid": "",
"trandate": "5/20/2015",
"print": "Print",
"type": [
{
"value": "VendPymt",
"text": "Bill Payment"
}
],
"payrollbatch": "",
"tranid": "2009",
"entity": [
{
"value": "35",
"text": "Pacific Bell Telephone"
}
],
"account": [
{
"value": "1",
"text": "1000 Checking"
}
],
"otherrefnum": "",
"statusref": [],
"trackingnumbers": "",
"memo": "",
"currency": [
{
"value": "1",
"text": "USA"
}
],
"expectedreceiptdate": "",
"trandate_1": "5/20/2015",
"enddate": "",
"item": [],
"vendorLine.entityid": "Pacific Bell Telephone",
"vendorLine.billaddress": "Pacific Bell Telephone\nPacific Bell Payment Center\nSacramento CA 95887-0001\nUS"
}
...]
So, the value is there, how do I get it? The search object has only getValue and getText methods, none of which work in this case.
There is a syntax error with this line
var vendorName = veResults[i].getValue({'vendorLine.entityid');
It should be
var vendorName = veResults[i].getValue('vendorLine.entityid');

Looping through JSON structure with jQuery

I am having some troubles with looping through a JSON structure through jQuery,
Here is my JSON data:
{
"suppliers": [
{
"Supplier": {
"id": "5704ebeb-e5e0-4779-aef4-16210a00020f",
"name": "Gillmans",
"mobile": "",
"office_telephone": "00000",
"ooh_contact": "00000",
"fax_number": "",
"address_line_1": "St Oswalds Road",
"address_line_2": "Gloucester",
"address_line_3": "",
"address_line_4": "",
"postcode": "GL1 2SG",
"email": "email#example.com",
"contact": "",
"position": "",
"aov": "180.00",
"engineer": false,
"cc_on_new_job_emails": true,
"can_add_quotes": false,
"notes": "",
"status": "1",
"created": "2016-04-06 11:58:51",
"modified": "2016-07-27 11:23:01",
"status_text": "Active",
"engineer_text": "No",
"cc_on_new_job_emails_text": "Yes"
},
"Trade": [],
"PostcodeArea": []
},
{
"Supplier": {
"id": "571e390f-91e8-4745-8f78-168b0a00020f",
"name": "Kings",
"mobile": "",
"office_telephone": "00000",
"ooh_contact": "0000",
"fax_number": "",
"address_line_1": "",
"address_line_2": "",
"address_line_3": "",
"address_line_4": "",
"postcode": "",
"email": "",
"contact": "",
"position": "Account Manager; Joanne Brook",
"aov": null,
"engineer": false,
"cc_on_new_job_emails": false,
"can_add_quotes": false,
"notes": "",
"status": "1",
"created": "2016-04-25 16:34:39",
"modified": "2016-07-08 15:22:15",
"status_text": "Active",
"engineer_text": "No",
"cc_on_new_job_emails_text": "No"
},
"Trade": [],
"PostcodeArea": []
}
]
}
This JSON is returned from my AJAX call in a variable called data. data is a Javascript object, i.e. it's already been parsed by the ajax call.
I am trying to loop through this JSON data and grab the name and id properties. Here is how I have done it:
$.each(data, function(k, v) {
$.each(this, function(key, val) {
$.each(this, function(key2, val2) {
$.each(this, function(key3, val3) {
if(key3 == 'name')
{
alert(val3);
}
});
});
});
});
This will print all of the name values but obviously this is quite a messy way and I was wondering if there is an easier way I can get the name and id properties of this structure and store them in variables?
You can deal with the JSON as an object if you parse it:
//this has already been done by the ajax call by the sounds of it.
//var jsObj = JSON.parse(data);
//suppliers is an array now ([]), so loop it
$.each(data.suppliers, function(index, value){
//value is a supplier object ({}) so you can acces it's properties directly
alert(value.Supplier.name);
});
$.each(data.suppliers, function(){
alert(this.Supplier.id);
});
Try this :
var data = {
"suppliers":[
{
"Supplier":{
"id":"5704ebeb-e5e0-4779-aef4-16210a00020f",
"name":"Gillmans"
},
"Trade":[
],
"PostcodeArea":[
]
},
{
"Supplier":{
"id":"571e390f-91e8-4745-8f78-168b0a00020f",
"name":"Kings"
},
"Trade":[
],
"PostcodeArea":[
]
}
]
}
$.each(data.suppliers, function(k, v) {
alert(this.Supplier.id);
})

How to avoid duplicate records adding inside angular for each loop?

The below directive code is forming an JSON dynamically. I have to form a JSON without dupicate object names.
Angular Code:
bosAppModule.directive('layoutTableCellControlLabelRender',function($compile,$rootScope){
var layoutTableCellControlLabelObj={};
var soJSON = {
entityInfo: {
entity: "",
tenantId: "",
timeStamp: new Date().toJSON().toString()
},
collections: {
}
};
var myobj={};
linkFnTableCellControlLabel=function(scope, element, attributes, controllerCtrl) {
scope.labelData="NO DATA";
angular.forEach(scope.pageObject.collections.objectattribute.rowset, function (value, index) {
if(value.objectattributeid==scope.attributeId){
scope.labelData=value.objectattributelabelname;
scope.attributeName=value.objectattributename;
//$rootScope.$broadcast("NEW_EVENT", scope.attributeName);
angular.forEach(scope.pageObject.collections.object.rowset, function (value2, index2) {
if(value2.tenantobjectid==value.objectattributeobjectid){
scope.objectname=value2.tenantobjectname;
if(!soJSON.collections[scope.objectname]) {
soJSON.collections[scope.objectname]={
"meta": {
"parentreference": "***",
"pkname": "***",
"fkname": "***"
},
"rowset": [],
"rowfilter": []
};
}
}
});
myobj[scope.attributeName]="test";
soJSON.collections[scope.objectname].rowset.push(myobj);
}
});
console.log(JSON.stringify(soJSON));
};
layoutTableCellControlLabelObj.scope={attributeId:'=',layoutData:'=',pageObject:'='};
layoutTableCellControlLabelObj.restrict='AE';
layoutTableCellControlLabelObj.replace='true';
layoutTableCellControlLabelObj.template="<div class='col-xs-12 col-sm-12 col-md-6 col-lg-6' attribute-name={{attributeName}} attribute-id='tablecellcontrol.layouttablecellcontrolbindingobjectattributeid' " +
"layout-data='layoutData' page-object='pageObject'><label class='k-label pull-right'>{{labelData}}</label></div>";
layoutTableCellControlLabelObj.link = linkFnTableCellControlLabel;
return layoutTableCellControlLabelObj;
});
Using aboeve code i am getting the JSON. In this JSON inside rowset records are duplicated because of loop. But i'm bit confused on this. I need avoid it. It should create only one time.
Anyone please help me to achieve this. If you need more details on this. let me know.
JSON
{
"entityInfo": {
"entity": "",
"tenantId": "",
"timeStamp": "2016-04-07T07:25:49.711Z"
},
"collections": {
"Customer29Jan16": {
"meta": {
"parentreference": "***",
"pkname": "***",
"fkname": "***"
},
"rowset": [
{
"CuId": "test",
"Name": "test",
"Quantity": "test",
"Rate": "test",
"Amount": "test"
},
{
"CuId": "test",
"Name": "test",
"Quantity": "test",
"Rate": "test",
"Amount": "test"
},
{
"CuId": "test",
"Name": "test",
"Quantity": "test",
"Rate": "test",
"Amount": "test"
},
{
"CuId": "test",
"Name": "test",
"Quantity": "test",
"Rate": "test",
"Amount": "test"
},
{
"CuId": "test",
"Name": "test",
"Quantity": "test",
"Rate": "test",
"Amount": "test"
}
],
"rowfilter": []
}
}
}
I don't know if it's always the same json but you can probably use uniqby from lodash on your rowset array.
_.uniqBy(json.collections.Customer29Jan16.rowset, function (e) {
return e.CuId;
});
Demo
EDIT
If you have several customers, you can loop over a customer array :
arrayOfCustomers = (Object.keys(json.collections));
for(var i = 0 ; i < arrayOfCustomers.length; i++){
json.collections[arrayOfCustomers[i]].rowset = _.uniqBy(json.collections[arrayOfCustomers[i]].rowset, function (e) {
return e.CuId;
});
}
https://jsfiddle.net/kq9gtdr0/23/

How to add some code inside the JavaScript array?

Enviroment: Visual Studio 2012, MVC4, Razor, Internet Application.
I have a JavaScript array..
var over100 = "#Model.Over100";
var filterarray = [
{
"name": "Condition",
"value": "New",
"paramName": "",
"paramValue": ""
},
{
"name": "FreeShippingOnly",
"value": "false",
"paramName": "Currency",
"paramValue": "USD"
},
];
In case of... if(over100 != null)
I want...
var filterarray = [
{
"name": "Condition",
"value": "New",
"paramName": "",
"paramValue": ""
},
{
"name": "FreeShippingOnly",
"value": "false",
"paramName": "Currency",
"paramValue": "USD"
},
{
"name": "Over100",
"value": "true",
"paramName": "",
"paramValue": ""
},
];
I would like to know all the possible ways, how to do that.
Thank You!
You can use push():
if(over100 != ''){
var addfilter = {"name": "Over100",
"value": "true",
"paramName": "",
"paramValue": ""}
filterarray.push(addfilter)
}
if(over100 != null) {
filterarray.push({"name": "Over100", "value": "true", "paramName": "", "paramValue": ""})
}

dynamic key in JSON file (used by backbone.js models for example...)

I am trying to create a backbone.js model with dynamic keys but can't seem to get it done right.
Here's my model:
window.MyModel = Backbone.Model.extend({
myDynamicVar : "country",
urlRoot: "api/myModel",
defaults: {
"id": null,
this.myDynamicVar : "USA",
"country": {
"route":"test/test/test",
"class":".usa2",
"txt":"USA 2"
},
"region": "California",
"year": "",
"description": "",
"picture": ""
}
});
Here I am trying to have myDynamicVar replaced by its content in my model.
Any thought how I could get this done?
Thanks in advance,
Jimmy
You won't be able to do that directly; the left side of the ":" in an object literal must be a constant. What you can do is this:
window.MyModel = Backbone.Model.extend({
myDynamicVar : "country",
urlRoot: "api/myModel",
defaults: function(fieldName) {
var value = {
"id": null,
"country": {
"route":"test/test/test",
"class":".usa2",
"txt":"USA 2"
},
"region": "California",
"year": "",
"description": "",
"picture": ""
};
value[fieldName] = "USA";
return value;
}(this.myDynamicVar)
});
edit — ah, I just noticed that "myDynamicVar" itself is part of the outer object. There's no way to refer to it with this in such a way, so if that's what you're trying to do the code would be different:
window.MyModel = Backbone.Model.extend(function() {
var value = {
myDynamicVar : "country",
urlRoot: "api/myModel",
defaults: {
"id": null,
"country": {
"route":"test/test/test",
"class":".usa2",
"txt":"USA 2"
},
"region": "California",
"year": "",
"description": "",
"picture": ""
}
};
value.defaults.[value.myDynamicVar] = "USA";
return value;
}());
If you want myDynamicVar itself to be dynamic, then you could pass it in as a parameter:
window.MyModel = Backbone.Model.extend(function(whichProperty) {
var value = {
myDynamicVar : "country",
urlRoot: "api/myModel",
defaults: {
"id": null,
"country": {
"route":"test/test/test",
"class":".usa2",
"txt":"USA 2"
},
"region": "California",
"year": "",
"description": "",
"picture": ""
}
};
value.defaults.[value[whichProperty]] = "USA";
return value;
}("myDynamicVar"));
You cannot use a variable in defining the object key. You can try something like this.
window.MyModel = Backbone.Model.extend({
myDynamicVar : "country",
urlRoot: "api/myModel",
defaults: {
"id": null,
"country": {
"route":"test/test/test",
"class":".usa2",
"txt":"USA 2"
},
"region": "California",
"year": "",
"description": "",
"picture": ""
}
});
window.MyModel.defaults[this.myDynamicVar] = "USA";

Categories