I load page-specific javascript files as follows. But when I call the page it gives an error like this
Hi,
I load page-specific javascript files as follows.
<pre><script suppress-error="BL9992" src='@KTTheme.GetAssetPath("zcustom/js/datatable.init.js")'></script></pre> <pre> protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await js.InvokeAsync<IJSObjectReference>("TestDataTablesAdd", "#kt_datatable_example"); //line 473 } } </pre>Javascript File:
<pre> function TestDataTablesAdd(tableName/*, jsonData, jsonColumns*/) { $(document).ready(function () { var table = document.querySelector(tableName); $(table).DataTable({ //"data": jsonData, //"columns": jsonColumns, "fixedHeader": { "header": true, "headerOffset": 73 }, columnDefs: [ { orderable: false, targets: 0 }, { orderable: false, targets: 1 }, { "visible": false, "targets": 1 } ], order: [[2, 'asc']], "scrollX": true, //"responsive": true, "info": false, "pageLength": 10, "select": true, "select": { style: 'multi', selector: 'td:first-child input[type="checkbox"]', className: 'row-selected' } }); }); } function TestDataTablesRemove(tableName) { $(document).ready(function () { var table = document.querySelector(tableName); $(table).DataTable().destroy(); // Removes the datatable wrapper from the dom. var elem = document.querySelector(table + '_wrapper'); elem.parentNode.removeChild(elem); }); } </pre><strong>But when I call the page it gives an error like this</strong>
[2023-09-27T04:38:08.993Z] Error: Microsoft.JSInterop.JSException: Could not find 'TestDataTablesAdd' ('TestDataTablesAdd' was undefined). Error: Could not find 'TestDataTablesAdd' ('TestDataTablesAdd' was undefined). at http://localhost:5070/_framework/blazor.server.js:1:497 at Array.forEach (<anonymous>) at s.findFunction (http://localhost:5070/_framework/blazor.server.js:1:465) at E (http://localhost:5070/_framework/blazor.server.js:1:2611) at http://localhost:5070/_framework/blazor.server.js:1:3499 at new Promise (<anonymous>) at Ft.beginInvokeJSFromDotNet (http://localhost:5070/_framework/blazor.server.js:1:3480) at Ft._invokeClientMethod (http://localhost:5070/_framework/blazor.server.js:1:75072) at Ft._processIncomingData (http://localhost:5070/_framework/blazor.server.js:1:72696) at Ft.connection.onreceive (http://localhost:5070/_framework/blazor.server.js:1:67009) at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args) at EruTipWeb.Pages.SystemPages.TestPage.OnAfterRenderAsync(Boolean firstRender) in T:....\TestPage.razor:line 473 at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Replies (3)
Hi,
Thank you for reaching out to us.
You can try adding your functions to the window scope like this:
window.TestDataTablesAdd = TestDataTablesAdd;
window.TestDataTablesRemove = TestDataTablesRemove; Additionally, ensure that your JavaScript file is correctly loaded on your page.
Regards,
Lauris Stepanovs,
Keenthemes Support Team
Hi,
Glad to hear that. All the best with your project!
Please don't hesitate to reach out if you need anything more from us.
Regards,
Lauris Stepanovs,
Keenthemes Support Team