Calculate page and row index of an dynamically added row while serveside processing

Calculate page and row index of an dynamically added row while serveside processing

jan-007jan-007 Posts: 4Questions: 2Answers: 0

When a row is added into a table, it shall be added and highlighted on the apppropriate page and row (page / row index) according to the current table ordering.

While the client side processing works as expected: https://jsfiddle.net/fegeb01b/23/
the same code does not work with the serverside processing.

How can be the serverside processing be implemented ?

Answers

  • jan-007jan-007 Posts: 4Questions: 2Answers: 0

    A possible solution for the Problem described above.

    The page calculation must be implemented on the server side, like this (c#, asp.net):

            public IActionResult GetPageNumber(string userId, int pageLen)
            {
                int pageNum = 0;
                int rowCount = 0;
                var users = _repo.Users;
    
                foreach(var user in users)
                {
                    if (user.Id == userId)
                    {
                        if(rowCount > pageLen )
                        {
                            pageNum = rowCount / pageLen;
                            break;
                        }
                    }
                    rowCount++;
                }
                return Json(new { respType = "data", success = true, message = "", data = new { PageNum = pageNum } });
            }
    

    The Client side that adds a new row in to the table and calls the DisplayAddedRecord() function:

        function AddUser() {
            $.ajax({
                type: "POST",
                url: "/Test/AddUser",
                data: {
                    userId: $('#TUsers :selected').val()
                }
            }).done(function (result) {
                if (result) {
                        _g_rowId = result.data.id;
                        var rowAdded = table.row.add({
                            "id": result.data.id,
                            "userName": result.data.userName
                        });
                        DisplayAddedRecord(result.data.id, table.page.len());
                }
            }).fail(function (xhr, textStatus, errorThrown) {
    ...
            });
        }
    
    

    The DisplayAddedRecord() function gets the calculated page from the server and redraws the (appropriate / calculated) page:

      function DisplayAddedRecord()
        {
            $.ajax({
                type: "GET",
                url: "/Test/GetPageNumber",
                data: {
                    userId: $('#Users :selected').val(),
                    pageLen: table.page.len()
                }
            }).done(function (result) {
                if (result) {
                        table.page(result.data.pageNum).draw(false);
                }
            }).fail(function (xhr, textStatus, errorThrown) {
    ...
            });
        }
    
    

    And finaly the selection / highlighting of the added row in the "rowCallback" function:

     table = $('#TestTable').DataTable({
            "serverSide": true,
            select: true,
            "dataType": "JSON",
            "ajax": {
                "url": "/Test/GetUsers" ...
                }
            },
            "type": "GET",
            "rowCallback": function( row, data ) {
                if (data.id === _g_rowId)
                {
                    $(row).addClass('selected');
                }
            }...
    
    
    
This discussion has been closed.