DataTable Component and Blazor

DataTable Component and Blazor

JoeConatyJoeConaty Posts: 3Questions: 1Answers: 0
edited February 23 in Free community support

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hello All,

I found this great post for making a DataTable Component in Blazor. This works great. Unfortunately the sDom settings I have are somehow removing my Blazor events. Namely a click Event. Has anyone every experienced this? I'm guessing it has to do with either and instance or a refresh. I appreciate any help. Here is my example:

I'm using the DataTable Component example described here: https://datatables.net/forums/discussion/60598/working-with-datatable-and-blazor-server-side

My JavaScript has the following setting (When I comment this it works, but I lose my filter etc.)

sDom: '<"row view-filter"<"col-sm-12"<"float-left"l><"float-right"f><"clearfix">>>t<"row view-pager"<"col-sm-12"<"text-center"ip>>>',`

In my component I have the following:

 <DataTable id="tblJournalEntries" class="data-table responsive nowrap dataTable no-footer dtr-inline" style="width: 100%;"
         data-order="[[ 2, &quot;desc&quot; ]]">
     <thead>
     <tr>
         <th>Entry</th>
         <th>Author</th>
         <th>Date</th>
         <th class="empty">&nbsp;</th>
     </tr>
     </thead>
     <tbody>
     @foreach (var entry in journal.JournalEntries)
     {
         <tr>
             <td>
                 <p class="list-item-heading">
                         <a class="list-item-heading mb-0 w-40 w-xs-100 h-link" href=" javascript:;" @onclick="() => showEntryDetail(entry)" @onclick:preventDefault>
                             @entry.EntryHeader
                         </a>
                 </p>
             </td>
             <td>
                     <p class="mb-0 text-muted ">@entry.EntryBy</p>
             </td>
             <td>
                 <p class="mb-0 text-muted ">@entry.EntryDate.ToShortDateString()</p>
             </td>
             <td></td>
         </tr>
     }
     </tbody>
 </DataTable>

Server Side Code in Component:

protected override async Task OnAfterRenderAsync(bool firstRender)
 {
     // await base.OnAfterRenderAsync(firstRender);

     if (firstRender)
     {

         JournalDetailModule = new(await JS.InvokeAsync<IJSObjectReference>("import", "./Pages/JournalDetails.razor.js"));         
         await JournalDetailModule.Value.InvokeVoidAsync("init");

     }
 }

 private void showEntryDetail(JournalEntry entry)
 {
     entryDetail = entry.Entry;
     entryTitle = entry.EntryHeader;
     entryBy = entry.EntryBy;
     entryDate = entry.EntryDate.ToShortDateString();
     withMe = (entry.WithMe.HasValue? entry.WithMe.Value : false);
 }

This code works when I use a regular html table.

Edited by Allan - Syntax highlighting and general markdown formatting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    I've no idea why setting dom would effect the Blazer events. If you link to a test case showing the issue I might be able to identify something, but I'm really surprised by that.

    Allan

  • JoeConatyJoeConaty Posts: 3Questions: 1Answers: 0

    Hi @allan ,
    I am too. I just tested it on another grid with simple redirects from HREF's and the datatable component kills them. I'm not sure how to build a test case as Im new to this forum, but will try and whip something up.

  • JoeConatyJoeConaty Posts: 3Questions: 1Answers: 0

    Update: its not due to setting the dom. Its for any Razor event. Once I use the datatable component everything dies.

  • alebrosalebros Posts: 4Questions: 1Answers: 1

    Blazor is unable to manage its own events due to DataTables modifying the DOM. This seems to be a common challenge when integrating DataTables with Blazor applications, as Blazor has its own internal representation of the DOM. When external libraries like DataTables make changes to the actual DOM, these modifications are not reflected in Blazor's internal DOM state.

    I use DataTables alongside Blazor. The data is loaded through REST APIs, and the DataTables events are triggered by JavaScript embedded in the page. These scripts call Blazor methods that are marked as invokable.
    This setup allows for a seamless integration where DataTables can leverage Blazor's capabilities for handling events and data manipulation. However, it's important to ensure that the JavaScript calls are correctly wired to the Blazor invokable methods to maintain the integrity of the event handling process.

    Lastly, I want to add that it's also possible to invoke Blazor methods by generating clicks at runtime through JavaScript on hidden elements that handle the click event. These elements can take page data as input, such as the selected item in the DataTable.
    This approach can be particularly useful for integrating DataTables with Blazor when you need to pass specific data to Blazor methods. By simulating clicks on hidden elements, you can trigger Blazor's event handlers and pass the necessary data without disrupting the user experience.
    Honestly I haven't found this last way of using events documented anywhere, however I find it a very convenient trick that I have implemented in some of my components with great ease of use.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    I haven't done much development with Blazor, so that (last) technique is not something I am familiar with, which is turn is why you won't find it documented on this site. I generally don't document per framework because there are millions of them.

    Perhaps you could show some code for how to do that so others could benefit from it if they are also using Blazor?

    Allan

Sign In or Register to comment.