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

Check if grid is initialized

7 Answers 1029 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 14 Apr 2014, 07:39 PM
I am trying to disable the delete buttons of a grid in jquery depending on certain conditions.  If I check my conditions on document.ready the code to disable the buttons is not working, seemingly because the grid has not been initialized yet.  I am trying to figure out how to check if it is initialized so I can do my check and then disable the buttons if necessary.  I have tried it onDataBinding but that doesn't seem to do it.  Here is the relevant code:

    @(Html.Kendo().Grid<PASS.ViewModels.Proposals.AttachmentsViewModel>()
        .Name("gridAttachments")
        .Columns(columns =>
        {
            columns.Bound(c => c.File_Name).ClientTemplate("<a href='" + Url.Action("LoadAttachment", "Proposals") + "/#= ID #'>" + "#= File_Name #" + "</a>").Title("File Name");
            columns.Bound(c => c.File_Size).Title("Size");
            columns.Bound(c => c.Content_Type).Title("Type");
            columns.Command(command => { command.Destroy(); }).Width(90);
        })
        .Sortable()
        .Events(events => events.DataBinding("onDataBinding"))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Model(model => model.Id(c => c.ID))
            .Read(read => read.Action("GetAttachments", "Proposals", new { proposalID = Model.Proposal_ID }))
            .Destroy(destroy => destroy.Action("DeleteAttachment", "Proposals"))
        )
    )
 
<script type="text/javascript">
$(document).ready(function () {
    var formDisabled = $('#Form_Disabled').val();
    if (formDisabled == "True") {
        $('#Files').data('kendoUpload').disable();
    }
})
 
$(function () {
    $("#Files").data("kendoUpload").bind("success", function () {
        $("#gridAttachments").data("kendoGrid").dataSource.read();
    })
})
 
function onDataBinding(e) {
    var formDisabled = $('#Form_Disabled').val();
    alert(formDisabled);
    if (formDisabled == "True") {
        $('.k-grid-delete', '#gridAttachments').hide();
    }
}
</script>

I have also tried assigning the onDataBinding in jquery instead of in the razor code but that didn't work either.






7 Answers, 1 is accepted

Sort by
0
Accepted
Dimiter Madjarov
Telerik team
answered on 15 Apr 2014, 08:25 AM
Hello Stephen,

The Grid itself is initialized on document ready, but in order to be sure that the data is bound, you should bind to the dataBound event. In the event handler you could access the Grid rows and hide the delete buttons.

I hope this information helps.

Regards,
Dimiter Madjarov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Stephen
Top achievements
Rank 1
answered on 22 Apr 2014, 07:02 PM
It works for me using the dataBound event but I could only get it to fire if I bound it within the script like this:
$('#Attachments').data('kendoGrid').bind("dataBound", "onDataBound");

If I did it as part of the Grid, the javascript function is never fired:
.Events(events => events.DataBound("onDataBound"))

Can you tell me why?
0
Dimiter Madjarov
Telerik team
answered on 23 Apr 2014, 10:15 AM
Hi Stephen,


If the data is not bound at all, when the event is part of the Grid configuration, this means that the onDataBound function is not being initialized yet at the time the Grid is created. This would also result in an error in the developer tools console. The JavaScript function should be already existing in the page, when the Grid is being initialized.

Regards,
Dimiter Madjarov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Stephen
Top achievements
Rank 1
answered on 23 Apr 2014, 01:52 PM
I am not receiving an error when I set it in the Grid configuration, it just never hits the javascript function that I point it to.  Could I have something else set incorrectly to cause this to happen?
0
Dimiter Madjarov
Telerik team
answered on 23 Apr 2014, 02:13 PM
Hi Stephen,


This issue sounds really strange and I could not state the exact reason for it. Would it be possible to provide a small runnable example, which demonstrates the problem, so I could inspect it locally and assist you further?

I am looking forward to hearing from you.

Regards,
Dimiter Madjarov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Stephen
Top achievements
Rank 1
answered on 23 Apr 2014, 02:17 PM
Hi,

Thanks again for all of your help.  I can try to put together a sample project for you to inspect it just may take a few weeks before I can do that.  I've got a major deadline coming up and I need to keep moving forward for the time being.  It is working with the event hooked up in javascript so I can move on for now but I definitely would like to find out why it is not working the other way.  Thanks!
0
Dimiter Madjarov
Telerik team
answered on 23 Apr 2014, 02:25 PM
Hi Stephen,


Thank you for the update. Do not hesitate to contact us again if an assistance is required with this or any other issue.

I wish you a great day!

Regards,
Dimiter Madjarov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Stephen
Top achievements
Rank 1
Share this question
or