使用Kendo Grid获取数据时,日期格式正在改变 [英] Date format is changing while fetching data using Kendo Grid
问题描述
我正在尝试使用一些参数来获取和加载数据到kendo网格。但是当我使用date参数时,日期格式正在改变,因此在服务器端显示错误的日期。
I am trying to fetch and load data to a kendo grid using some parameters. But when I am using date parameter, format of date is changing hence showing me wrong dates in server side.
作为参数的一个例子,我使用的是:new Date 2016年4月1日)。但是在服务器端,这是04/01/2016是错误的。
As an example as a parameter I am using: new Date("April 01, 2016"). But in server side it becomes 04/01/2016 which is wrong.
function passFilterCstDetails() {
var statemenetInquiryParameter = {};
statemenetInquiryParameter.isPrintZero = true;
statemenetInquiryParameter.isPrintPayments = true;
statemenetInquiryParameter.isPrintAdjust = true;
statemenetInquiryParameter.cst_stmt_from = new Date("April 01, 2016");
statemenetInquiryParameter.cst_stmt_to = new Date("April 12, 2016");
statemenetInquiryParameter.customerCode = 007;
return {
statemenetInquiryParameter: statemenetInquiryParameter
}
}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
@(Html.Kendo().Grid<ServicePROWeb.ServiceProWCFService.CstTran>()
.Name("gridCustomerCstTranDetails")
.Columns(columns =>
{
columns.Bound(p => p.cst_inv_date).Title("Invoice Date").HtmlAttributes(new { @style = "text-align: right;" }).Format(Session["DisplayFormat_GridDate"].ToString()).Width(80);
columns.Bound(p => p.cst_type).Title("Type").Width(80);
columns.Bound(p => p.cst_ih_invno).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Invoice Number").Width(80);
columns.Bound(p => p.cst_dr_amount).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Debit").Width(80);
columns.Bound(p => p.cst_cr_amount).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Credit").Width(80);
columns.Bound(p => p.cst_dr_balance).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Balance").Width(80);
})
.Selectable()
.Sortable()
.Scrollable()
.Resizable(resize => resize.Columns(true))
.HtmlAttributes(new { style = "cursor:pointer;height:auto;width:auto;margin-top: 0px;" })
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("LoadCustomerStatementEnquiryDetails", "Stage").Data("passFilterCstDetails")))
)
</div>
</div>
public class StatemenetInquiryParameter
{
public decimal customerCode { get; set; }
public DateTime cst_stmt_from { get; set; }
public DateTime cst_stmt_to { get; set; }
public bool isPrintZero { get; set; }
public bool isPrintPayments { get; set; }
public bool isPrintAdjust { get; set; }
}
public ActionResult LoadCustomerStatementEnquiryDetails([DataSourceRequest]DataSourceRequest request, StatemenetInquiryParameter statemenetInquiryParameter)
{
List<CstTran> l = new List<CstTran>();
for (int i = 0; i < 12; i++)
{
CstTran c = new CstTran();
c.cst_inv_date = statemenetInquiryParameter.cst_stmt_from.AddDays(i);
c.cst_type = "I";
c.cst_ih_invno = i + 1;
c.cst_dr_amount = i;
c.cst_cr_amount = 0;
c.cst_dr_balance = c.cst_dr_balance + i;
l.Add(c);
}
return Json(l.ToDataSourceResult(request));
}
推荐答案
我也有这个问题,我通过检查保存时的数据格式来解决它。
I too had this problem, i solved it by checking the data format at the time of saving.
1-保存时查找数据
var dataS = $("#grid").data("kendoGrid").dataSource;
var updatedData = dataS._data;
2-检查格式然后保存,我的数据参数是rsrc_dt
2- check the format and then save it, my data parameter is rsrc_dt
var dateValue = updatedData[i].rsrc_dt;
var day; var year; var mon;
if (typeof dateValue === 'string' || dateValue instanceof String) {
day = dateValue.split('/')[0]; // use when date is not in correct string format
mon = dateValue.split('/')[1];
year = dateValue.split('/')[2];
var dateS = day + '/' + mon + '/' +year;
serverData[i].rsrc_dt = dateValue;
}
else if (dateValue instanceof Date) {
var date = new Date(updatedData[i].rsrc_dt);
year = date.getFullYear();
day = date.getDate();
day = day < 10 ? '0' + day : day
mon = date.getMonth();
mon = mon + 1;
mon = mon < 10 ? '0' + mon : mon;
var dateF = day + "/" + mon + "/" + year;
serverData[i].rsrc_dt = dateF;
}
3-你也可以尝试这个,在字段模板中给出数据格式,如这个一个
3- You may try this also, give format of data in field template like this one
{ field: "rsrc_dt", title: "Session Date", format: "{0:dd/MM/yyyy}", editor: dateTimeEditor, width: 73, attributes: { "class": "azN" }, },
4-使用DateEditor
4- Use DateEditor
function dateTimeEditor(container, options) {
$('<input name="editableBlock" data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
.appendTo(container)
.kendoDatePicker({ min: btch_strt_dt });
}
这篇关于使用Kendo Grid获取数据时,日期格式正在改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!