The jquery script function is not triggering in _layout.cshtml page while child page is loaded in ajax

40 Views Asked by At

I solicit your valuable input to resolve an issue.

Context: My application is having asp.net core MVC as tech stack. I have put client-side sorting and various functionalities in jquery. To make those applicable to all pages, I put those functions in _layout.cshtml page. At the time of page load, it was running fine. But when a search result(throuh ajax) populates result set in underlying modules. The jquery functions in _layout pages is not calling.

The code sample in _layout.cshtml page looks like as follows

<script>
    jQuery(document).ready(function ($) {
        // Use jQuery noConflict to avoid conflicts
        $.noConflict();

        // Make table columns resizable
        $("#mytable th").resizable({
            handles: "e", // Only allow resizing from the right edge
            minWidth: 50, // Minimum column width
            
        });
    });
</script>

The mytable is id of the table that contains search result in each .cshtml page. Hence, if I have N number of .cshtml pages which are having N number of mytable and one _layout.cshtml. To make those client side functions generic I put those in _layout page.

The code sample for populating the mytable in each page is different but for an example it goes like this

 $(document).on('click', '#btnProjects', function () {
   
    $.ajax({
        type: 'GET',
        url: '/Project/GetProjects?Factoryid=' + fId,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data.length > 0) {
                $('.table').html(data); 
                getPersistentColumn();
            }.....
    })

Problem statement: The below code is not responding in _lauout.cshtml page inside jQuery(document).ready while search result is populated through ajax

$("#fixTable th").resizable({handles: "e", minWidth: 50, // Minimum column width});

Thanks and regards Nilanjan Saha

1

There are 1 best solutions below

4
JuanR On

Assuming this line of code targets the container for table #mytable:

$('.table').html(data); 

And that data contains the HTML for table #mytable...

You are wiping out the existing table so you need to make the new header cells resizable once you recreate it with the contents from data. Call the code to do so again, right after you have replaced the HTML:

$(document).on('click', '#btnProjects', function () {
   
    $.ajax({
        type: 'GET',
        url: '/Project/GetProjects?Factoryid=' + fId,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data.length > 0) {
                $('.table').html(data);

                // Make table columns resizable
                $("#mytable th").resizable({
                            handles: "e", // Only allow resizing from the right edge
                            minWidth: 50, // Minimum column width
                            
                        });
                 
                getPersistentColumn();
            }.....
    })