Allow server-side caching in `fnServerData` in Dat

2019-07-21 11:22发布

By default Ajax calls in DataTables avoid caching by adding a bogus field onto the url, e.g. http://yoursite.com/api/?_348522852542'. I would like to disable this. It appears that the ajax functions call fnServerData which is defined something like this...

    "fnServerData": function ( sUrl, aoData, fnCallback, oSettings ) {
        oSettings.jqXHR = $.ajax( {
            "url":  sUrl,
            ....
            "cache": false,
            ....
            });
      };

I want the value for "cache" to be true rather than false. Am I missing something? Why is this hard-coded in like this? Is the best way to do make this change to edit the source either locally or by submitting a patch?

2条回答
霸刀☆藐视天下
2楼-- · 2019-07-21 11:54

You can override the fnServerData callback: http://datatables.net/usage/callbacks#fnServerData

查看更多
放荡不羁爱自由
3楼-- · 2019-07-21 12:00

This actually helped me to solve my problem when already cached data (in redis) was re-cached by datatables (1.10.10), here's my code (it'll refresh the data as well and since it's cached it's super fast):

<script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
            var table = $('#issues-table').DataTable( {
                processing : true,
                ajax: {
                        url: '/jira',
                        dataSrc: 'issues',
                        cache: true
                },
                order: [ 0, 'desc' ],
                fnRowCallback: function( nRow, aData, iDisplayIndex ) {
                    $('td:eq(0)', nRow).html('<a href="http://jira.com/browse/' + aData[0] + '" target="_blank">' +
                        aData[0] + '</a>');
                    return nRow;
                },
            } );
            setInterval( function () {
                table.ajax.reload( null, false ); // user paging is not reset on reload
            }, 30000 );
        } );
</script>
查看更多
登录 后发表回答