How to feed Json response into an Empty ant design table? - json

I am quite new to react and want to feed my json response which i got from an api into an empty ant-design table and I am getting no clue on how to do that?
i have already mapped id with name but also want all other columns and into the table format of ant design.
JSon response
[
{
"id": 9685,
"name": "Mukesh Rai",
"count of sub": 3,
"count of int": 2,
"count of po": null
},
{
"id": 2085,
"name": "Abhyudaya Singh Panwar",
"count of sub": 1049,
"count of int": 17,
"count of po": 5
},
{
"id": 7087,
"name": "Amit Kumar Sharma",
"count of sub": 363,
"count of int": 68,
"count of po": 10
},
{
"id": 2100,
"name": "Aditya Kumar",
"count of sub": 724,
"count of int": 5,
"count of po": 1
}
]
component file
componentDidMount(){
axios.get("http://127.0.0.1:8000/marketer_details/")
.then(response=>{
console.log(response)
this.setState({posts: response.data})
})
.catch(error=> {
console.log(error)
})
}
render() {
const { posts } = this.state
return(
<div>
List of Posts
{
posts.length ?
posts.map(post => <div key = {post.id}>{post.name}</div>) :
null
}
</div>
)
//Wanted all other columns as well
render(){
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
width: '30%',
...this.getColumnSearchProps('name'),
},
{
title: 'Count of Submissions',
dataIndex: 'Count',
key: 'sub',
width: '20%',
...this.getColumnSearchProps('sub'),
},
{
title: 'Count of Interviews',
dataIndex: 'Count',
key: 'int',
...this.getColumnSearchProps('int'),
},
{
title: 'Count of PO',
dataIndex: 'Count',
key: 'po',
...this.getColumnSearchProps('po'),
},
];
return <Table columns={columns} dataSource={this.state.posts} />;
}
}
}
export default App
I am unable to understand how the multiple mapping could be done against each column row.

The Table component takes a column structure as argument.
Here, dataIndex refers to the index in the data that is to be referenced for the column, key is intended to remain unique across column names.
In your case, the columns array could be:
const columns = [
{
name: "Name",
dataIndex: "name"
},
{
name: "Count of submissions",
dataIndex: "count of sub"
},
{
name: "Count of interviews",
dataIndex: "count of int"
},
{
name: "Count of PO",
dataIndex: "count of po"
}
];
Codesandbox Demo

Related

how to display json in table vuejs

how to display the following json data ?
i have json data like this, and want to display it in table, i use vue-bostrapt .
Previously I tried like this, but it's not perfect.
this my json
[
{
"id":"1",
"name": "KINTIL",
"desc": "Kintil is good",
"location": [
{
"prov": "Jawa Barat",
"city": "Bandung"
},
{
"prov": "Banten",
"city": "Tanggerang"
}
],
"applied": [
{
"item_name": "galian"
},
{
"item_name": "timbunan"
}
],
"exception": [
{
"ex_name": "F001",
"ex_value": "001001"
},
{
"ex_name": "M001",
"ex_value": "002002"
}
]
}
]
and this html
<b-table class="table spacing-table" style="font-size: 13px;" show-empty
:items="inovasi" :fields="fields" :current-page="currentPage" :per-page="0" :filter="filter" >
</b-table>
and this my script
import json from "../static/data.json";
export default {
name: 'tes',
data() {
return {
inovasi:[],
filter: null,
fields: [
{
key: 'id',
label: 'id',
sortable: true
},
{
key: 'name',
label: 'name',
sortable: true
},
{
key: 'location',
label: 'location',
sortable: true
},
{
key: 'applied',
label: 'applied',
sortable: true
},
{ key: 'actions', label: 'Doc' }
],
currentPage: 0,
perPage: 5,
totalItems: 0
}
},
mounted() {
this.inovasi = json;
},
computed:{
},
methods: {
}
}
this result
how to display location and applied , into a single row table ?
thanks in advance for those who have answered :)
thanks
You can do it using formatter like
fields: [
{
key: 'id',
label: 'id',
sortable: true
},
{
key: 'name',
label: 'name',
sortable: true
},
{
key: 'location',
label: 'location',
sortable: true,
formatter: (value, key, item) => {
return value.map(x => 'prov: ' + x.prov + ' city:' + x.city).join(", ")
}
},
{
key: 'applied',
label: 'applied',
sortable: true,
formatter: (value, key, item) => {
return value.map(x => x.item_name).join(", ")
}
},
{ key: 'actions', label: 'Doc' }
],
It will show for the location column this: prov: Jawa Barat city:Bandung, prov: Banten city:Tanggerang and for the applied column this: galian, timbunan

Extjs load combo boxes dynamically using recursive json data

I have set of models and a store as given below.
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
'id', 'name', 'total'],
hasMany: {
model: 'Order',
name: 'orders'
},
proxy: {
type: 'rest',
url: 'users.json',
reader: {
type: 'json',
root: 'users'
}
}
});
Ext.define('Order', {
extend: 'Ext.data.Model',
fields: [
'id', 'total'],
hasMany: {
model: 'OrderItem',
name: 'orderItems',
associationKey: 'order_items'
},
belongsTo: 'User'
});
Ext.define('OrderItem', {
extend: 'Ext.data.Model',
fields: [
'id', 'price', 'quantity', 'order_id', 'product_id'],
belongsTo: ['Order', {model: 'Product', associationKey: 'product'}]
});
var store = Ext.create('Ext.data.Store', {
model: 'User'
});
And below is the json file which I use to load data.
{
"users": [
{
"id": "123",
"name": "Ed",
"orders": [
{
"id": "50",
"total": "100",
"order_items": [
{
"id" : "20",
"price" : "40",
"quantity": "2",
"product" : {
"id": "1000",
"name": "MacBook Pro"
}
},
{
"id" : "21",
"price" : "20",
"quantity": "3",
"product" : {
"id": "1001",
"name": "iPhone"
}
}
]
}
]
},
{
"id": "124",
"name": "Nisha",
"orders": [
{
"id": "52",
"total": "1004",
"order_items": [
{
"id" : "22",
"price" : "40",
"quantity": "23",
"product" : {
"id": "1002",
"name": "Nokia"
}
},
{
"id" : "23",
"price" : "100",
"quantity": "3",
"product" : {
"id": "1003",
"name": "apple"
}
}
]
}
]
}
]
}
I am loading the user IDs to L1_combo_box as below and according to the user ID the user selects from the L1_combo_box, I need to load order_item ids to L2_combo_box .
For example, I load user ids 123, 124 to L1_combo_box and when user selects 123 from L1 combo box, I need to load 20,21 to L2 combo box. If user selects 124, then I need to load 22,23.
Below is the partially completed code. can anyone help me to complete this?
var searchFormFieldsetItems = [
{
xtype: 'fieldcontainer',
combineErrors: true,
name: 'search_form_fieldset_items',
msgTarget: 'side',
fieldLabel: '',
defaults: {
hideLabel: true
},
items: [{
xtype: 'combo',
name: 'L1_combo_box',
displayField: 'id',
valueField: 'id',
queryMode: 'remote',
store:store,
listeners: {
change: {
fn: function(combo, value) {
var store1 = 'users/orders/order_items/';//This line is partially completed
L2_combo_box.bindStore(store1);
}
}
}
},{
xtype: 'combo',
name: 'L2_combo_box',
displayField: 'id',
valueField: 'id'
}
]
}
];
For this you need to use select for combobox and inside of select event you need to use loadData() method of store to adding data in second combo.
In this FIDDLE, I have created a demo using your code and put my efforts for showing data in second combo. I hope this will help/guide you to achieve your requirement.
CODE SNIPPET
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.define('User', {
extend: 'Ext.data.Store',
autoLoad: true,
alias: 'store.user',
fields: ["id", "name", "orders"],
proxy: {
type: 'ajax',
url: 'users.json',
reader: {
type: 'json',
rootProperty: 'users'
}
}
});
Ext.define('Order', {
extend: 'Ext.data.Store',
alias: 'store.order',
field: ["id", "price", "quantity", "product"],
storeId: 'order'
});
Ext.create('Ext.form.Panel', {
title: 'Example Combo',
bodyPadding: 5,
defaults: {
width: 250
},
// The fields
defaultType: 'combo',
items: [{
name: 'L1_combo_box',
displayField: 'id',
valueField: 'id',
queryMode: 'local',
emptyText: 'Select user',
store: {
type: 'user'
},
listeners: {
select: function (combo, rec) {
var L2_combo_box = combo.up('form').getForm().findField('L2_combo_box'),
order = rec.get('orders') || [],
data = [];
//reset combo value
L2_combo_box.reset();
//If order have multipe data then need use forEach for all data
order.forEach(item => {
data = data.concat(item.order_items);
});
//load data in combo store
Ext.getStore('order').loadData(data);
}
}
}, {
emptyText: 'Select order items',
name: 'L2_combo_box',
displayField: 'id',
valueField: 'id',
queryMode: 'local',
store: {
type: 'order'
}
}],
renderTo: Ext.getBody()
});
}
});

ReactJS filter complex Json

[enter image description here][1]Please help me as I am struggling to get something to work. I have a JSON that represents a tree view in my app.
{
name: 'Global', toggled: true, children: [
{
name: 'Region A', nodeid: 0,chosen: true,pushed: false,
children: [
{ name: 'Town A', nodeid: 1, chosen: true, pushed: false,
children: [{
name : "child 1", pushed: false, chosen:false
}]
},
{ name: 'Town B', nodeid: 2, chosen: false, pushed: false,
children: [{
name : "child 2", pushed: false, chosen: false
}]
}
]
}
]
};
What I want to do is traverse through my JSON and only return the entries which have the chosen property as true.
I tried many things without success so far, would you guys be able to help me?
https://i.stack.imgur.com/r61MO.jpg
onPush(e){
var chosennodes = filters.filterTreeChosen(this.state.data);
this.setState({selectednodes: chosennodes});
}
Then the filter itself:
[Object.filter = (obj, predicate) =>
Object.keys(obj)
.filter( key => predicate(obj\[key\]) )
.reduce( (res, key) => (res\[key\] = obj\[key\], res), {} );
export const isChosen = (chosen) =>{
return chosen == true;
}
export const filterTreeChosen = (nodes) => {
var filtered = Object.filter(nodes, node => node.chosen == true);
console.log(filtered);
};][1]
https://i.stack.imgur.com/IAXZ7.jpg
Check this out. I think it should apply to your task.
const tree = {
name: 'Global', toggled: true, children: [
{
name: 'Region A', nodeid: 0,chosen: true,pushed: false,
children: [
{ name: 'Town A', nodeid: 1, chosen: true, pushed: false,
children: [{
name : "child 1", pushed: false, chosen:false
}]
},
{ name: 'Town B', nodeid: 2, chosen: false, pushed: false,
children: [{
name : "child 2", pushed: false, chosen: false
}]
}
]
}
]
};
function getChosenNodes (nodes) {
let result = [];
nodes.forEach(node => {
if (node.chosen) {
result = result.concat([node.nodeid]);
}
if (node.children) {
result = result.concat(getChosenNodes(node.children));
}
})
return result;
}
console.log(getChosenNodes([tree]))
I've returned only nodeid but you can change it as you need.

Sort a store data based on inner JSON field sencha

i have a json in the following format:
{
"collection": [
{
"id": 4,
"tickets": [
{
"price": 40,
},
{
"price": 50,
}
],
},
{
"id": 1,
"tickets": [
{
"price": 10,
},
{
"price": 15,
}
]
},
]
}
STORE:
Ext.define("myProject.store.ABCs", {
extend: "Ext.data.Store",
config: {
model: "myProject.model.ABC",
autoLoad: false,
proxy: {
type: "ajax",
url: '', //myURL
reader: {
type: "json",
rootProperty: "collection", // this is the first collection
},
},
}
});
For this particular JSON i created the models as:
Ext.define("myProject.model.ABC", {
extend: "Ext.data.Model",
config: {
idProperty: "id",
fields:[
{name: "id", type: "int" },
],
hasMany: [
{
model: "myProject.model.XYZ",
name: "tickets",
associationKey: "tickets",
},
],
}
});
And second model as:
Ext.define("myProject.model.XYZ", {
extend: "Ext.data.Model",
config: {
// idProperty: "id",
fields:[
{name: "inner_id", type: "int" },
],
belongsTo: 'myProject.model.ABC'
}
});
This particular code creates a store and populates the models correctly.
var store = Ext.getStore('ABCs');
Now i want to sort this store based on store.tickets().getAt(0).get('price') that is sort the ABC records based on XYZ's first price property.
In the above json. ABC Records will be: [{id:4}, {id:1}]
But since first price in XYZ (40 > 10), i want to sort them and create [{id:1}, {id:4}]
Take a look at the Ext.util.Sorter class, where you can set a sorterFn. See the example at the top of the page - you should be able to simply write the logic for sorting records the way you describe.

JQuery Grid-SubGrid for Parent-Child relation

I need some idea, about how to implement a subgrid in the following sceaniro.
The following is the json data that I want to display in the JQuery Grid and Subgrid.
Basically I am getting an object called "Contact" that has a collection called "actionSet".
{
"total" : "10",
"page" : "1",
"records" : "78",
"rows" : [ {
"comment" : null,
"givenName" : "Contact A",
"familyName" : "A",
"actionSet" : [ {
"actionID" : 1,
"actionDueDate" : "2012-12-08",
"actionNote" : "Action 1"
}, {
"actionID" : 2,
"actionDueDate" : "2012-12-08",
"actionNote" : "Action 2"
} ]
} ...]
}
I want eache Grid row to display the "Contact" and the subgris associated with the grid should display "actionSet" collection.
When a particular row in the Grid is selected, I do not want to make a server call to get the associated actions, as they are allready present in the "actionSet".
I have got the Grid working, displaying the "Contacts" nicely, but I get confused while implement the subgrid, as how to get the data for it, as its allready available in json.
jq(function() {
jq("#grid").jqGrid({
url:'/smallworks/project/getall.do',
datatype: 'json',
mtype: 'GET',
colNames:['Id', 'First Name', 'Last Name'],
colModel:[
{name:'id',index:'id', width:55,editable:false,editoptions: {readonly:true,size:10},hidden:true},
{name:'givenName',index:'givenName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
{name:'familyName',index:'familyName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}}
],
postData: {
},
rowNum:20,
rowList:[20,40,60],
height: 200,
autowidth: true,
rownumbers: true,
pager: '#pager',
sortname: 'id',
viewrecords: true,
sortorder: "asc",
caption:"Contacts",
emptyrecords: "Empty records",
loadonce: false,
loadComplete: function() {
},
Is this achievable?
Do I need to parse JSON data specially for the subgrid?
How can this be achieved?
I suggest that you save information from actionSet in an object which you can easy access later. For example you can use userData parameter and fill the userdata part of JSON data inside of beforeProcessing. The create subgrid you can follow the answer or another one.
The demo demonstrate the implementation approach:
It uses the following code
var mainGridPrefix = "s_";
$("#grid").jqGrid({
url: "Adofo.json",
datatype: "json",
colNames: ["First Name", "Last Name"],
colModel: [
{ name: "givenName" },
{ name: "familyName" }
],
cmTemplate: {width: 100, editable: true, editrules: {required: true},
editoptions: {size: 10}},
rowNum: 20,
rowList: [20, 40, 60],
pager: "#pager",
gridview: true,
caption: "Contacts",
rownumbers: true,
autoencode: true,
height: "100%",
idPrefix: mainGridPrefix,
subGrid: true,
jsonReader: { repeatitems: false },
beforeProcessing: function (data) {
var rows = data.rows, l = rows.length, i, item, subgrids = {};
for (i = 0; i < l; i++) {
item = rows[i];
if (item.actionSet) {
subgrids[item.id] = item.actionSet;
}
}
data.userdata = subgrids;
},
subGridRowExpanded: function (subgridDivId, rowId) {
var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
pureRowId = $.jgrid.stripPref(mainGridPrefix, rowId),
subgrids = $(this).jqGrid("getGridParam", "userData");
$subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId));
$subgrid.jqGrid({
datatype: "local",
data: subgrids[pureRowId],
colNames: ["Due Date", "Note"],
colModel: [
{ name: "actionDueDate", formatter: "date", sorttype: "date" },
{ name: "actionNote" }
],
sortname: "actionDueDate",
height: "100%",
rowNum: 10000,
autoencode: true,
autowidth: true,
jsonReader: { repeatitems: false, id: "actionID" },
gridview: true,
idPrefix: rowId + "_"
});
}
});
UPDATED: The JSON data used in the demo one can see below. I added id property which is required for jqGrid. I used actionID as the id of the subgrids:
{
"total": "10",
"page": "1",
"records": "78",
"rows": [
{
"id": 10,
"comment": null,
"givenName": "Contact A",
"familyName": "A",
"actionSet": [
{
"actionID": 1,
"actionDueDate": "2012-12-08",
"actionNote": "Action 1"
},
{
"actionID": 2,
"actionDueDate": "2012-12-09",
"actionNote": "Action 2"
}
]
},
{
"id": 20,
"comment": null,
"givenName": "Contact B",
"familyName": "B",
"actionSet": [
{
"actionID": 3,
"actionDueDate": "2012-12-11",
"actionNote": "Action 3"
},
{
"actionID": 4,
"actionDueDate": "2012-12-10",
"actionNote": "Action 4"
}
]
}
]
}