This is a migrated thread and some comments may be shown as answers.

Validate child grid on save, at least 1 row in child grid.

1 Answer 264 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Csa
Top achievements
Rank 1
Csa asked on 03 Jun 2014, 02:53 AM
Hi All,

I have parent and child grid. When user create new, there must be 1 record in the child grid before it can save.
How can i validate this on client side before sending request to server?

So far i got this reply from my support ticket but it didn't work because at grid saveChanges event, e.container = null.
How do i get the detailrow on grid saveChanges event?

function onsaveChanges(e) {
    var detailRow = e.container.next(".k-detail-row");
    if (detailRow.length) {
        var grid = detailRow.find("[data-role='grid']").data("kendoGrid");
        var data = grid.dataSource.data();
        if (data.length === 0) {
            e.preventDefault();
            alert("error message");
        }
    }
}


Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Csa
Top achievements
Rank 1
answered on 03 Jun 2014, 03:00 AM
Please find my code bellow:



=======Razor=======

@{

    ViewBag.Title = "Index - Sample";

}

@section featured {

    <section class="featured">

        <div class="content-wrapper">

            <hgroup class="title">

                <h1>@ViewBag.Title.</h1>

                <h2>@ViewBag.Message</h2>

            </hgroup>

        </div>

    </section>

}



@(

    Html.Kendo().Grid<DHI_LIMS.ViewModels.H_SampleViewModel>()

        .Name("kendogrid")

        .Columns(columns =>

        {

            columns.ForeignKey(x => x.H_Sample_Project_ID,
(System.Collections.IEnumerable)ViewData["projects"], "Project_ID",
"Project_Code");

            columns.Bound(x => x.H_Sample_Registration_No);

            columns.Bound(x => x.H_Sample_Status);

            columns.Bound(x => x.H_Sample_Reference_No);

            columns.Bound(x => x.H_Sample_Receive_Date).Format("{0:dd/MM/yyyy}");

            columns.Bound(x => x.H_Sample_Receive_By);

            columns.Bound(x => x.H_Sample_Desc);

            columns.Command(command =>

            {

                command.Destroy();

                command.Custom("Duplicate")

                    .Text("Duplicate")

                    .Click("duplicateH_Sample")

                    .SendDataKeys(true);

                command.Custom("Receipt")

                    .Text("Receipt")

                    .Click("receiptH_Sample")

                    .SendDataKeys(true);

                command.Custom("Re-Open")

                    .Text("Re-Open")

                    .Click("reopenH_Sample")

                    .SendDataKeys(true);

                command.Custom("Review")

                    .Text("Review")

                    .Click("reviewH_Sample")

                    .SendDataKeys(true);

                command.Custom("ApproveH")

                    .Text("Approve")

                    .Click("approveH_Sample")

                    .SendDataKeys(true);

            });

        })        

        .ToolBar(toolbar =>

        {

            toolbar.Create();

            toolbar.Save();

        })        

        .DataSource(

            datasource => datasource

                .Ajax()

                .PageSize((int)@ViewBag.PageSize)

                .Read(read => read.Action("Read", "Sample"))

                .Create(create => create.Action("Create", "Sample"))

                .Update(update => update.Action("Update", "Sample"))

                .Destroy(destroy => destroy.Action("Destroy", "Sample"))

                .Sort(sort => sort.Add("H_Sample_Registration_No").Descending())

                .Events(events => events.Error("onErrorKendoGrid"))

                .Model(model =>

                {

                    model.Id(x => x.H_Sample_ID);

                    model.Field(x => x.H_Sample_Registration_No).Editable(false).DefaultValue("Auto Generated");

                    model.Field(x => x.H_Sample_Receive_By).Editable(false).DefaultValue(" ");

                    model.Field(x => x.H_Sample_Status).Editable(false).DefaultValue("New");

                    model.Field(x => x.H_Sample_Project_ID).DefaultValue((int)ViewData["projects_Default"]);

                })

        )

    .Editable(editable => editable.Mode(GridEditMode.InCell))

    .ClientDetailTemplateId("client-template")

    .Navigatable()

    .Pageable()

    .Sortable(

        sortable => sortable

        .AllowUnsort(true)

        .SortMode(GridSortMode.MultipleColumn)

    )

    .Filterable(filterable => filterable

        .Extra(false)

        .Operators(operators => operators

            .ForString(str => str.Clear()

                .Contains("Contains")

                .StartsWith("Starts with")

                .IsEqualTo("Is equal to")

                .IsNotEqualTo("Is not equal to")

            ))

        )     

    .Scrollable(scr=>scr.Height(350))

    .Resizable(resize => resize.Columns(true))

    .Events(events =>

    {

        events.DataBound("onDataBound_H_Sample");

        events.Save("onSave_H_Sample");

    })    

)



<script id="client-template" type="text/x-kendo-template">

    @(Html.Kendo().Grid<DHI_LIMS.ViewModels.D_SampleViewModel>()

        .Name("grid_#=H_Sample_ID#")

        .Columns(columns =>

        {

            columns.Bound(x => x.D_Sample_Registration_No);

            columns.ForeignKey(x => x.D_Sample_Matrix_Type_ID,
(System.Collections.IEnumerable)ViewData["sampleMatrixs"], "Item_ID",
"Item_Name");

            columns.Bound(x => x.D_Sample_Amount);

            columns.ForeignKey(x => x.D_Sample_Unit_ID,
(System.Collections.IEnumerable)ViewData["units"], "Unit_ID",
"Unit_Name");

            columns.Bound(x => x.D_Sample_Status);

            columns.Bound(x => x.D_Sample_Receive_Date).Format("{0:dd/MM/yyyy}");

            columns.Bound(x => x.D_Sample_Desc);

            columns.Bound(x => x.D_Sample_Reference_No);

            columns.Command(command =>

            {

                command.Destroy();

                command.Custom("Duplicate_D_Sample")

                    .Text("Duplicate")

                    .Click("duplicateD_Sample")

                    .SendDataKeys(true);                

            });

        })

        .DataSource(dataSource => dataSource

            .Ajax()

            .Read(read => read.Action("Read_D_Sample", "Sample", new { h_sampleID = "#=H_Sample_ID#" }))

            .Create(create => create.Action("Create_D_Sample", "Sample", new { h_sampleID = "#=H_Sample_ID#" }))

            .Update(update => update.Action("Update_D_Sample", "Sample"))

            .Destroy(destroy => destroy.Action("Destroy_D_Sample", "Sample"))

            .PageSize(10000)

            .Events(events => events.RequestEnd("onRequestEnd_D_Sample"))

            .Model(model =>

            {

                model.Id(x => x.D_Sample_ID);

                model.Field(x => x.D_Sample_Registration_No).Editable(false).DefaultValue("Auto Generated");

                model.Field(x => x.D_Sample_Receive_By).Editable(false).DefaultValue("You");

                model.Field(x => x.D_Sample_Status).Editable(false);

                model.Field(x => x.D_Sample_Amount).DefaultValue(1);

                model.Field(x => x.D_Sample_Status).DefaultValue("New");

                model.Field(x => x.D_Sample_Matrix_Type_ID).DefaultValue((int) ViewData["sampleMatrixs_Default"]);

                model.Field(x => x.D_Sample_Unit_ID).DefaultValue((int) ViewData["units_Default"]);

            })

        )

        .ToolBar(toolbar =>

        {

            toolbar.Create();

            toolbar.Save();

        })

        .Editable(editable => editable.Mode(GridEditMode.InCell))

        .Navigatable()

        .Sortable(

            sortable => sortable

            .AllowUnsort(true)

            .SortMode(GridSortMode.MultipleColumn)

        )

        .Events(events => events.DataBound("onDataBound_D_Sample"))

        .ToClientTemplate()

)



</script>



=======End of Razor=======



=======Script======



function onSave_H_Sample(e) {

    var detailRow = e.container.next(".k-detail-row");

    if (detailRow.length) {

        var grid = detailRow.find("[data-role='grid']").data("kendoGrid");

        var data = grid.dataSource.data();

        if (data.length === 0) {

            e.preventDefault();

            alert("Please fill in Sample Detail.");

        }

    }

}





=======End of Script======

Tags
Grid
Asked by
Csa
Top achievements
Rank 1
Answers by
Csa
Top achievements
Rank 1
Share this question
or