I am using Ext JS 6, trying to get a grid to have a row for each area, country, and city. I have no control over my backend and the format of my JSON (see below). I think I have my store correct (see below). I am wondering what I need to do in order to display a record for each nested item in the grid. Ext JS is determined to only have one record in this grid. My real question is what should my model look like?
JSON
{
"locations" : [
{
"type" : "area",
"name" : "Middle East",
"country" : [
{
"type" : "country",
"name" : "Afghanistan",
"city": [
{
"type" : "city",
"name" : "Bagram",
"data" : [
{
"data1" : 5,
"data2" : 10
},
{
"data1" : 2,
"data2" : 9
}
]
},
{
"type" : "city",
"name" : "Kabul",
"data" : [
{
"data1" : 3,
"data2" : 7
},
{
"data1" : 6,
"data2" : 2
}
]
}
]
}
]
}
]
}
Store
Ext.define('App.store.Locations', {
extend: 'Ext.data.Store',
model: 'App.model.Location',
proxy: {
type: 'ajax',
url: 'data/location.json',
reader: {
type: 'json',
rootProperty: 'locations'
}
},
autoLoad: true
});
I think you have to use Ext.data.reader.Reader.transform(), like this:
Ext.define('App.store.Locations', {
extend: 'Ext.data.Store',
model: 'App.model.Location',
proxy: {
type: 'ajax',
url: 'data/location.json',
reader: {
type: 'json',
rootProperty: 'locations',
transform: {
fn: function(rawData) {
//Your parse data logic here
var data = [],
locationTypes = ['area', 'country', 'city'],
parseData = function(dataPart) {
data.push({
type: dataPart['type'],
name: dataPart['name']
});
for(var i = 0; i < locationTypes.length; ++i) {
if(Object.prototype.hasOwnProperty.call(dataPart, locationTypes[i])) {
for(var j = 0; j < dataPart[locationTypes[i]].length; ++j) {
parseData(dataPart[locationTypes[i]][j]);
}
}
}
};
for(var i = 0; i < rawData.length; ++i) {
parseData(rawData['locations'][i]);
}
return (data);
}
}
}
},
autoLoad: true
});
Check this working fiddle
Related
I created function which is adding people_id inside array of given card. But there is problem that inserted id is always not as an objectId() where I just need it to be saved as objectId.
When id is added to array i I'm sending whole variable board JSON to nodejs API where is executed function findOneAndUpdate. And here is problem because after saved this arrays is not objectID in Author. Can someone tell me how to make it?
JSON board
{
"_id" : ObjectId("59e096a7622a3825ac24f343"),
"name" : "1",
"users" : [
ObjectId("59cd114cea98d9326ca1c421")
],
"lists" : [
{
"list" : "1",
"cards" : [
{
"name" : "2",
"Author" : [
"59df60fb6fad6224f4f9f22d",
"59df60fb6fad6224f4f9f22d",
"59df60fb6fad6224f4f9f22e"
]
},
{
"name" : "3",
"Author" : []
}
]
},
{
"list" : "fea",
"cards" : [
{
"name" : "card",
"Author" : []
}
]
}
],
"__v" : 0 }
Router:
router.post('/add/member', function (req, res, next) {
console.log(req.body)
Board.findOneAndUpdate({ _id: req.body._id },
{
$set: {
lists : req.body.lists
}
},
{
upsert: true
},
((cards) => {
res.send(cards)
})
)
});
model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BoardSchema = new Schema({
name: { type: String, maxlength: 20 },
lists : { type: Array },
users : [{ type : Schema.Types.ObjectId, ref: 'User' }],
});
module.exports = mongoose.model('Board', BoardSchema);
And here is function with adding poeple
$scope.addMemberToCard = (indexList, indexCard, member) => {
$scope.board.lists[indexList].cards[indexCard].Author.push(member);
console.log( $scope.board.lists[indexList].cards[indexCard].Author)
return ApiService.staff.addMemberToCard($scope.board).then(function () {
})
}
You could use the mongoose Types ObjectID method and then transform the string sent into an ObjectID.
const id = new new mongoose.Types.ObjectId(Author);
I have successfully set up several KendoUI Grids, but I cannot get one using server-side paging to work.
I modified my rest service so I will return a total value (hard coded right now).
I also modified my javascript source. [see below].
Usually I just get a blank screen.
Would be very grateful for any assistance.
Script:
$(document).ready(function(){
// Setup Rest Service
var loc = ( location.href );
var url = loc.substring( 0, loc.lastIndexOf( "/" ) ) + "/xpRest.xsp/test/";
dataSource = new kendo.data.DataSource({
pageSize: 20,
serverPaging: true,
serverFiltering: true,
serverSorting: true, transport : {
read : {
url : url + "READ",
dataType : "json"
},
type : "READ"
},
schema : {
total: "total",
model : {
id : "unid",
fields : {
unid : {
type : "string",
nullable : false
},
tckNbr : {
type : "string",
editable : false
},
tckSts : {
type : "string",
editable : false
}
}
}
}
});
grid = $("#grid-databound-dataItem").kendoGrid({
dataSource : dataSource,
height: 550,
filterable: true,
sortable: true,
pageable: true,
columns : [
{field : "tckNbr", title : "Number", type: "string"},
{field : "tckSts", title : "Status", type: "string"}
]
}).data("kendoGrid");
});
JSON feed:
[
{
"total":100,
"data":
[
{
"tckNbr":"3031",
"tckSts":"1 Not Assigned",
"unid":"0014DA9095BF6D638625810700597A36",
"tckReqs":"Bryan S Schmiedeler",
"tckNts":
[
"Bryan DeBaun"
],
"tckBUs":
[
"NAP\/IFI"
],
"tckApps":"GTM",
"tckType":"Issue",
"tckPriority":"Medium"
},
{
"tckNbr":"3031",
"tckSts":"1 Not Assigned",
"unid":"00598976D88226D2862581070059AD25",
"tckReqs":"Bryan S Schmiedeler",
"tckNts":
[
"Bryan DeBaun"
],
"tckBUs":
[
"NAP\/IFI"
],
"tckApps":"GTM",
"tckType":"Issue",
"tckPriority":"Medium"
}
]
}
]
Correct your JSON feed, you need to return object not array:
{
"total": 10,
"data": []
}
After that say what is data and what is total in you schema:
schema : {
total: "total",
data: "data",
.
.
}
Note: if you mock data like in your case (total: 100, data -> size is 2) your paginatio will be created by total parameter not data itself. You will see 5 pages with same data (that is ok).
My database has data in the following format :
{ "_id" : ObjectId( "abcd" ),
"coordinate" : [somevalue, somevalue],
"value" : [
{ "time" : 1,
"characteristics" : "pqrs" },
{ "time" : 10,
"characteristics" : "pqrs" } ] }
I want to find the field closest coordinate and a time that is less than or equal to a given value.
Currently I'm using this query :
db.collection.aggregate({
coordinate: {
$geoNear: [latitude, longitude],
$maxDistance: 10
},
"value.time": {
$lte: 5
}
})
This one returned the entire entry, but what I wanted is the field:
{ "time" : 1, "characteristics" : "pqrs" }
Is it even possible to just return this field ? What if there are multiple result and I just want the one that's closest to my value.time input ?
You can perform an aggregation to :
$match items with specified coordinate and value.time
$filter value array to remove everything < 5
$unwind value array
$group by max value
Query is :
db.collection.aggregate({
$match: {
coordinate: {
$geoNear: [0, 0],
$maxDistance: 10
},
"value.time": {
$lte: 5
}
}
}, {
$project: {
value: {
$filter: {
input: "$value",
as: "value",
cond: { $lte: ["$$value.time", 5] }
}
}
}
}, {
$unwind: "$value"
}, {
$group: {
_id: "$_id",
time: { $max: "$value.time" },
characteristics: { $first: "$value.characteristics" }
}
})
Sample output :
{ "_id" : ObjectId("588a8c3080a14de2d654eb7b"), "time" : 4, "characteristics" : "pqrs" }
{ "_id" : ObjectId("588a89327fe89686fd2210b2"), "time" : 1, "characteristics" : "pqrs" }
I want to read json data from file - content of the json file shown below
{
"form": {
"fields" : [
{
"field":"textfield",
"name": "username",
"constrain": "5-10",
"value": ""
},
{
"field":"textfield",
"name": "password",
"constrain": "5-10",
"value": ""
},
{
"field":"datepickerfield",
"name": "Birthday",
"constrain": "5-10",
"value": "new Date()"
},
{
"field":"selectfield",
"name": "Select one",
"options":[
{"text": "First Option", "value": 'first'},
{"text": "Second Option", "value": 'second'},
{"text": "Third Option", "value": 'third'}
]
},
]
}
}
Model
Ext.define('dynamicForm.model.Form', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'field', type: 'string'},
{name: 'name', type: 'string'},
{name: 'constrain', type: 'string'},
{name: 'value', type: 'string'}
],
hasMany: {model: 'dynamicForm.model.SelectOption', name: 'options'}
}
});
Ext.define('dynamicForm.model.SelectOption', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'text', type: 'string'},
{name: 'value', type: 'string'}
]
}
});
store
Ext.define('dynamicForm.store.FormStore', {
extend : 'Ext.data.Store',
storeId: 'formStore',
config : {
model : 'dynamicForm.model.Form',
proxy : {
type : 'ajax',
url : 'form.json',
reader : {
type : 'json',
rootProperty : 'form.fields'
}
},
autoLoad: true
}
});
This what i tried so for.
var fromval = Ext.create('dynamicForm.store.FormStore');
fromval.load(function (){
console.log(fromval);
// i added register view which having form panel with id "testForm"
Ext.Viewport.add({
xtype : 'register'
});
for(i=0; i< fromval.getCount(); i++) {
console.log("------");
Ext.getCmp('testForm').add({
xtype: fromval.getAt(i).data.field,
label: fromval.getAt(i).data.name,
value: fromval.getAt(i).data.value,
options: [
{text: "First Option", value: "first"},
{text: "Second Option", value: "second"},
{text: "Third Option", value: "third"}
]
});
}
});
two text fileds and date are woking good, but i don't know how to get options for select field from store, just heard coded now.
over all Based on the above json data, i need to create sencha form dynamically.
Better to follow MVC structure:
Create a model:
Ext.define('MyApp.model.FormModel', {
extend: 'Ext.data.Model',
config: {
fields: ["field","name"]
}
});
A store with proxy:
Ext.define('MyApp.store.FormStore',{
extend: 'Ext.data.Store',
config:
{
model: 'MyApp.model.FormModel',
autoLoad:true,
proxy:
{
type: 'ajax',
url : 'FormData.json', //Your file containing json data
reader:
{
rootProperty:'form.fields'
}
}
}
});
The formData.json file:
{
"form": {
"fields" : [
{
"field":"textfield",
"name": "username"
},
{
"field":"textfield",
"name": "password"
},
]
}
}
And then use the FormStore to fill the form data as you need.
Ext.define('MyApp.view.LoginPage', {
extend: 'Ext.form.Panel',
config: {
items:{
xtype:'fieldset',
layout:'vbox',
items:[{
flex:1,
xtype:'textfield',
id:'namefield',
placeHolder:'Username'
},{
flex:1,
xtype:'passwordfield',
id:'passwordfield',
placeHolder:'Password'
}]
},
listeners:{
painted:function()
{
var store=Ext.getStore('FormStore');
while(!store.isLoaded())
{
console.log("loading...");
}
var record=store.getAt(0);
Ext.getCmp('namefield').setValue(record.data.name);
Ext.getCmp('passwordfield').setValue(record.data.password);
}
}
}
});
{
"data":[
{
"xtype":"textfield",
"title":"UserName",
"name": "username"
},
{
"xtype":"textfield",
"title":"password",
"name": "password"
},
{
"xtype":"textfield",
"title":"phone no",
"name": "birthday"
},
{
"xtype":"textarea",
"title":"address",
"name": "address"
}
]
}
Ext.define('dynamicForm.model.FormModel', {
extend: 'Ext.data.Model',
fields: ['field', 'name']
});
Ext.define('dynamicForm.store.FormStore', {
extend : 'Ext.data.Store',
model : 'dynamicForm.model.FormModel',
proxy :
{
type : 'ajax',
url : 'data/user.json',
reader :
{
type : 'json',
rootProperty:'data'
},
autoLoad: true
}
});
Ext.define('dynamicForm.view.DynaForm',{
extend:'Ext.form.Panel',
alias:'widget.df1',
items:[]
});
Ext.application({
name:'dynamicForm',
appFolder:'app',
controllers:['Users'],
launch:function(){
Ext.create('Ext.container.Viewport',{
items:[
{
xtype:'df1',
items:[]
}
]
});
}
});
Ext.define('dynamicForm.controller.Users',{
extend:'Ext.app.Controller',
views:['DynaForm'],
models:['FormModel'],
stores:['FormStore'],
refs:[
{
ref:'form1',
selector:'df1'
}
],
init:function(){
console.log('in init');
this.control({
'viewport > panel': {
render: this.onPanelRendered
}
});
},
onPanelRendered: function() {
var fromval=this.getFormStoreStore();
var form=this.getForm1();
fromval.load({
scope: this,
callback: function(records ,operation, success) {
Ext.each(records, function(rec) {
var json= Ext.encode(rec.raw);
var response=Ext.JSON.decode(json);
for (var i = 0; i < response.data.length; i++) {
form.add({
xtype: response.data[i].xtype,
fieldLabel: response.data[i].title,
name: response.data[i].name
});
}
//form.add(Ext.JSON.decode(json).data);
form.doLayout();
});
}
});
}
});
It will be done automatically if you insert it into any extjs component content :
var jsonValues = "{
"form": {
"fields" : [
{
"field":"textfield",
"name": "username"
},
{
"field":"textfield",
"name": "password"
},
]
}
}";
var panel = new Ext.Panel({
id : 'myPanel',
items : [jsonValues]
});
panel.show();
Hi I'm trying to load a local JSON file in Sencha Touch 2.
I'm using phonegap 1.4 and iOS 5, testing in VM.
Here is the code:
Ext.define("User", {
extend: 'Ext.data.Model',
config: {
fields: [
'id', 'name'
],
hasMany: {model: 'Order', name: 'orders'},
proxy: {
type: 'ajax',
url : 'users.json',
reader: {
type: 'json',
root: 'users'
}
}
}
});
Ext.define("Order", {
extend: 'Ext.data.Model',
config: {
fields: [
'id', 'total'
],
hasMany : {model: 'OrderItem', name: 'orderItems', associationKey: 'order_items'},
belongsTo: 'User'
}
});
Ext.define("OrderItem", {
extend: 'Ext.data.Model',
config: {
fields: [
'id', 'price', 'quantity', 'order_id', 'product_id'
],
belongsTo: ['Order', {model: 'Product', associationKey: 'product'}]
}
});
Ext.define("Product", {
extend: 'Ext.data.Model',
fields: [
'id', 'name'
],
hasMany: 'OrderItem'
});
var store = Ext.create('Ext.data.Store', {
model: "User"
});
store.load({
callback: function() {
//the user that was loaded
var user = store.first();
//console.log("Orders for " + user.get('name') + ":")
alert(user.get('name'));
//iterate over the Orders for each User
user.orders().each(function(order) {
console.log("Order ID: " + order.getId() + ", which contains items:");
//iterate over the OrderItems for each Order
order.orderItems().each(function(orderItem) {
//we know that the Product data is already loaded, so we can use the synchronous getProduct
//usually, we would use the asynchronous version (see Ext.data.association.BelongsTo)
var product = orderItem.getProduct();
});
});
}
});
The JSON file:
{
"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"
}
}
]
}
]
}
]
}
The alert says 'undefined', any help? Many thanks
In Sencha Touch 2 the reader object should use rootProperty instead of root.