Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

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.

protected override async Task OnAfterRenderAsync(bool firstRender)
{
 if (firstRender)
 { 
 await js.InvokeAsync("TestDataTablesAdd", "#kt_datatable_example");  //line 473
 }
}

Javascript File:

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);
 });
}

But when I call the page it gives an error like this

[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 () 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 () 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)

Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)

Deleted comment

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,
It worked this way, thank you.



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


Your Support Matters!

We will highly appreciate your Metronic Review on Themeforest.
Please go to Themeforest Downloads page, select Metronic & leave your feedback.
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(