Pass DataTable id to the controller

Pass DataTable id to the controller

shaniishanii Posts: 5Questions: 2Answers: 0

Hi Allan,

Can you please an help me on this Issue?It's a bug in production and I am in hot water because of that.
I have dataTables with dynamic IDs based on each group, how can I pass(retrieve) the table ID to controller to customize sql query in LINQ?(want to load the data for each user in its dataTable).

here are my codes:

    <div class="accordion-group">
        <div class="accordion-heading">
            <div class="accordion-toggle" data-toggle="collapse" data-target="#@collapseId" id="User_@Html.DisplayFor(itemdata => userName)">
                <strong>@Html.DisplayFor(itemdata => userName)</strong> (@countText)
            </div>
        </div> <!--/accordion heading-->
        <div id="@collapseId" class="collapse">
            <table class="table table-bordered table-striped table-condensed datatable-full-wrapper-manager-grouped" id="@Html.DisplayFor(itemdata => userName)">
                <thead>
                    <tr style="font-weight:bold">
                        <td>Assigned User</td>
                        <td>Priority</td>
                        <td>Record ID</td>
                        <td>Patient</td>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>

        </div><!--/accordion group content-->
    </div><!--/accordion group-->
}  

</div>

Controller:

    internal JsonResult RetrieveUserWorkAssignments(JQueryDataTableParamModel param)
    {
        #region variables

        string search = !string.IsNullOrEmpty(param.sGroup) ? param.sSearch : "";

        var start = param.iDisplayStart;
        int length = param.iDisplayLength;
        string sortColumn = string.Empty;
        string sortColumnDir = string.Empty;
        int recordsTotal = 0;
        int filteredCount = 0;
        JsonResult data;
        List<AllWorkAssignments> workAssignments;
        string[][] aaData;

        #endregion


        workAssignments = _workAssignmentServiceHelper.GetAllWorkForTeam(start, length, sortColumn, sortColumnDir, search);

        if (workAssignments.Count > 0)
        {
            recordsTotal = workAssignments[0].TotalRecords;
            filteredCount = workAssignments[0].RecordsFiltered;
        }
        aaData = workAssignments.Select(
            d =>
                new string[] { d.AssignedUser, d.Priority, d.RecordId, d.Patient })
            .ToArray();



        data = Json(
                new
                {
                    param.sEcho, // Passing the sEcho value back, avoiding redundancy in property name
                    iTotalDisplayRecords = filteredCount,
                    iTotalRecords = recordsTotal,
                    aaData // Passing the aaData value back, avoiding redundancy in property name
                }, JsonRequestBehavior.AllowGet);
        return data;
    }

Answers

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    I don't see your DataTables' initialisation in the above, but you would use ajax.data if you are using ajax to get the data via Ajax. ajax.data lets you add information to send to the server, including the table id if you wanted to.

    Allan

  • shaniishanii Posts: 5Questions: 2Answers: 0

    Thanks Allan, here is the initialization in bootstrap :

    $(document).ready(function () {
    //Setup any datatables in the DOM after document load
    setupDataTables();

    //Setup any datatables in the DOM after ajax calls (partial view loads, etc)    
    $(document).bind("ajaxComplete", function () {
        setupDataTables();
    });
    

    });

    and :

    $(".datatable-full-wrapper-manager-grouped:not(.dataTable)").dataTable({
    "sDom": "<'row-fluid'<'span12't>><'row-fluid'<'span12'p>><'row-fluid'<'span12 dataTables-info-right'i>>",
    "bServerSide": true,
    "sAjaxSource": "LoadAllTeamWorkData",

       // "oSearch": { "sSearch":"??????" },
        "bProcessing": false,
        "bLengthChange ": true,
        "bSearchable":true,
        "sServerMethod": "POST",
        "oLanguage": {
            "sLengthMenu": "_MENU_ records per page",
            "oPaginate": {
                "sPrevious": "",
                "sNext": ""
            },            
        },
    

    for sSearch how can I get the dataTable ID? Is it there when the setup is calling?

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    You are using the legacy options so you would use fnServerParams to add information to the Ajax request.

    Allan

  • shaniishanii Posts: 5Questions: 2Answers: 0

    Right, the problem is retrieving dataTable ID, I am creating each table with an ID, which parameter or object has that ID in legacy options?

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Use table().node(). e.g.:

    table.table().node().id;
    

    Allan

  • shaniishanii Posts: 5Questions: 2Answers: 0

    Thanks a lot!, I am able to get the TableID now.

    How can I grab the DataTable object with its ID in $(".accordion-group").on('show.bs.collapse', function (e)
    event?

    I wanna reDraw it based on a new filter!

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    $('#myId').DataTable() will give you the DataTable object. If you don't know the id, then you could try using a class selector based on whatever class your DataTable is using, or use $.fn.dataTable.tables().

    Allan

This discussion has been closed.