PDA

View Full Version : Sorting Date



zimazipo
18 Jan 2011, 1:02 AM
Hello
I have a C# list collection that I'm trying to sort. The strings that I'm trying to sort are dates 18/01/2011 10:56 AM,15/09/2010 10:10AM,14/07/2011 4:11 PM,12/01/2011 9:18PM...etc. For the first time it sort correctly, after i click the column header, it become wrong sorting. If i sort desc, it will go
"18/01/2011 10:56 AM","15/09/2010 10:10AM","14/07/2011 4:11 PM","12/01/2011 9:18PM",..etc.
This is ordering them wrong because it sort by date.
i need to sort column header (Gridview) for date by year, month and date.

Condor
18 Jan 2011, 1:18 AM
If Ext needs to sort these dates as dates and not as strings then the store needs to contain Date instances.

Define your field as:

{name: 'myfield', type: 'date', dateFormat: 'd/m/Y H:iA'}
and your column as:

{header: 'My field', dataIndex: 'myfield', sortable: true, rendered: Ext.util.Format.dateRenderer('d/m/Y/ H:iA')}

zimazipo
18 Jan 2011, 1:55 AM
Hi Condor,
Below is my code, i still having problem with sorting date, if i uncomment the renderername, all record will empty. but if i commnet it, for the first time date sorting no problem when i click column header, sorting wrong, it sort by date not year, month follow by date.


if (File.Exists(MetaDataPath))
{
FileStream MyFileStream = new FileStream(MetaDataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
System.IO.TextReader tw = new System.IO.StreamReader(MyFileStream);

ArrAnn = (RecordsListMetaData[])xsel.Deserialize(tw);


int i = 0;
while (ArrAnn.Length > i)
{
writer.WriteStartObject();
writer.WriteMember("name");
writer.WriteString(ArrAnn[i].Name);
if (ArrAnn[i].type != "")
{
writer.WriteMember("type");
writer.WriteString(ArrAnn[i].type);

string strDefaultValue = ArrAnn[i].defaultValue + "";
switch (ArrAnn[i].type.ToLower())
{
case "string":
writer.WriteMember("defaultValue");
writer.WriteString(strDefaultValue);
break;
case "date":
writer.WriteMember("dateFormat");
writer.WriteString("d/m/Y H:iA");
break;
case "bool":
writer.WriteMember("defaultValue");
if (strDefaultValue != "")
{
if (strDefaultValue.ToLower() == "true")
writer.WriteBoolean(true);
else
writer.WriteBoolean(false);
}
else
writer.WriteBoolean(false);
break;
case "auto":
writer.WriteMember("defaultValue");
writer.WriteNull();
break;
default:
break;
}
}
else
{
if (ArrAnn[i].Name.StartsWith("Date "))
{

writer.WriteMember("type");
writer.WriteString("date");

writer.WriteMember("dateFormat");
writer.WriteString("d/m/Y H:iA");


}
}

//if (ArrAnn[i].defaultValue != "")
//{
// writer.WriteMember("defaultValue");
// writer.WriteNull();
//}

writer.WriteEndObject();
i++;
}
tw.Close();
}
else
GlobalFunc.Log("[initiateTreeGrid] File not exist: " + MetaDataPath);

if (isClassfication)
{
writer.WriteStartObject();
writer.WriteMember("name");
writer.WriteString("allowRecordsAttach");
writer.WriteMember("type");
writer.WriteString("bool");
writer.WriteEndObject();
}

writer.WriteEndArray();//end RecordDef

writer.WriteEndObject();//end metadata

writer.WriteMember("columnsConfig"); //columns
writer.WriteStartArray();

ArrayList al2 = new ArrayList();

RecordsListColumnsConfig[] ArrAnn2 = (RecordsListColumnsConfig[])al2.ToArray(typeof(RecordsListColumnsConfig));

XmlSerializer xsel2 = new XmlSerializer(ArrAnn2.GetType());


string ColumnsConfigDir = System.Configuration.ConfigurationManager.AppSettings["SaffronWorkArea"] + "/" + GlobalFunc.DATASETID + "/RecordsListDisplay/" + GlobalFunc.UserName;

string strRecordsListColumnsConfig = "RecordsListColumnsConfig.xml";

if (isClassfication)
{
strRecordsListColumnsConfig = "ClassificationsListColumnsConfig.xml";
}
if (iswindowpopup)
{
strRecordsListColumnsConfig = "iswindowpopup_RecordsListColumnsConfig.xml";
if (isClassfication)
{
strRecordsListColumnsConfig = "iswindowpopup_ClassificationsListColumnsConfig.xml";
}
}
string ColumnsConfigPath = ColumnsConfigDir + "/" + strRecordsListColumnsConfig;
if (!File.Exists(ColumnsConfigPath))
{
string OrigSettings2 = System.Configuration.ConfigurationManager.AppSettings["SaffronWorkArea"] + "/RecordsListDisplay/" + strRecordsListColumnsConfig;
Directory.CreateDirectory(ColumnsConfigDir);
File.Copy(OrigSettings2, ColumnsConfigPath);
}

if (File.Exists(ColumnsConfigPath))
{
FileStream MyFileStream = new FileStream(ColumnsConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
System.IO.TextReader tw = new System.IO.StreamReader(MyFileStream);

ArrAnn2 = (RecordsListColumnsConfig[])xsel2.Deserialize(tw);


int i = 0;
while (ArrAnn2.Length > i)
{
writer.WriteStartObject();
writer.WriteMember("header");
writer.WriteString(ArrAnn2[i].header);
writer.WriteMember("dataIndex");
writer.WriteString(ArrAnn2[i].dataIndex);

writer.WriteMember("width");
writer.WriteNumber(ArrAnn2[i].width);

if (ArrAnn2[i].id != "")
{
writer.WriteMember("id");
writer.WriteString(ArrAnn2[i].id);
}

if (ArrAnn2[i].rendererName != "")
{
writer.WriteMember("rendererName");
writer.WriteString(ArrAnn2[i].rendererName);
}
if (ArrAnn2[i].dataIndex.StartsWith("Date "))
{

//writer.WriteMember("rendererName");
// writer.WriteNumber("Ext.util.Format.dateRenderer('d/m/Y/ H:iA')}");
}

writer.WriteEndObject();
i++;
}
tw.Close();

}
else
GlobalFunc.Log("[initiateTreeGrid] File not exist: " + ColumnsConfigPath);


thanks

Condor
18 Jan 2011, 2:41 AM
Can you post the JSON data that this code actually generates?

zimazipo
18 Jan 2011, 5:54 PM
here the code

//set Columns
var columns = store.reader.jsonData.columnsConfig;
if (columns) {

if (AllowCheckBox)
columns.unshift(sm); //insert checkboxSelectionModel
colModel.setConfig(columns);

//set Columns Plugin Renderer
for (var i = 0; i < colModel.getColumnCount(); i++) {

if (colModel.config[i].dataIndex != "") { //avoid checkbox column
//set Breadcrumb title
if (colModel.config[i].id == 'firstColumn') {
tree.root_title = colModel.config[i].header;

var gridView = tree.getView();
//gridView.setRootBreadcrumbs();
}

var tmpRendererName = colModel.config[i].rendererName;
if (tmpRendererName == null)
tmpRendererName = ''; //set default renderer even though the rendererName is null
var colIndex = colModel.findColumnIndex(colModel.config[i].dataIndex);
switch (tmpRendererName.toLowerCase()) {
case "rendertips":
colModel.setRenderer(colIndex, renderTips);
break;
case "renderrecicon": // render record icon
colModel.setRenderer(colIndex, renderRecIcon);
break;
//case "rendertitle":
// colModel.setRenderer(colIndex, renderTitle);
// break;
case "rendericon":
colModel.setRenderer(colIndex, renderIcon);
break;
default:
colModel.setRenderer(colIndex, renderForValidation);
break;
}

//set renderer for Thumbnail column (if any) - in order to display thumbnail picture
if (colModel.config[i].dataIndex.toLowerCase() === "_thumbnail") {
colModel.setRenderer(colIndex, renderThumbnail);
}
}
}

Condor
18 Jan 2011, 11:29 PM
And an example of the jsonData?