I use ExtJS version 4.0.7. I have a data store with fields: recordName and recordDate. What I want to do is to sort these records by date, however recordDate format is: d/m/y. After sorting the records, I need to bind a grid panel with these records. At this point user still wants to see recordDate in d/m/y format.
As a summary, I have a recordDate field in a datastore with format d/m/y (because user wants to see it in that format in the grid panel), but to sort by date I need to reverse to format as y/m/d without changing the format in the grid panel.
Example:
recordDate in the datastore: 27/08/2012 (and this is what user want to see at the grid panel)
date format to sort by date: 2012/08/27
As #Evan mentioned:
If it's a date object, it's not relevant. If you're storing it as a string, then it's not date sorting, it's string sorting.
You need to set your model in a similar fashion:
Ext.define('MyModel', {
extend : 'Ext.data.Model',
fields : [{
name : 'recordDate',
type : 'date',
dateFormat : 'd/m/Y'
},{
name: 'recordName,
type: 'string'
}]
});
Since recordDate will now be a date object, you can use a datecolumn in your grid
columns:[{
text : 'Date',
dataIndex : 'recordDate',
xtype: 'datecolumn',
format:'d/m/Y'
},{
text : 'Name',
dataIndex : 'recordName'
}]
Related
I'm making a request to retrieve data from posts with the API, in my listTodo request I want to add sort the data to filter the data according to the date of their creation to put them in an order, but since it's the backend aws-amplify by default, I don't know how to do that.
export const listTodos = /* GraphQL */ `
query ListTodos(
$filter: ModelTodoFilterInput
$limit: Int
$nextToken: String
) {
listTodos(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
content
email
coverImage
images
page
language
dateAndLocation
createdAt
updatedAt
}
nextToken
}
}
`;
I already try:
add in setting sortDirection: ASC or sortDirection: DESC
add in setting queryField: "todosByDate", sortKeyFields: ["createdAt"]
lots of other try
Can anyone tell me how to sort the data in this query, sort by creation date ?
here is a few possibilities you can explore:
Global secondary index's:
One option is to create an index (global secondary index in dynamodb) on the part that you want to filter. To do that you have to update your graphql model. The model below will give you a query where you can retrieve all todos based on a language, and sort the data based on the createdAt field.
type Todo #model {
id: ID!)
title: String
content: String
email: String
coverImage: String
images: String
page: Int
language: String #index(name: "todosByLanguage", queryField: "todosByLanguage", sortKeyFields: ["createdAt"])
dateAndLocation: String
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
You will then be able to do a query, called TodosByLanguage where you can pass inn the sort direction. This query will require an input for language.
Adding a sort key to your main index: There is also the possibility to give expand your original key (ID field) with a sort key.
type Todo #model {
id: ID! #primaryKey(sortKeyFields: ["createdAt"])
title: String
...
}
I haven't done this approach before so I am not sure exactly how it will work. But you might be able to sort the data on the createdAt field with this. But this changes the core behaviour of your dynamoDB table, for example you might night to pass inn the sort key (createdAt) when you will retrieve one single item, or update items. So make sure you do this in a dev environment before you try.
Elasticsearch / Opensearch
The last solution is to add #searchable to your model. This will stream all your data in the todo table into an elasticsearch (only new ones, existig data won't automatically be streamed over). But be aware, elasticsearch is run on rented servers and can be expensive (depending on your budget).
type Todo #searchable #model {
id: ID!
title: String
...
}
Opensearch is much more flexible in doing queries than dynamoDB. And you will here be able to search and sort almost everything in the way you want it to be queried.
Edit
Elasticsearch is now called Opensearch in aws
I have a Post class and I want to get the products associated to it. I'd like to write a single query that fetches all of the posts with the products associated to them. Below is a rough idea of what I'd like the hash to looks like.
Schema:
Post - objectId, title
PostItem - post(pointer), product(pointer)
Product - objectId, title, price
Output I want
{'results':
[{'objectId':'blah', 'title':'The Post',
'products':
[{'objectId':'14',
'title':'First product',
'price':'20'
},
{'objectId':'26',
'title':'Second product',
'price':'55'
}]
},
{'objectId':'blah2', 'title':'Second post',
[{'objectId':'38',
'title':'Some product',
'price':'54'
},
{'objectId':'26',
'title':'Another Product',
'price':'88'
}]
}]
}
Is this possible to do in one query? How would I do this using curl and parse.com?
If this is a many-to-many relationship, use a Parse relationship.
You'll want to use the include key option alongside pointers. Since it's been nearly a year since you posted, I assume you figured it out, but lurkers can read more about this (in Javascript) here.
given this json?
[
{
"CompanyId":20,
"CompanyName":"Walmart",
"CompanyContacts":[
{
"CompanyId":20,
"FirstName":"Bob",
"LastName":"Green",
"Email":"bob#test.com",
"Phone":"1234567",
"IsActive":false
}
]
}
]
The KendoUI datasource schema.Model does not currently support nested json or json with related entities. It needs flat data. Hopefully in the future the schema.Model will support mapping complex json to flat in the model definition. However you can still use complex data in the grid you just can't define it in a schema.Model definition.
The mapping is actually done in the field definitions of the grid.
In addition see schema docs you can parse your data using the schema.parse or schema.data functions to manually transform your nested data into flat data.
Here is a fiddle example with your data
{
field : "CompanyContacts[0].FirstName",
title: "First Name"
}
Also note, if you don't need parent record CompanyName and CompanyID since you have CompanyID in your CompanyContacts in the way your data is currently defined then you can use the data attribute of the schema to indicate the starting point of your records like so
schema : {
model: mySchema,
data: "CompanyContacts"
},
I have the following property in my model:
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? Date { get; set; }
And I am trying to use the Html.DisplayFor helper to use this specification in a WebGrid column, like so:
Sources.Column("Date", "As Of Date", (item) => Html.DisplayFor(x => item))
When I run this, I get a lot of extra information in the column, and the date comes out as a long format date, instead of the desired short format. The output I get makes me suspect that DisplayFor is looking through each property in the model and printing it, instead of just looking at Date. Why would it do this? Is there something I can do to use DisplayFor in the WebGrid?
When I try to specify item.Date I get the error "An expression tree may not contain a dynamic operation"
Try this code snippet instead of what you have now:
Sources.Column( header: "Date", format: ( item -> { return #Html.Raw(item.date.ToString("d")); }))
I have a store + model which is connected to a 3rd party plugin (Ext.ux.TouchGridPanel). The plugin calls the store's sort() method properly with the relevant mapping. Everything is working fine, and the store sorts itself. However, I would prefer to add customer sorting to the store. I have tried adding a sortType field into my model:
Ext.regModel("Transactions", {
fields: [
{
name: 'unit',
type: 'string',
sortType: function(value) {
console.log('PRINT GDAMNIT');
return 0;
}
},
...
]
});
This, however, is not working, and the sortType is not getting called.
TLDR: How to make custom sorting work for stores?
Your store will need a sorter added that will sort on that field before it will call the sortType function.
var store = new Ext.data.Store({
model: 'Transactions',
sorters: [
{
property: 'unit',
direction: 'DESC'
}
]}
);
Sort type converts the value of a field into another value to ensure proper ordering. If you aren't sorting on that field than there is no reason to call that function. You could add the sortDir to the field which would sort the field into ascending/descending order based on the type of the field alone.
A workaround might be to (I know this sounds inefficient but bear with me) add an extra field to your model instances (lets call it sortField) and use that for your sorting function. You can then loop through your model instances in your store applying your custom sorting algorithm and assign a sort value of 0,1,2,3,4,5 etc.. to sortField. Then in your store, you can add 'sorters: 'sortField'... Hope this helps a bit, I'm going through something similar at the current moment.
The custom SortType in Sencha Touch 2 works accordingly, as per http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes:
Ext.apply(Ext.data.SortTypes, {
asPerson: function(person){
// expects an object with a first and last name property
return person.lastName.toUpperCase() + person.firstName.toLowerCase();
}
});
Ext.define('Employee', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'person',
sortType: 'asPerson'
}, {
name: 'salary',
type: 'float' // sortType set to asFloat
}]
}
});
What you're attempting to do might be tricky. Calling store.sort() removes all existing sorters by default (according to Sencha Touch API documentation). To keep the existing sorters you would need to add the sorter to the MixedCollection store.sorters.
Secondly, to call the sort method with a custom sort function, you would need to pass a specific sorterFn instead of property to the Sorter (again, see the API for more details) - but this might prove tricky since the sort call is initiated from the plugin.
Not sure if this helps to solve your problem, but maybe it assists you to look at the right direction.