{"id":4224,"date":"2021-12-13T06:07:51","date_gmt":"2021-12-13T06:07:51","guid":{"rendered":"https:\/\/himbap.com\/blog\/?p=4224"},"modified":"2021-12-13T06:07:51","modified_gmt":"2021-12-13T06:07:51","slug":"creating-aggregated-view-using-datatable","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=4224","title":{"rendered":"Creating aggregated view using DataTable"},"content":{"rendered":"<p><strong>Requirement<\/strong><br \/>\nLet&#8217;s say we want to show aggregated total of the records based on the grouping. let&#8217;s understand this with below example:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/tablev1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-4229 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/tablev1-300x74.png\" alt=\"tablev1\" width=\"300\" height=\"74\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/tablev1-300x74.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/tablev1-1024x253.png 1024w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/tablev1-624x154.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/tablev1.png 1077w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>In above screenshot group 1 as what we have in our entity and 2 is what we want to show on the dashboard, just want to show single entry for the duplicate record and calculate total.<\/p>\n<p><strong>Details<\/strong>:<br \/>\nI am going to use <a href=\"https:\/\/datatables.net\/\">data table<\/a> here to show this data on dashboard. We need to create HTML web resource<br \/>\nusing any html editor. First we need to add reference for the cdn and script in our web resource.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script src=&quot;ClientGlobalContext.js.aspx&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/cdn.datatables.net\/1.10.16\/css\/jquery.dataTables.min.css&quot;&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/cdn.datatables.net\/select\/1.2.5\/css\/select.dataTables.min.css&quot;&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap.min.css&quot;&gt;\r\n    &lt;script src=&quot;https:\/\/code.jquery.com\/jquery-1.12.4.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;https:\/\/cdn.datatables.net\/1.10.16\/js\/jquery.dataTables.min.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>Next we need to fetch data from CRM based on our query<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction LoadVendorGrid() {\r\n            debugger;\r\n            var query = &quot;?$apply=groupby((new_name),aggregate(new_amount with sum as total))&amp;$filter=new_amount gt 0 and new_name ne null&quot;;\r\n            window.parent.Xrm.WebApi.retrieveMultipleRecords(&quot;new_vendor&quot;, query).then(\r\n                function success(results) {\r\n                    var result = results.entities;\r\n                    if (result != null) {\r\n                        $('#vendordt').DataTable({\r\n                            &quot;bInfo&quot;: false,\r\n                            data: result,\r\n                            columns: [\r\n                                { data: 'new_name' },\r\n                                { data: 'total', render: $.fn.dataTable.render.number(',', '.', 0, '\u20b9') }\r\n                            ]\r\n                        });\r\n                    }\r\n\r\n                },\r\n                function (error) {\r\n                    console.log(error.message);\r\n\r\n                }\r\n            )\r\n        }\r\n<\/pre>\n<p>In above code I am fetching details from the vendor entity and grouping data based on the name as well as checking condition to render records where amount is &gt;0 and name is not null to avoid and error in data table rendering. Once we got result we can pass our fields to the data table column like above. Finally we need to make call to this method. Here is the full code of the html web resource.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;script src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.5.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;https:\/\/cdn.datatables.net\/1.11.3\/css\/jquery.dataTables.min.css&quot;&gt;\r\n    &lt;script type=&quot;text\/javascript&quot; charset=&quot;utf8&quot; src=&quot;https:\/\/cdn.datatables.net\/1.11.3\/js\/jquery.dataTables.min.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;ClientGlobalContext.js.aspx&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n    &lt;meta charset=&quot;utf-8&quot;&gt;\r\n    &lt;title&gt;&lt;\/title&gt;\r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n        $(document).ready(function () {\r\n            LoadVendorGrid();\r\n        });\r\n        function LoadVendorGrid() {\r\n            debugger;\r\n            var query = &quot;?$apply=groupby((new_name),aggregate(new_amount with sum as total))&amp;$filter=new_amount gt 0 and new_name ne null&quot;;\r\n            window.parent.Xrm.WebApi.retrieveMultipleRecords(&quot;new_vendor&quot;, query).then(\r\n                function success(results) {\r\n                    var result = results.entities;\r\n                    if (result != null) {\r\n                        $('#vendordt').DataTable({\r\n                            &quot;bInfo&quot;: false,\r\n                            data: result,\r\n                            columns: [\r\n                                { data: 'new_name' },\r\n                                { data: 'total', render: $.fn.dataTable.render.number(',', '.', 0, '\u20b9') }\r\n                            ]\r\n                        });\r\n                    }\r\n\r\n                },\r\n                function (error) {\r\n                    console.log(error.message);\r\n\r\n                }\r\n            )\r\n        }\r\n    &lt;\/script&gt;\r\n   &lt;\/head&gt;\r\n&lt;body onfocusout=&quot;parent.setEmailRange();&quot; style=&quot;overflow-wrap: break-word;&quot;&gt;\r\n\r\n&lt;table id=&quot;vendordt&quot; class=&quot;cell-border&quot;&gt;\r\n\r\n&lt;thead&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;th&gt;Vendor Name&lt;\/th&gt;\r\n\r\n\r\n&lt;th&gt;Amout&lt;\/th&gt;\r\n\r\n            &lt;\/tr&gt;\r\n\r\n        &lt;\/thead&gt;\r\n\r\n    &lt;\/table&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Now we can place this code in html web resource and place it to the dashboard. While rendering it should look like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/vendorgrid.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-4230 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/vendorgrid-300x292.png\" alt=\"vendorgrid\" width=\"300\" height=\"292\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/vendorgrid-300x292.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2021\/12\/vendorgrid.png 470w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This is how we can use data table and use it&#8217;s out of the box search, sorting and paging features.<\/p>\n<p>Hope it will help someone !!<br \/>\n<strong>Keep learning and Keep Sharing !! <\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement Let&#8217;s say we want to show aggregated total of the records based on the grouping. let&#8217;s understand this with below example: In above screenshot group 1 as what we have in our entity and 2 is what we want to show on the dashboard, just want to show single entry for the duplicate record and calculate total. Details: I&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=4224\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":4230,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,402,522,492],"tags":[924,923,922,928,925,926,929,927,921],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4224"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4224"}],"version-history":[{"count":5,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4224\/revisions"}],"predecessor-version":[{"id":4233,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4224\/revisions\/4233"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/4230"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}