Check if grid is initialized

8 posts, 1 answers
  1. Stephen
    Stephen avatar
    158 posts
    Member since:
    Jan 2009

    Posted 14 Apr 2014 Link to this post

    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.






  2. Answer
    Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2310 posts

    Posted 15 Apr 2014 Link to this post

    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.

     
  3. Stephen
    Stephen avatar
    158 posts
    Member since:
    Jan 2009

    Posted 22 Apr 2014 in reply to Dimiter Madjarov Link to this post

    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?
  4. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2310 posts

    Posted 23 Apr 2014 Link to this post

    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.

     
  5. Stephen
    Stephen avatar
    158 posts
    Member since:
    Jan 2009

    Posted 23 Apr 2014 in reply to Dimiter Madjarov Link to this post

    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?
  6. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2310 posts

    Posted 23 Apr 2014 Link to this post

    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.

     
  7. Stephen
    Stephen avatar
    158 posts
    Member since:
    Jan 2009

    Posted 23 Apr 2014 in reply to Dimiter Madjarov Link to this post

    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!
  8. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2310 posts

    Posted 23 Apr 2014 Link to this post

    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.

     
Back to Top