Kendo Grid DatePicker编辑器模板DateTime更改值不起作用

时间:2014-02-28 15:47:36

标签: kendo-ui kendo-grid kendo-asp.net-mvc

考虑以下代码,如果日期验证失败,我想将网格datePicker列设置为空WorkOrderDate<任务日期,任何帮助都会非常明显。

** * ** * ** * 的** * ** * ** * ** * ** * **

columns.Bound(c => c.WorkOrderDetailsDate)
.Title("Estimated Start Date")
.EditorTemplateName("WorkOrderDetailsDate")

** * ** * ** * 的** 编辑 * ** * ** * ** * ** * *

@model DateTime?
@(Html.Kendo().DatePicker()
    .Name("WorkOrderDetailsDate")
    .Value(Model == null ? DateTime.Now.Date : ((DateTime)@Model).Date)
    .Events(d=>d.Change("TaskDateValidate"))
)

** * ** * ** * 的**** 的JavaScript * ** * ** * ** < EM> * *

function TaskDateValidate(e)
        {

            if ($("#workOrder_EstStartDate").val() != null && $("#workOrder_EstStartDate").val() != "") {                
                var workDate = kendo.parseDate($("#workOrder_EstStartDate").val());
                var taskDate = kendo.parseDate(kendo.toString(this.value(), 'd'));

                if (taskDate < workDate)
                {

                    showMessage("Task date should be after work order Date");                   
                    this.value(""); <-----this is not working want to set to empty to force user to select date again
                    this.value("28/02/2014");<---this is not working as well...
                }
            }

        }

请告知此问题 reagrds

Shaz

1 个答案:

答案 0 :(得分:0)

找到了我自己的解决方案......

//Add validation on Grid
            (function ($, kendo) {

                $.extend(true, kendo.ui.validator, {
                    rules: {
                        greaterdate: function (input) {
                            if (input.is("[data-val-greaterdate]") && input.val() != "") {
                                var date = kendo.parseDate(input.val()),
                                    earlierDate = kendo.parseDate($("[name='" + input.attr("data-val-greaterdate-earlierdate") + "']").val());
                                return !date || !earlierDate || earlierDate.getTime() < date.getTime();
                            }

                            return true;
                        },//end of greaterdate
                        shorterdate: function (input) {
                            if (input.is("[data-val-shorterdate]") && input.val() != "") {
                                var date = kendo.parseDate(input.val()),
                                    laterDate = kendo.parseDate($("[name='" + input.attr("data-val-shorterdate-laterdate") + "']").val());
                                return !date || !laterDate || laterDate.getTime() > date.getTime();
                            }
                            return true;
                        },//end of shorter date

                        // custom rules
                        taskdate: function (input, params) {

                            if (input.is("[name=WorkOrderDetailsDate]")) {

                                //If the input is StartDate or EndDate
                                var container = $(input).closest("tr");
                                var tempTask = container.find("input[name=WorkOrderDetailsDate]").data("kendoDatePicker").value();
                                var tempWork = $("#workOrder_EstStartDate").val();

                                var workDate = kendo.parseDate(tempWork);
                                var taskDate = kendo.parseDate(tempTask);
                                if (taskDate < workDate) {
                                    return false;
                                }
                            }

                            //check for the rule attribute
                            return true;
                        }

                    }, //end of rule                       
                    messages: {
                        greaterdate: function (input) {
                            return input.attr("data-val-greaterdate");
                        },
                        shorterdate: function (input) {
                            return input.attr("data-val-shorterdate");
                        },
                        taskdate: function (input) {
                            return "Task date must be after work date!";
                        },
                    }
                });
            })(jQuery, kendo);