How to change field display order using radDataForm with JSON source - json

Using Vue.js / NativeScript (radDataForm) I want to reorder the display of the fields when using JSON as a source. My current code works, but the display is:
Album Name
Band Name
Owned
Year
Borrowed
Which is not the order in the code.
I'm aware of this:
<df:EntityProperty name="albumName" displayName="Name of Album" index="0" />
but, how do I add index="0"to my JSON object?
Also - I don't know what this does?
str: "",
bool: false
Sample Code:
<template>
<Page class="page">
<ActionBar title="JSON example" class="action-bar" />
<RadDataForm :source="album" />
</Page>
</template>
<script>
import Vue from "nativescript-vue";
import RadDataForm from "nativescript-ui-dataform/vue";
Vue.use(RadDataForm);
export default {
data() {
return {
album: {
bandName: "Beatles",
albumName: "Seargent Peppers",
year: "2017",
owned: true,
borrowed: true
},
str: "",
bool: false
};
}
};
</script>
iPhone Screenshot:

I don't think you could control index from source but you can do that using metadata.
Example
<template>
<Page class="page">
<ActionBar title="JSON example" class="action-bar" />
<RadDataForm :source="album" :metadata="metadata" />
</Page>
</template>
<script>
import Vue from "nativescript-vue";
import RadDataForm from "nativescript-ui-dataform/vue";
Vue.use(RadDataForm);
export default {
data() {
return {
album: {
bandName: "Beatles",
albumName: "Seargent Peppers",
year: "2017",
owned: true,
borrowed: true
},
metadata: {
"isReadOnly": false,
"propertyAnnotations":
[
{
"name": "bandName",
"index": 0
},
{
"name": "albumName",
"index": 1
},
{
"name": "year",
"index": 2
},
{
"name": "owned",
"index": 4
},
{
"name": "myRating",
"index": 3
}
]
}
};
}
};
</script>

Related

Importing a .json file local to create a treemap within vue js

I am trying to import a .json file into my vue js project and then create a treemap based on the data.
my .json file looks like this
{
"warehouses":[
{
"id":4,
"name":"Goimek",
"itemCount":354,
"idParent":0,
"nameParent":"",
"locationInfo":[
{
"id":19,
"name":"Puerta 4",
"itemCount":354
}],
"warehouseChildrenInfo":[]
},
{
"id":5,
"name":"Karpa",
"itemCount":167,
"idParent":0,
"nameParent":"",
"locationInfo":[
{
"id":24,
"name":"Karpa",
"itemCount":167
}],
"warehouseChildrenInfo":[]
},
{
"id":6,
"name":"Wec",
"itemCount":145,
"idParent":0,
"nameParent":"",
"locationInfo":[
{
"id":25,
"name":"WEC",
"itemCount":115
}],
"warehouseChildrenInfo":[
{
"id":1009,
"name":"BIGUMETRIK",
"itemCount":30,
"idParent":0,
"nameParent":"",
"locationInfo":[
{
"id":1015,
"name":"BIGUMETRIK",
"itemCount":30
}],
"warehouseChildrenInfo":[]
}]
},
{
"id":1037,
"name":"PROVEEDOR",
"itemCount":10,
"idParent":0,
"nameParent":"",
"locationInfo":[
{
"id":1553,
"name":"PROVEEDOR Lehenetsitako kokapena",
"itemCount":0
},
{
"id":1554,
"name":"PIKUMEK S.L",
"itemCount":0
},
{
"id":1555,
"name":"TENKOR S.L",
"itemCount":10
},
{
"id":1556,
"name":"ZUMELTXU S.L",
"itemCount":0
}],
"warehouseChildrenInfo":[]
}
]
}
and this is how im importing it
import warehouses from './warehouses.json'
export default {
name: 'app',
data () {
return {
title: 'My first tree map',
warehouses : warehouses
}
}
}
I then just try and display the data by doing this in a div
<div v-for="(warehouse,index) in warehouses" :key ="index" >{{warehouse.id }}</div>
but it doesn't display anything. What am I doing wrong here?
You can use your JSON from a js file in vue. Here is a sandbox demo for your JSON.

extract subset of imported json file in vue's data() function

I have imported the following json file:
[
{
"case_id": "1234",
"thread": [
{
"t_id": "1111",
"text": "test"
},
{
"t_id": "2222",
"text": "test"
}
]
},
{
"case_id": "5678",
"thread": [
{
"t_id": "9999",
"text": "test"
},
{
"t_id": "8888",
"text": "test"
},
{
"t_id": "777",
"text": "test"
}
]
}
]
using the following:
import cases from '../cases.json'
The whole json dataset is available in cases variable and can be used in the template with the support of v-if and v-for.
How can I create a separate dataset (thecase) that contains only threads for a given case_id? In the template I would only like to use v-for to display all threads for a given case_id.
Below is my export default section:
export default {
name: "details",
props: {
case_id: {
required: true,
type: String
}
},
data () {
return {
cases,
thecase: ### THIS IS THE PART I CANNOT FIGURE OUT ###
}
}
};
You can remove thecase from data options and use a computed property instead for thecase. Inside the computed property, we will need to use array .find() method to find the case where case_id is same as the case_id passed in the prop:
data: {
cases,
},
computed: {
thecase: function() {
return this.cases.find(c => c.case_id === (this.case_id || ''))
}
}
and then you can use v-for on thecase.thread just like you would do for a data option like:
<li v-for="item in thecase.thread" :key="item.t_id">
{{ item.text }}
</li>
You can further modify it and use v-if & v-else to show a text like No cases were found with give case id in case there is no match found.

Check a recursive JSON structure for matchinig numerical values in Set in Typescript Angular

I have a UI where initially a User has to check some checkboxes. The checkboxes have sequential IDs. The JSON Structure for it is as follows:
{
"categories": [{
"name": "Product",
"labels": [{
"id": 1,
"name": "I work on an asset (capital good).",
"checked": false
}, {
"id": 2,
"name": "I work on a consumer product.",
"checked": false
}, {
"id": 3,
"name": "I am not sure what type of product I work on.",
"checked": false
}
]
}, {
"name": "Goal",
"labels": [{
"id": 4,
"name": "I want to improve the product's reliability.",
"checked": false
}, {
"id": 5,
"name": "I need information to identify root causes.",
"checked": false
}, {
"id": 6,
"name": "I need information about the product's environment.",
"checked": false
}, {
"id": 7,
"name": "I need information about customer requirements.",
"checked": false
}, {
"id": 8,
"name": "I need quantified information.",
"checked": false
}, {
"id": 9,
"name": "I am not sure what I need.",
"checked": false
}
]
}
]
}
I render it Angular using the following Code:
component.html
<div class="row mt-lg-auto" *ngFor="let filter of filters['categories']">
<div class="col-lg-12">
<h4>
{{filter['name']}}
</h4>
<div *ngFor="let label of filter['labels']">
<div class="form-check">
<input class="form-check-input"
type="checkbox"
value="{{label['id']}}"
id="{{label['id']}}"
[(ngModel)]="label['checked']"
(change)="changeCheck(label['id'], $event)"
>
<label class="form-check-label" for="{{label['id']}}">
{{label['name']}}
</label>
</div>
</div>
</div>
</div>
component.ts
I directly import the JSON file from src/assets/ folder and save the id to a Set in order to avoid duplicate values when the user selects a checkbox.
import { Component, OnInit } from '#angular/core';
import * as FilterFunc from 'src/assets/FilterFunction.json';
const Filters: any = FilterFunc;
#Component({
selector: 'explore-step1',
templateUrl: './explore-step1.component.html',
styleUrls: ['./explore-step1.component.css']
})
export class ExploreStep1Component implements OnInit {
filters = Filters.default;
selections = new Set();
constructor() {
}
ngOnInit(): void {
}
changeCheck(id: number, event: any) {
(event.target.checked) ?
this.selections.add(id):
this.selections.delete(id);
console.log(this.selections);
}
}
I am using ngx-treeview to render a tree view for a fixed JSON file that has the following structure:
GitHub Gist of the Complete Recursive JSON
Here on the children in the most depth have the following key-value pair:
"value": {
"label_ids": [relevant ids from the first json],
"description": "some text to render"
}
else the "value" is null.
I wish to compare the Set values to the above mentioned recursive JSON's label_ids and if one or more than one values in the label_ids match with the Set then change the checked value to true
How does one accomplish this in Typescript/Angular?
I solved it by creating a Recursion Parsing Function which takes in the JSON Structure.
Within the ngOnInit() I call the service and pass it to the parseTree function
I recursively parse it and compare the values with the Set
I add additional information like a Font-Awesome class text within the value structure to render it
pass the updated JSON to the respective items variable
component.ts:
parseTree(factors: TreeviewItem[]) {
factors.forEach(factor => {
// console.log(factor);
if (!isNil(factor.value)) {
let labels: number[] = factor.value['label_ids'];
labels.forEach(label => {
if(this.selected.findIndex(l => l == label) > -1) {
factor.value['highlighted'] = true;
factor.value['class'] = 'fas fa-flag';
}
});
}
if (!isNil(factor.children)) {
this.parseTree(factor.children);
}
});
this.items = factors;
}
here selections is a Set and within ngOnInit() I set it a fixed value:
ngOnInit() {
this.selections.add(15);
this.items = this.parseTree(this.service.getDataQualityFactors());
}
Based on ngx-treeview example I use the itemTemplate in the code and add the Font-Awesome fonts next to the selections as follows:
component.html
<label class="form-check-label" *ngIf="item.value !== null && item.value['highlighted']">
<i class="{{item.value['class']}}" aria-hidden="true" title="Highlighted" [ngClass]="{'marked': item.checked}"></i>
</label>
and use the CSS classes to manipulate the color change of the font:
.fa-flag {
color: red;
}
.fa-flag.marked {
color: green;
}
StackBlitz Example Code

Ember Multitple belongTo relationships not working with JSONAPI

I have the following json api document:
{
"data": [
{
"type": "haves",
"id": "2708f443-0857-4ae9-9935-9aa4b4e9f721",
"attributes": {
"quantity": 1
},
"relationships": {
"card": {
"data": {
"type": "cards",
"id": "3be08f31-3361-404c-9977-23535ed837f3"
}
}
}
}
],
"included": [
{
"type": "cards",
"id": "3be08f31-3361-404c-9977-23535ed837f3",
"attributes": {
"name": "Name"
},
"relationships": {
"set": {
"data": {
"type": "sets",
"id": "0fec70de-02e0-4646-bdcf-f86acea90d23"
}
}
}
},
{
"type": "sets",
"id": "0fec70de-02e0-4646-bdcf-f86acea90d23",
"attributes": {
"name": "Name"
}
}
]
}
With the following ember models:
// app/models/have.js
export default DS.Model.extend({
quantity: DS.attr('number'),
minPrice: DS.attr('number'),
account: DS.belongsTo('account'),
card: DS.belongsTo('card')
});
// app/models/set.js
export default DS.Model.extend({
name: DS.attr('string'),
cards: DS.hasMany('card')
});
// app/models/card.js
export default DS.Model.extend({
name: DS.attr('string'),
set: DS.belongsTo('set'),
haves: DS.hasMany('have')
});
And a custom inflector rule:
inflector.irregular('have', 'haves');
When I load the json document with this structure though, I can't seem to do something like have.card.set.name in my template when I iterate this jsonapi document. I'm guessing my jsonapi structure is incorrect. What am I missing? I don't get any errors in my chrome console or in the ember server running. When I load Ember Inspector, I see the set model under Data.

Unable to display data in polymer api

I was using polymer example and they used core-ajax to call the api.I want to display text from the openweathermap api.When i call the api it displays no data.I'm not able to display any data and when i placed console.log(this.post) in the post-list element it gives me undefined.I'm practically a noob when it comes to polymer.
Below is the Api Calling Method
<polymer-element name="post-service" attributes="posts">
<template>
<style>
:host {
display: none;
}
</style>
<core-ajax id="ajax"
auto
url="http://api.openweathermap.org/data/2.5/weather?q=hyderabad"
on-core-response="{{postsLoaded}}"
handleAs="json">
</core-ajax>
</template>
<script>
Polymer('post-service', {
created: function() {
this.posts = [];
},
postsLoaded: function() {
// Make a copy of the loaded data
this.posts = this.$.ajax.response;
},
/**
* Update the service with the current favorite value.
* (Two-way data binding updates the favorite value
* stored locally.) If this was a real service, this
* method would do something useful.
*
* #method setFavorite
* #param uid {Number} Unique ID for post.
* #param isFavorite {Boolean} True if the user marked this post as a favorite.
*/
setFavorite: function(uid, isFavorite) {
// no service backend, just log the change
console.log('Favorite changed: ' + uid + ", now: " + isFavorite);
}
});
</script>
</polymer-element>
This is element is used to display
<polymer-element name="post-list" attributes="show">
<template>
<style>
:host {
display: block;
width: 100%;
}
post-card {
margin-bottom: 30px;
}
</style>
<post-service id="service" posts="{{posts}}">
</post-service>
<div layout vertical center>
<template>
<post-card>
<h2>{{post.weather.main}}</h2>
<p>{{post.weather.description}}</p>
</post-card>
</template>
</div>
</template>
<script>
Polymer({});
console.log(this.post);
</script>
</polymer-element>
Example json
[
{
"uid": 1,
"text" : "Have you heard about the Web Components revolution?",
"username" : "Eric",
"avatar" : "../images/avatar-01.svg",
"favorite": false
},
{
"uid": 2,
"text" : "Loving this Polymer thing.",
"username" : "Rob",
"avatar" : "../images/avatar-02.svg",
"favorite": false
}
]
My api response(json)
{
"coord": {
"lon": 78.47,
"lat": 17.38
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}
],
"base": "cmc stations",
"main": {
"temp": 303.15,
"pressure": 1010,
"humidity": 62,
"temp_min": 303.15,
"temp_max": 303.15
},
"wind": {
"speed": 7.7,
"deg": 280
},
"clouds": {
"all": 40
},
"dt": 1436677800,
"sys": {
"type": 1,
"id": 7830,
"message": 0.0124,
"country": "IN",
"sunrise": 1436660330,
"sunset": 1436707470
},
"id": 1269843,
"name": "Hyderabad",
"cod": 200
}
The core-ajax element sends an event when the data are received. To use them you have to manipulate the fired event.
postsLoaded: function(event, detail) {
// Event contains lot of informations
console.log(event);
// Detail would be the received data
console.log(detail);
// Make a copy of the loaded data
this.posts = detail; // or this.posts = event.detail;
}
It would be easier to see what happens if you add a listener on the posts attributes in your element post-list. See the doc section Observing properties.
<script>
Polymer({
postsChanged: function(oldValue, newValue) {
console.log(newValue);
}
});
</script>
Moreover, I think you have a typo in your code. In the template you are using post and no posts :
<template>
<post-card>
<h2>{{post.weather.main}}</h2>
<p>{{post.weather.description}}</p>
</post-card>
</template>
Finally, if you are starting with Polymer, I suggest you to start with the version 1.0.