使用 Kendo Grid 获取数据时日期格式正在发生变化 [英] Date format is changing while fetching data using Kendo Grid
问题描述
我正在尝试使用一些参数获取数据并将其加载到剑道网格中.但是当我使用日期参数时,日期格式正在改变,因此在服务器端显示错误的日期.
作为我使用的参数示例:new Date("April 01, 2016").但是在服务器端它变成了 04/01/2016,这是错误的.
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("2016 年 4 月 12 日");statemenetInquiryParameter.customerCode = 007;返回 {statemenetInquiryParameter:statemenetInquiryParameter}}
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">@(Html.Kendo().Grid().Name("gridCustomerCstTranDetails").Columns(columns =>{columns.Bound(p => p.cst_inv_date).Title("发票日期").HtmlAttributes(new { @style = "text-align: right;" }).Format(Session["DisplayFormat_GridDate"].ToString()).宽度(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("发票编号").Width(80);columns.Bound(p => p.cst_dr_amount).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("借方").宽度(80);columns.Bound(p => p.cst_cr_amount).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("信用").宽度(80);columns.Bound(p => p.cst_dr_balance).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("平衡").宽度(80);}).Selectable().Sortable().Scrollable().Resizable(resize => resize.Columns(true)).HtmlAttributes(new { style = "cursor:pointer;height:auto;width:auto;margin-top: 0px;" }).DataSource(dataSource => 数据源.Ajax().Read(read => read.Action("LoadCustomerStatementEnquiryDetails", "Stage").Data("passFilterCstDetails"))))
公共类StatemenetInquiryParameter{公共十进制客户代码 { 获取;放;}公共日期时间 cst_stmt_from { 获取;放;}公共日期时间 cst_stmt_to { 获取;放;}public bool isPrintZero { get;放;}public bool isPrintPayments { get;放;}public bool isPrintAdjust { get;放;}}public ActionResult LoadCustomerStatementEnquiryDetails([DataSourceRequest]DataSourceRequest request, StatemenetInquiryParameter statemenetInquiryParameter){列表l = new List();for (int i = 0; i <12; i++){CstTran c = new CstTran();c.cst_inv_date = statemenetInquiryParameter.cst_stmt_from.AddDays(i);c.cst_type = "我";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.添加(c);}返回 Json(l.ToDataSourceResult(request));}
我也遇到了这个问题,我通过保存时检查数据格式解决了.
1- 保存时查找数据
var dataS = $("#grid").data("kendoGrid").dataSource;var updatedData = dataS._data;
2-检查格式然后保存,我的数据参数是rsrc_dt
var dateValue = updatedData[i].rsrc_dt;无功日;变年;var mon;if (typeof dateValue === 'string' || dateValue instanceof String) {day = dateValue.split('/')[0];//当日期的字符串格式不正确时使用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);年份 = date.getFullYear();日 = date.getDate();天 = 天 <10 ?'0' + 天:天mon = date.getMonth();mon = mon + 1;mon = mon <10 ?'0' + mon : mon;var dateF = 日 + "/" + mon + "/" + 年;serverData[i].rsrc_dt = dateF;}
3- 你也可以试试这个,像这样在字段模板中给出数据格式
{ field: "rsrc_dt", title: "Session Date", format: "{0:dd/MM/yyyy}", editor: dateTimeEditor, width: 73, attributes: { "class": "azN" }, },
4- 使用日期编辑器
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(容器).kendoDatePicker({ min: btch_strt_dt });}
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.
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- find the data while saving
var dataS = $("#grid").data("kendoGrid").dataSource;
var updatedData = dataS._data;
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- 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- 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屋!