Search code examples
jqueryjquery-jtable

jquery jtable - How to show newly added record in top row of table


I'm using JTable latest version, JQuery 2.4.0 version.
I create a Jtable table and it works fine
but I want when adding new record using jquery jtable . i want to add that record to the top of the table (latest added item comes to the top most position)
.when page reloads all records must be arrange to the given sorting order.

How can i fix it? This is my html file:

<html lang="en">
    <head>
    <script src="jquery/jquery-2.1.0.js"></script>
    <script src="jquery-ui/ui/minified/jquery-ui.min.js"></script>
    <script src="jtable/jquery.jtable.min.js"></script>
    <link rel="stylesheet" href="jtable/themes/metro/blue/jtable.min.css">
    <script type="text/javascript">
        $(document).ready(function()
        {

            $('#GridTableContainer').jtable(
            {
                title: 'Delivery Grid Table',
                paging: true, //Enable paging
                pageSize: 10, //Set page size (default: 10)
                sorting: true, //Enable sorting
                defaultSorting: 'Name ASC', //Set default sorting
                actions:
                {
                    listAction: function (postData, jtParams)
                    {
                        console.log("Loading from custom function...");
                        return $.Deferred(function ($dfd)
                        {
                            $.ajax({
                                url: 'ajax/grid-ajax.php?action=list&jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting,
                                type: 'POST',
                                dataType: 'json',
                                data: postData,
                                success: function (data)
                                {
                                    $dfd.resolve(data);
                                },
                                error: function ()
                                {
                                    $dfd.reject();
                                }
                            });
                        });
                    },
                    createAction: 'ajax/grid-ajax.php?action=create',
                    updateAction: 'ajax/grid-ajax.php?action=update',
                    deleteAction: 'ajax/grid-ajax.php?action=delete'
                },
                fields: {
                    deliverygrid_id: {
                        key: true,
                        list: false,
                        title: 'id',
                    },
                    area: {
                        title: 'Express',
                        width: '40%'
                    },
                    price: {
                        title: 'Fee ($)',
                        width: '20%'
                    }
                }

            });






        });
        $('#GridTableContainer').jtable('load');
    </script>
  </head>
  <body>
    <div id="GridTableContainer"></div>
  </body>
</html>

Solution

  • Currently you are using default sorting for Name Column
    defaultSorting: 'Name ASC'
    change it to key column
    defaultSorting: 'deliverygrid_id DESC'
    And on server side in list action using following code

    if (jtSorting.Equals("deliverygrid_id DESC"))
    {
        //here get data order by desc record for deliverygrid_id 
    }
    

    When you write defaultSorting: 'deliverygrid_id DESC' it means jtable sends deliverygrid_id DESC string value in jtSorting variable. You can find more info about sorting Here