{"id":25338,"date":"2015-08-10T11:29:41","date_gmt":"2015-08-10T05:59:41","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=25338"},"modified":"2022-01-11T14:40:03","modified_gmt":"2022-01-11T09:10:03","slug":"using-pipeline-in-datatables","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-pipeline-in-datatables\/","title":{"rendered":"Using pipeline in DataTables"},"content":{"rendered":"<p>jQuery DataTables is simple to use as a jQuery plug-in with a huge range of customizable options.<\/p>\n<p>As discussed in a previous <a href=\"http:\/\/www.tothenew.com\/blog\/jquery-datatables-plugin-for-pagination-of-html-tables-2\/\">blog<\/a>, we already know how to draw a basic paginated html table using DataTables.<\/p>\n<p>Another way is to make an ajax call for every page. This causes multiple requests to be made on your server for every dataset.<\/p>\n<p>Using DataTable pipeline, we can reduce these requests by fetching more data than is needed for each dataset.<br \/>\nFor example, If we have 1000 records and we are displaying 10 records on a single page, \u00a0we can configure pipeline to fetch 50 records (data for 5 pages) in a single request. If user navigates to page 6, only then a new request is made to fetch data for next 5 pages (6-10 pages).<\/p>\n<p>Clearly, using pipelines, we can reduce the number of requests on our server.<\/p>\n<p>Let&#8217;s see how it works :<\/p>\n<p>Step 1 : Include <a href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/1.11.3\/jquery.min.js\">jquery.min.js<\/a>, <a href=\"http:\/\/cdn.datatables.net\/1.10.7\/js\/jquery.dataTables.min.js\">jquery.datatables.min.js<\/a> and <a href=\"http:\/\/cdn.datatables.net\/1.10.7\/css\/jquery.dataTables.min.css\">jquery.datatables.min.css<\/a> on your page.<\/p>\n<p>Step2 : Create an empty table structure (with empty table body) :<\/p>\n<p>[java]<br \/>\n&lt;table id=&quot;employeeListing&quot;&gt;<br \/>\n        &lt;thead&gt;<br \/>\n        &lt;tr&gt;<br \/>\n            &lt;td&gt;First Name&lt;\/td&gt;<br \/>\n            &lt;td&gt;Last Name&lt;\/td&gt;<br \/>\n            &lt;td&gt;Email&lt;\/td&gt;<br \/>\n            &lt;td&gt;Age&lt;\/td&gt;<br \/>\n            &lt;td&gt;City&lt;\/td&gt;<br \/>\n            &lt;td&gt;Salary&lt;\/td&gt;<br \/>\n        &lt;\/tr&gt;<br \/>\n        &lt;\/thead&gt;<br \/>\n        &lt;tbody&gt;<br \/>\n        &lt;\/tbody&gt;<br \/>\n&lt;\/table&gt;<br \/>\n[\/java]<\/p>\n<p>Step 3 : Include <a href=\"https:\/\/github.com\/mansi90\/ajaxified-dataTable-demo\/blob\/master\/grails-app\/assets\/javascripts\/ajaxifiedDataTable.js\">this<\/a> js file on your page (This is taken from one of the examples of datatable). Here, in &#8216;enableAjaxifiedDataTable&#8217; function, we are extending datatable to support data pipelining.<\/p>\n<p>Step 4 : Create a new function which intializes datatables for your table :<\/p>\n<p>[java]<br \/>\nfunction drawTable(table, noOfPagesToCache, totalRecords, ajaxUrl) {<br \/>\n    $(table).dataTable({<br \/>\n        &#8216;aoColumnDefs&#8217;: [<br \/>\n            {<br \/>\n                &#8216;bSortable&#8217;: false,<br \/>\n                &#8216;aTargets&#8217;: [&#8216;nosort&#8217;]<br \/>\n            }<br \/>\n        ],<br \/>\n        &quot;lengthMenu&quot;: [<br \/>\n            [10, 25, 50, 100, totalRecords],<br \/>\n            [10, 25, 50, 100, &quot;All&quot;]<br \/>\n        ],<br \/>\n        &quot;serverSide&quot;: true,<br \/>\n        &quot;bProcessing&quot;: true,    \/\/ Display processing text, while an ajax call is being made<br \/>\n        &quot;ajax&quot;: $.fn.dataTable.pipeline({<br \/>\n            url: ajaxUrl,     \/\/specify ajax url<br \/>\n            pages: noOfPagesToCache   \/\/ no of pages you wish to cache<br \/>\n        })<br \/>\n    });<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Step 5 : Invoke the above functions in document.ready :<\/p>\n<p>[java]<br \/>\n$(document).ready(function () {<br \/>\n\tvar totalRecords = 2000; \/\/ total no of records<br \/>\n\tvar ajaxUrl = &quot;localhost:8080\/someUrl&quot; \/\/ url that fetches the data for the table<br \/>\n    enableAjaxifiedDataTable();<br \/>\n    drawTable($(&#8216;#employeeListing&#8217;), 1, totalRecords, ajaxUrl);  \/\/Initializes the datatable<br \/>\n});<br \/>\n[\/java]<\/p>\n<p>When an ajax call is made by datatables it sends various parameters, following are some commonly used parameters :<br \/>\n&#8211; <strong>start<\/strong> \/\/ start index<br \/>\n&#8211; <strong>length<\/strong> \/\/ total no of records to fetch (in one draw)<br \/>\n&#8211; <strong>order[indexOfColumn][dir]=sortOrder<\/strong> \/\/ here indexofColumn means the column-index* which you want to sort, sortOrder would be &#8220;asc&#8221; or &#8220;desc&#8221;<br \/>\n&#8211; <strong>search[value]=searchText<\/strong> \/\/ here, searchText is the text you entered in the search text-box<\/p>\n<p><strong>Note<\/strong> :<br \/>\ncolumn-index : The table columns are zero-indexed, where the first column has 0 index, second has 1st index, and so on&#8230;<br \/>\nIn this case : 0 &#8211; &#8220;FirstName&#8221;, 1- &#8220;LastEmail&#8221;, 2 &#8211; &#8220;Email&#8221;, 3 &#8211; &#8220;Age&#8221;, 4 &#8211; &#8220;City&#8221;, and 5 &#8211; &#8220;Salary&#8221;.<\/p>\n<p>Step 6 : Implement your back-end logic accordingly.<\/p>\n<p>Once you get everything to work, this should help you reduce a considerable number of requests on your server.<\/p>\n<p>For better understanding, I have created a demo <a href=\"https:\/\/github.com\/mansi90\/ajaxified-dataTable-demo\">application<\/a>.<\/p>\n<p>Hope it helps \ud83d\ude42<\/p>\n<p>Please share your queries in the comment section below so that I will get back to you as soon as possible. To know more about what we offer in Grails service, <a title=\"Grails Development | TO THE NEW Digital\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">get in touch with us<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery DataTables is simple to use as a jQuery plug-in with a huge range of customizable options. As discussed in a previous blog, we already know how to draw a basic paginated html table using DataTables. Another way is to make an ajax call for every page. This causes multiple requests to be made on [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7],"tags":[96,1359,27,2186],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25338"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=25338"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25338\/revisions"}],"predecessor-version":[{"id":54563,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25338\/revisions\/54563"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=25338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=25338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=25338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}