Sort by datetime, then group by date - sorting

I want to sort by a datetime value, and group by a date string.
I have found the sortProperty on the grouper:
You can set this configuration if you want the groups to be sorted on something other then the group string returned by the groupFn. This serves the same role as property on a normal Ext.util.Sorter.
So I tried the following, which only sorts the date correctly:
grouper:{
sortProperty: 'StartDate',
property: 'StartDateOnly',
direction: 'ASC'
},
and I tried the following, which only sorts the time correctly:
grouper:{
sortProperty: 'StartDate',
property: 'StartDateOnly',
direction: 'ASC'
},
sorters: [{
property: 'StartDate',
direction: 'ASC'
}]
You can try it here:
https://fiddle.sencha.com/#view/editor&fiddle/2cei
What am I doing wrong here?

The problem is that you make a sort on a string type but you have date by changing the code on your sencha fiddle like this :
Ext.application({
name : 'Fiddle',
launch : function() {
var mdl = Ext.define('', {
extend: 'Ext.data.Model',
fields: [{
/** #field {date} StartDate with format: Y-m-d H:i */
name: 'StartDate',
type: 'date',
dateFormat: 'Y-m-d H:i'
},{
name: 'StartDateOnly',
type: 'date', // <= here before it was 'string'
convert: function(v,rec) {
return Ext.Date.format(rec.get('StartDate'), 'D d.m.Y');
}
}]
});
...
Then you run, you can see that the group of date StartDateOnly is now order by ASC.
After that, you can retrieve this data in date format and convert it into a string. But if you make a sort on a string you will always have the first of February last like now.
The problem is that it does an alphabetical sort on the days of the week.
In order: Fri, Sat, Thu.

Sencha support did not provide a full solution, but helped me a bit on my way. They found how to "fix" the problem at hand and told me to change the data type of the StartDateOnly column to date, but neither did this fix prove universal nor did it make any sense that this fixed the problem at all. But it showed some underlying correlations, which helped me to dig into the problem and find a universal solution.
I would say the issue is a bug in ExtJS, but I am awaiting confirmation from Sencha on this. I have to yet find the exact line where the bug is introduced.
Problem description:
When sortProperty is set on the grouper and at least one sorter is added on the store, the "transform" function of the grouper is set to the "transform" function of the first sorter (otherwise, it is null).
Solution:
You can manually override the transform function on the grouper by adding the correct sort type explicitly to the grouper config:
grouper:{
sortProperty: 'StartDate',
property: 'StartDateOnly',
transform: Ext.data.SortTypes.asDate,
direction: 'ASC'
},

Related

ID values for jqGrid select lists

I am using jQrid version 3.8.1 and I have a grid that displays information about cars. The jQgrid should is set up to display one car per row and one of the columns is a multi-select list that displays which types of seats the car is configured with. A car can have multiple seat types.
When the user edits a car row, it makes an ajax query to get all of the seats types available in the system and sticks them in the multi-select list. In addition to populating the list, it needs to also select the options already chosen for that car.
The values inside the Installed Seats column are not simple strings. They have both an ID and a string value. So the ID for "Wire mesh" might be 2883 and the value for "Composite" might be 29991. They are just unique numeric values (basically the primary key from the table they are stored in).
After the multi-select list is populated with all the appropriate Seat values, I need to select the ones that the car currently has installed (in the picture above it's "Steel" and "Wire frame"). I need to do this based on the seat IDs stored for that car. However, I don't know where these value are going to come from. The grid only stores the names for the seats, not the IDs. Hopefully there is a way to make it store both.
The column model looks like this:
colModel: [
{ name: 'Year', index: 'Year', editable: true, edittype: "select", editoptions: { multiple: true } },
{ name: 'Make', index: 'Make', editable: true, edittype: "select", editoptions: { multiple: true } },
{ name: 'Body', index: 'Body', editable: true, edittype: "select", editoptions: { multiple: true } },
{ name: 'Seats', index: 'Seats', editable: true, edittype: "select", editoptions: { multiple: true }, cellattr='is-seat-list="1"' }
]
Notice that the 'Seats' model has a cell attribute called is-seat-list. I'm using this to find the select box in the row inside the 'editRow' function.
The onSelectRow handler looks like this:
onSelectRow: function (index) {
var curValues = $('#cargrid').getRowData(index);
jQuery('#cargrid').jqGrid('editRow', index, true, function(rowId) {
//when the user edits the row, query for all the seat types and fill in the list
jQuery.ajax({
url: '/getalltheseats',
complete: function (allSeats, stat) {
var list = $('#cargrid').find('tr[id="' + rowId + '"] td[is-seat-list="1"] select');
var $list = $(list);
//add the all seat types to the list, checking the ones that this car currently has selected
_.each(allSeats, function(seat) {
var selected = '';
if(curValues['Seats'].indexOf(seat.ID) !== -1) //<-- what do I do here??
selected = 'selected';
$list.append($('<option ' + selected + '></option>').attr('value', seat.ID).text(seat.Name));
});
});
});
});
},
The important line is
if(curValues['Seats'].indexOf(seat.ID) !== -1)
I have the value of the row but it only contains the seat name, not the ID. The data returned from the ajax call contains each seat name and ID but the <option> elements don't contain the ID value so I don't know which ones to select in the list.
So the question is, what's the best way to make jqGrid store both the seat names and IDs so that when I create the list dynamically, I can check the <option>s for the seats that have been chosen for that car.
Note:
For various reasons the standard dataUrl and buildSelect features of jqGrid are not going to work for me, which is why I'm building the list on the fly in this manner.
First of all you need additionally add formatter: "select" and to populate ID values in Seats column during filling of the grid. The formatter: "select" will decode the IDs and the corresponding Name value will be displayed to the user.
If you would use more recent version of jqGrid the you can use beforeProcessing callback created for the purpose. It allows to include all different ID/Name pairs in the server response for filling of the grid. It allows you to fill the information needed for the formatter: "select" directly in the main server response. You don't need to load the information before creating the grid.
If you use retro version of jqGrid (3.8.1) then I hope that you can still use the following trick. You can define userdata part of the server response defined as function. The outer elements of the server response root, page, total, records and userdata will be processed before the processing the main part with all items. It allows you to modify editoptions.value before it will be processed by formatter: "select".
For example the response from the server can looks like
{
"page": 1,
"total": 20,
"records": 400,
"userdata": {
"seats": "29991:Composite;42713:Nappa leather;6421:Steel;2883:Wire mesh"
},
"rows": [
{
"year": 2007,
"model": "Toyota/Camry",
"body": "Sedan",
"seats": "29991,6421"
},
{
"year": 2057,
"model": "BMW/Series 4",
"body": "Sedan",
"seats": "6421,2883"
}
]
}
Inside of jsonReader you can define userdata which set userdata.seats as value of editoptions. You can use setColProp method for example to do this.
In the way you will be able to implement all your requirements.

jqGrid casting data to a String before sort

I'm trying to sort a list of orders based on total money spent on the purchase. The list looks like this (extra fields omitted for clarity):
[
{products:[
{name: 'foo',price:2.53},
{name: 'bar',price:5.74}
]},
{products:[
{name: 'baz',price:6.74},
{name: 'quux', price:7.68}
]}
]
The column definition in question looks like this (with sumPricesOf taking an array similar to the above and returning the total spent):
{
name: 'products',
label: 'Total spent',
index: 'products',
formatter: function(d){return '$'+sumPricesOf(d.products)},
sorttype: 'function',
sortfunc: function(a,b){
return sumPricesOf(a.products) - sumPricesOf(b.products);
}
}
However, if I use console.log(a,b) in the sortfunc, when it gets called a and b are getting passed as strings! The arguments get passed as [object Object],[object Object].
Why can't jqGrid just give me back the data that I passed it?
Strong recomend use sorttype: float option in colModel array.

How do I get the formatted value of one column in another column in jqgrid

How do I get the formatted value of one column in another column in jqgrid.
For eg:
{ name: 'amount', index: 'amount', sorttype: "float", formatter: processAmount, title: false },
{ name: 'netAmount', index: 'netAmount', sorttype: "float", formatter: function (cellvalue, options, rowObject)
{
// How do I get the formatted value of column "amount" here?
}
}
I know that I am posting very little of my requirement or code. But I hope this is sufficient. Please let me know if you need more information on anything.
Thanks,
Sam
Custom formatters will be called before the body of the grid will be placed on the page. So you can't access the formatted value of one column inside of custom formatter of another column. What you can still do is calling of another formatfer. For example you can call processAmount function inside of formatter of netAmount column.

jqgrid editable x number formating is inconsisent with format on grid

I've built a grid as the code bellow:
colModel: [
{ name: 'price',
label: 'price',
index: 'price',
jsonmap: 'price',
formatter: 'number',
formatoptions: {decimalSeparator:",", thousandsSeparator: ".", decimalPlaces: 2, defaultValue: '0,00'},
editable: true
}
]
The format of field is correctly on grid, for instance: 10,32, but the form created to edit the field fills one with 10.32 instead of 10,32.
Someone knows why this is going on? Do I need to use properties as edittype and editoptions (this one using formmater and formatoptions) as well? if yes, How I need set up these properties?
I've fixed the problem using the the function afterShowForm to handle the formating of field on form generated from grid.
In fact, I was expecting that jQGrid could do that automatically, i.e, using the configuration provided to column to apply on field generated, or if i could apply the configurion on JSON message, for instance:
editoptons: { formatter = "number", formatteroptions = { .... } ...
anyway, it's working now.

ExtJS checking value of a cell before submitting

I have a column named event_date. I want to warn the user if he trys to submit the info without filling up the event_date column and prevent further execution. Here is how I define my column:
header: 'Event_date',
width: 115,
dataIndex: 'event_time',
renderer: this._renderExactDate,
field: {
type: 'textfield'
}
and my render function is:
_renderExactDate: function(date) {
if (date == '0000-00-00 00:00:00' || date == undefined) {
Ext.MessageBox.show({
title: 'New event',
msg: 'You must set a date',
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
}
else
{
return date;
}
}
but obv. renderer is not the way to do this, yet I haven't found a way to do this.
thanks
Leron
If this is using ExtJS4 then you should make use of the model field validations field to set the accepted format. Write a custom one if any of the standards don't fit the bill. Then you can check if the model is valid before any attempts to sync and flag a warning accordingly.
The event you want to hook into is this one. Then you can get the record/model and check its 'isValid' method before generating your warning.
Link: Ext.grid.plugin.CellEditing validateedit event

Resources