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

Prevent edit only on update

4 Answers 737 Views
Grid
This is a migrated thread and some comments may be shown as answers.
H2e
Top achievements
Rank 1
H2e asked on 01 Feb 2013, 12:32 PM
Hello.  I have a grid where the user can set the Primary key value of the table.  This is great, but I don't want them editing that value.
I only want the edit field to show up on creates.  I want it to be locked for normal edits.

If i set it to .Editable(False) then I can't add new ones.

I tried putting and onEdit event in which called a javascript which sought out that cell and called the .closecell() but that didn't work.  Maybe I was doing something wrong.

Is there an easy bit of javascript for closing cell 4 on an edit of an existing item.

I've added what I think I should be using..i'm just missing something...please help!
<script>
    function onEdit(e) {
        //if the model isn't new (editing)
        if (!e.model.isNew()) {
            var grid = $("#Devices").data("kendoGrid");
        //missing code I want her
            grid.closecell();
            }
    }
</script>

4 Answers, 1 is accepted

Sort by
0
Iliana Dyankova
Telerik team
answered on 05 Feb 2013, 08:43 AM
Hi Tony,

Actually the correct method is closeCell(), not closecell(). Hence in order to achieve the desired scenario you can hook up to the edit event, check the cell index and close it if it is the 3rd. Like here: 
function onEdit(e) {
    //get the cell index (zero based)
    var indexCell = e.container.context.cellIndex;
    if (indexCell == 3) {
       $('#Devices').data("kendoGrid").closeCell();
    }
}

 
Regards,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
H2e
Top achievements
Rank 1
answered on 05 Feb 2013, 09:21 AM
Hi.  I added that function but it doesn't appear to be working.  My kendo grid and javascript function are below

@(Html.Kendo().Grid(Of Models.Device) _
    .Name("Devices") _
    .Columns(Function(columns)
                     columns.Bound(Function(d) d.deviceTag).Groupable(True).EditorTemplateName("KendoGridString")
                     columns.Bound(Function(d) d.DeviceEnabled).Groupable(True)
                     columns.Bound(Function(d) d.DeviceDescription).Groupable(True).EditorTemplateName("KendoGridString")
                     columns.Bound(Function(d) d.ViewSuffix).Groupable(True).EditorTemplateName("KendoGridString")
                     columns.Command(Function(command)
                                             command.Edit()
                                     End Function).Width(200)
             End Function) _
    .Events(Function(events)
                    events.Edit("ClosePrimaryKey")
            End Function) _
    .ClientDetailTemplateId("DeviceRuleTemplate") _
    .Editable(Function(editable) editable.Mode(GridEditMode.InLine)) _
    .ToolBar(Function(toolbar) toolbar.Create().Text("Add New Device")) _
    .DataSource(Function(dataSource)
                        dataSource.Ajax.Events(Function(events) events.Error("error_handler"))
                        dataSource.Ajax.Model(Sub(model)
                                                      model.Id(Function(d) d.ViewSuffix)
                                                      'model.Field(Function(d) d.ViewSuffix).Editable(False)
                                              End Sub)
                        dataSource.Ajax.Create(Function(create) create.Action("Device_Create", "Device"))
                        dataSource.Ajax.Read(Function(read) read.Action("Device_Read", "Device"))
                        dataSource.Ajax.Update(Function(update) update.Action("Device_Update", "Device"))
                End Function)
    )
 
<script type="text/javascript">
    function ClosePrimaryKey(e) {
    //get the cell index (zero based)
        var indexCell = e.container.context.cellIndex;
       if (indexCell == 3) {
       $('#Devices').data("kendoGrid").closeCell();
    }
}
</script>
indexCell is apparently 'undefined'

Also, If I use the code you specified, then it will be called when I hit the "add new item" button too won't it?  They should be allowed to add a new one, just not update an existing one.
0
Accepted
Iliana Dyankova
Telerik team
answered on 07 Feb 2013, 08:54 AM
Hello Tony,

Thank you for the details.

Actually the suggested approach is suitable when Batch editing is used, however I observed that you are using Inline editing mode. Generally speaking in Kendo UI MVC Grid with Inline editing mode a single cell cannot be put out of edit mode. In order to achieve the desired outcome you could hook up to the Grid's edit event, find the cell and replace its content. For example: 
function edit(e) {
    if (!e.model.isNew()) {
        e.container.find("td:eq(3)").text(e.model.ViewSuffix);
    }
}

I hope this will fit your requirements.

Regards,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
H2e
Top achievements
Rank 1
answered on 07 Feb 2013, 05:08 PM
This was perfect!  thanks!
Tags
Grid
Asked by
H2e
Top achievements
Rank 1
Answers by
Iliana Dyankova
Telerik team
H2e
Top achievements
Rank 1
Share this question
or