Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6990175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:18:46+00:00 2026-05-27T19:18:46+00:00

What I want to do is to modify the sAjaxSource of the datatable if

  • 0

What I want to do is to modify the sAjaxSource of the datatable if a value in combo box changes and then I want to call the fnDraw() function of the datatable

Datatable is:

 $("#example").dataTable({
             "aoColumns": [
                     { "sTitle": "Id" },
                      { "sTitle": "Name" }],


             "bProcessing": true,
             "bServerSide": true,

             "sAjaxSource": '@Url.Action("FetchData", "Home")',
             "sPaginationType": "full_numbers",

});

What I have so far is:
C# code:

public JsonResult GetData(DataTableParameters param, int? SelectedId)
 {
//return the results
}

And the javascript code for change of value is:

    $('#SelectedId').change(function () {
                alert("Hi");
                $("#example").dataTable(
                    {sAjaxSource:"/home/FetchData?SelectedId=1"}).fnDraw(true); ;
            });

It reaches alert(“Hi”) point but it doesn’t redraw the table. How do I get it to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:18:47+00:00Added an answer on May 27, 2026 at 7:18 pm

    Explanation

    To use the API for datatables you need to have a handle first.
    The .dataTable function returns a handle to the datatable created.
    So, doing this

    var oTable = $("#example").dataTable({
        ...
    });
    
    oTable.fnDraw();
    

    should allow you to access and execute the functions for a specific table.


    Information

    Datatables doesn’t support changing the settings after it’s been initialized, and for a good reason.

    //This does *NOT* work.
    var oTable = $("#example").dataTable({
        ...
    });
    var oSettings = oTable.fnSettings();
    oSettings.sAjaxSource = "some_url";
    //Nothing will happen
    oTable.Draw();
    

    However, you could try using the fnServerData function to intercept the request before it is sent, and then just update the table with your own url whenever the change occur.
    Or
    You could destroy the table and reinitialize it.

    To learn more about fnServerData, click here and search for “fnServerData”.


    Solution

    I’m unsure whether or not this actually works, I haven’t done it before, but it should work.

    var currentSource = "this_url";
    var oTable = $('#example').dataTable( {
        "bServerSide": true,
        "bProcessing": true,
        "aoColumns": [
            {"sTitle": "id"},
            {"sTitle": "name"}
        ],
        "sPaginationType": "full_numbers",
        "sAjaxSource": currentSource,
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax( {
                "dataType": 'json',
                "type": "POST",
                "url": currentSource,
                "data": aoData,
                "success": fnCallback
            });
        }
    });
    
    $("#SelectedId").change(function)(){
        currentSource = "new_url";
        oTable.fnDraw(); //or fnReloadAjax()
    });
    

    Alternative Solution

    An alternative would be to destroy the table and then reinitialize it with the new settings. This is, however, a very ineffective way of handling it.

    var initParams = {
        "bServerSide": true,
        "bProcessing": true,
        "aoColumns": [
            {"sTitle": "id"},
            {"sTitle": "name"}
        ],
        "sPaginationType": "full_numbers",
        "sAjaxSource": "this_url",
    };
    var oTable = $('#example').dataTable(initParams);
    
    $("#SelectedId").change(function)(){
        oTable.fnDestroy();
        initParams.sAjaxSource = "new_url";
        oTable = $('#example').dataTable(initParams);
    });
    

    Sidenote

    When you’ve got bServerSide = true you have to take care of everything, and that means it complicates everything too!

    Happy coding! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to modify my function that strips the tag from the value of
I want to modify globals declared in java SDK via an NDK C function,
I want to modify a local variable in a function of extension method. See
I want to modify the following extract numbers function below to check if the
I want to modify this code so that the value is a variable and
I want to modify xml attribute using objective c on some action for example,
I want to modify through the dom various element properties and then save that
I want to modify say MediaRecorder class for example. And I want to show
I want to modify an recursive function from an 'ternary search tree' library (
I want to modify Mathematica's Interpolation[] function (in 1 dimension) by replacing extrapolation with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.