1

Here's the case.

  • I am using ajax call in datatable js to bind json data in my table.
  • Right now I am using directly json file for databinding.
  • Now I want to access the data from my db for which I have written a method inside my controller which returns json value.
  • But I am not able to call this method like I was calling my json file in ajax. Kindly suggest the solution.

    Below are the code sample

    var table = $('#example').DataTable({
            "ajax": "/content/data/dataList.json", //here I want the url of my method.
            "bDestroy": true,
            "iDisplayLength": 15,
            "columns": [
                {
                    "class": 'details-control',
                    "orderable": false,
                    //"data": null,
                    "defaultContent": ''
                },
                { "data": "name" },
            ],
            "order": [[1, 'asc']],
            "fnDrawCallback": function (oSettings) {
                runAllCharts();
            }
        }); 
    

And my method id :

//Controller Name AppDetail
    public string getData(string ddlid)
    {
        DataTable ddl = new DataTable();
        string query = string.Empty;
        if (ddlid == "O1")
        {
            query = "SELECT for O1";
        }
        else if (ddlid == "O2")
        {
            query = "SELECT for O2";
        }
        con.Open();
        MySqlDataAdapter da = new MySqlDataAdapter(query, con);
        da.Fill(ddl);
        con.Close();
        System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        return jSearializer.Serialize(ddl);               
    }

And here is the json data sample

    {
    "data": [
         {
            "name": "Aladdin"
        }  
    ]
}

Kindly Help.

3 Answers 3

1

if you are not using server side processing method get all data first using ajax method and use that data on data table. look at the code below... it might help you for getting some idea.

  $.ajax({
            url: 'api/AppDetail/getData',
            method: 'get',
            data :{ddlid:'01'},          // this is input parameter for your function
            dataType: 'json',
            contentType: 'text/json',
            success: function(res){
                var table=$('#example').dataTable({
                    data: res,
                    columns:[
                           {'data':'name'}
                      ],
                     bDestroy : true,
                iDisplayLength : 15,
                 });  
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

0

If your controller works you can call it before DataTables and insert the data via the data (https://datatables.net/reference/option/data) source of DataTables

Comments

0

On the Controller if u are getting Data which u want, then u can return this Data to a partial view. Note that the partial view is nothing a html table which u bulild upon Razor syntax or anything. Then make ajax call to return this partial view, On success u can apply data table plugin.

<div id=MyTable></div>
$.ajax({
        type: 'GET',
        url: ControllerName/ActionName=partialView Which makes table. 
        success: function (data) {
            debugger;
            $('#MyTable').html(data); //Puting result of ajax call to the  div which containg Id,
            $('#PartilView_Table').DataTable({  // Applying DataTable Plugin table inside partialView                               
                "bProcessing": true,
                "bDeferRender": true,
                "scrollX": true,
                "stateSave": true,
                "bAutoWidth": true,
                "bSort": false,
                "columnDefs": [
                  {

                  }
                ]
              });
      },
      });

Hope this will help you..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.