0

I want to bind data to a table using jQuery DataTables.

I am returning the data using action result and converting it to JSON in view, data is not displaying in the table, only blank columns are shown.

Here is the view part

  @model Guardian.Core.Model.Models.PatientMedicationsCollection
    @{
        var ModelJsonData = Newtonsoft.Json.JsonConvert.SerializeObject(Model);
        Layout = null;
    }

    <script type="text/javascript">

        var MedicationMasterData = JSON.parse(@Html.Raw(Json.Encode(ModelJsonData)));
        var CCDAChecked;
        var LatestCCDA;
        var AllCCDA;
        var CLaimsChecked;
        var AllClaims;
        var Last6Month;
        var OneYear;
        var FilteredData = [];

        $(document).ready(function () {
            debugger;

            $.each(MedicationMasterData, function (idx, obj) {
                FilteredData.push(this);

            });
            FilterTable(MedicationMasterData);
});
        function FilterTable(FilteredData) {
            debugger;
            FData = JSON.stringify(FilteredData)
            $('#PatientMedications').DataTable({
                "scrollX": 100,
                "paging": true,
                "pagingType": "full_numbers",
                "dom": '<"toolbar">frtip',
                "aaData": FData,

                columns: [
                    { title: "PatientID" },
                    { title: "FirstName" },
                    { title: "LastName" },
                    { title: "DOB." },
                    { title: "Gender" },
                    { title: "Source" },
                    { title: "SourceCreatedDate" },
                    { title: "SourceKeyID" },
                    { title: "LatestCCDA" },
                    { title: "Code" },
                    { title: "CodeType" },
                    { title: "DrugName" },
                    { title: "DrugDecriptionsM" },
                    { title: "StartDate" },
                    { title: "StopDate" },
                    { title: "Instructions" },
                    { title: "Status" },
                    { title: "DoseQuantity" },
                    { title: "RateQuantity" },
                    { title: "RateQuantityUnit" },
                    { title: "DrugDecriptionsM" },
                    { title: "ClaimLineFromDate" },
                    { title: "DaysSupply" },
                    { title: "DOSAGEFORMNAME" },
                    { title: "ROUTENAME" },
                    { title: "ST" }

                ]

            });

Controler Part where we are returning the data using action result

public ActionResult PatientMedications(int patientID)
            {
                return PartialView(patientrepository.PatientMedications(patientID));
            }
2
  • A couple of questions - Why are you returning a 'PartialView'? Why don't you return a json object instead of doing all that client-side processing Use return Json(new { aaData= Commented Oct 6, 2015 at 11:23
  • I did not catch your question clearly but i guess your column name should match. Then it will show the data Commented Oct 6, 2015 at 13:14

3 Answers 3

1
$('#PatientMedications').dataTable({
  "aaData": data,   //this is your JSON object, which is what is showing in your post above with console.log(data)
  "aoColumns": [{
    "mDataProp": "PatientID"
  }, {
    "mDataProp": "FirstName"
  }]
});
Sign up to request clarification or add additional context in comments.

Comments

0
  • Use columns.data option instead of columns.title to define data source for the column from the row's data object.

  • Do not pass JSON string to aaData, array is expected instead. Remove line with JSON.stringify().

  • Use data option name instead of retired aaData. I assume that FilteredData is an array of objects.

$('#PatientMedications').DataTable({
    "scrollX": 100,
    "pagingType": "full_numbers",
    "dom": '<"toolbar">frtip',
    "data": FilteredData,
    "columns": [
        { data: "PatientID" },
        { data: "FirstName" },
        { data: "LastName" },
        { data: "DOB." },
        { data: "Gender" },
        { data: "Source" },
        { data: "SourceCreatedDate" },
        { data: "SourceKeyID" },
        { data: "LatestCCDA" },
        { data: "Code" },
        { data: "CodeType" },
        { data: "DrugName" },
        { data: "DrugDecriptionsM" },
        { data: "StartDate" },
        { data: "StopDate" },
        { data: "Instructions" },
        { data: "Status" },
        { data: "DoseQuantity" },
        { data: "RateQuantity" },
        { data: "RateQuantityUnit" },
        { data: "DrugDecriptionsM" },
        { data: "ClaimLineFromDate" },
        { data: "DaysSupply" },
        { data: "DOSAGEFORMNAME" },
        { data: "ROUTENAME" },
        { data: "ST" }
    ]
});

3 Comments

Yes it array of object but if we use data it will not work so I used aaData and mData and its working fine
@AdeshGaonkar, what DataTables version are you using?
We are using DataTables of version 1.10.4
0

I used aaData and mData and its working fine

 function FilterTable(FilteredData) {

        $('#PatientMedications').DataTable({
            "bDestroy": true,
            "bRetrieve": true,
            "scrollX": 100,
            "paging": true,
            "pagingType": "full_numbers",
            "dom": '<"toolbar">frtip',
            "aaData": FilteredData,

            "aoColumns": [
                { "mData": "PatientID" },
                { "mData": "FirstName" },
                { "mData": "LastName" },
                { "mData": "DateOfBirth" },
                { "mData": "Gender" },
                { "mData": "Source" },
                { "mData": "SourceCreatedDate" },
                { "mData": "SourceKeyID" },
                { "mData": "LatestCCDA" },
                { "mData": "Code" },
                { "mData": "CodeType" },
                { "mData": "DrugName" },
                { "mData": "Drug Descriptions" },
                { "mData": "StartDate" },
                { "mData": "StopDate" },
                { "mData": "Instructions" },
                { "mData": "Status" },
                { "mData": "DoseQuantity" },
                { "mData": "RateQuantity" },
                { "mData": "RateQuantityUnit" },
                { "mData": "ClaimLineFromDate" },
                { "mData": "QuantityDispensed" },
                { "mData": "DaysSupply" },
                { "mData": "DosageFormName" },
                { "mData": "ROUTENAME" },
                { "mData": "ST" }

            ]

        });

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.