How to use jQuery UI from Blazor component

前端 未结 4 1257
孤城傲影
孤城傲影 2020-12-20 02:51

I am working through some Blazor examples, and while trying to work with some JSInterop solutions, I ran into an issue with jQuery UI elements. I am not a proficient Javascr

相关标签:
4条回答
  • 2020-12-20 03:15

    The most probable cause is timing. The jQuery function gets executed before the DOM elements exists.

    You should register the JS code as an interop function and call that after in OnAfterRender event.

    0 讨论(0)
  • 2020-12-20 03:16

    The problem is that of timing: your jQuery function executes before your Blazor app has rendered.

    The way I solved this is by replacing the "onready" ($(...)) function with a named function (e.g. onBlazorReady) that I then invoke from my Blazor's MainLayout component at the right time.

    The right time being OnAfterRender.

    For example:

    MainLayout.razor:

    @code {
       [Inject]
       protected IJSRuntime JsRuntime { get; set; }
    
       protected override void OnAfterRender(bool firstRender)
       {
           if (firstRender)
               JsRuntime.InvokeVoidAsync("onBlazorReady");
       }
    }
    

    index.html:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width">
        <base href="/" />
        <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
        <link href="css/site.css" rel="stylesheet" />
        <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    </head>
    <body>
        <app>Loading...</app>
    
        <script src="_framework/blazor.server.js"></script>
        <script>
            onBlazorReady() {
                $("#resizable").resizable();
            });
        </script>
    </body>
    </html>
    

    As you can see, I'm injecting IJSInterop so that I can call my onBlazorReady JS function after the LayoutComponent has rendered.

    0 讨论(0)
  • 2020-12-20 03:19

    Normally you don't need any JQuery function like ready, you can simply put those all required code into your custom js function which should run on ON_LOAD of your component as said by @Sipke Schoorstra in above answer

    0 讨论(0)
  • 2020-12-20 03:42

    Works fine for me like this, to add datepicker from jquery ui:

    Razor component file.

    @code {
        protected override async void OnAfterRender(bool firstRender)
        {
            await jsRuntime.InvokeVoidAsync("addDatePicker");
            base.OnAfterRender(firstRender);
        }
    }
    

    Global razor file:

    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Magiro.Blazor</title>
        <base href="~/" />
        <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
        <link href="css/site.css" rel="stylesheet" />
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    </head>
    <body>
        <app>
            @(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
        </app>
    
        <script src="_framework/blazor.server.js"></script>
        <script>
            window.addDatePicker = () => {
                $(".datepicker").datepicker();
            }
        </script>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题