PDA

View Full Version : Timefield shows full date when editing in Grid



Ralf
Jul 19, 2016, 10:54 AM
I'm using a TimeField as editor in a grid in .Net 4.5 Asp.Net MVC 5 with Ext.Net 4.1.0.0 and when I click inside the field to edit it, it shows the full date (e.g. "2008-01-01T16:45:00) instead of just the time ("16:45").

Example Code:



X.GridPanel()
.Title("Title")
.Height(400)
.Frame(true)
.Flex(3)
.ID("TestGrid")
.Store(Html.X().Store()
.Model(Html.X().Model()
.Fields(
new ModelField("Id", ModelFieldType.Auto),
new ModelField("EndTime", ModelFieldType.Auto, "H:i")
).IDProperty("Id")
)
.Proxy(X.RestProxy()
.AppendAction(false)
.Reader(X.JsonReader().RootProperty("data").MessageProperty("message"))
.API(urls =>
{
urls.Read = Url.Action("ReadValues");
})
.Writer(collection => collection.Add(X.JsonWriter().AllowSingle(true)))
)
.AutoLoad(true)
)
.ColumnModel(

Html.X().DateColumn().Text("End").DataIndex("EndTime").Format("H:i").Flex(1).Editor(X.TimeField().Format("H:i").AllowBlank(true)).TabIndex(10)
)
.Plugins(pl => pl.Add(X.CellEditing().ClicksToEdit(1)))


Model:



public class TestObject
{
public long? Id { get; set; }

public DateTime? EndTime { get; set; }
}


Controller Method Example:



[AcceptVerbs(HttpVerbs.Get)]
public RestResult ReadValues()
{
try
{
return new RestResult() {
Success = true,
Data = new List<TestObject>(){ new TestObject(){Id = 1, EndTime = DateTime.Now}} };

}
catch (Exception e)
{
return new RestResult
{
Success = false,
Message = e.Message
};
}
}


View of the Grid before clicking on the Field/Focussing it
24684

How it looks once you click it (With the full date string shown instead of just the time)
24683

How can I get the TimeField to only show the Time when it's focussed?

fabricio.murta
Jul 19, 2016, 10:24 PM
Hello @Ralf! Welcome to Ext.NET forums!

Have you searched the forums for similar questions? I seem to find this one very close to yours, at least:
Date time field editing in gridpanel (http://forums.ext.net/showthread.php?20189).

Ralf
Jul 20, 2016, 7:10 AM
Hello Fabricio,

thank you for your reply.

I did search the forums and google for about an hour before posting this thread.

I have also found the thread you linked previously, but I can't see a solution to my problem there. That thread seems to be an issue with having the same value in both a date- and a timefield, whereas I don't have a datefield at all, only a timefield. Or is there something specific I'm missing?

Is there anything else I could try?

On a sidenote, my company has Premium support, but the person usually responsible for Ext.Net is currently on vacation. What would be the best way to give my account premium support as well?

fabricio.murta
Jul 21, 2016, 6:12 AM
Hello Ralf,

Please email support@ext.net with your organizations Customer Number or original Transaction ID and we will check if your forum account can be upgraded to a Premium Member.

I'll try to get to your main question real soon!

fabricio.murta
Jul 21, 2016, 6:47 AM
And hello again!

About your code, view line 11, don't specify a date/time format there. You want it in date format and the default provided by a C# DateTime request will bind just fine. In other words, line 11 should read as:



new ModelField("EndTime", ModelFieldType.Date)


And you should get the editor the way you want.

I see you have German language there, so localization issues can happen. If you really really want it with the date format in the Model field:
- keep using ModelFieldtype.Date (from the code above)
- assuming you add the third argument as "H:i" in the line 11 of your view code
- instead of returning a DateTime object in the action, return a string in "H:i" format
- it could contain a string like DateTime.Now.Hour + ":" + DateTime.Now.Minute
- this way Model Field can interpret the input correctly.

I hope this helps!

Ralf
Jul 22, 2016, 7:09 AM
Hello Fabricio,

Thank you for your help!

I had tried setting ModelFieldType.Date before, but it always gave me an ArgumentException for the Asp.net MVC Databinding in the controller and it didn't fix the problem.

I just tried setting it again, and now it worked. I'm not entirely sure why it works now and didn't before, but well, as long as it works now, that's fine.

Thank you again.