{"id":27962,"date":"2021-05-21T13:13:33","date_gmt":"2021-05-21T13:13:33","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=27962"},"modified":"2021-09-01T10:02:48","modified_gmt":"2021-09-01T10:02:48","slug":"how-to-sort-dataset-in-pcf-control","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2021\/05\/how-to-sort-dataset-in-pcf-control\/","title":{"rendered":"How to Sort Dataset in PCF Control?"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p style=\"text-align: justify;\">In this blog, we will see how we can sort records in the dataset in PCF control. The dataset object does have the property &#8220;sorting&#8221; which we can use to sort records according to a particular column in ascending or descending order.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-Sort-Dataset-in-PCF-Control.jpeg\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27963 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-Sort-Dataset-in-PCF-Control.jpeg\" alt=\"Sort Dataset in PCF Control\" width=\"1137\" height=\"400\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-Sort-Dataset-in-PCF-Control.jpeg 1137w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-Sort-Dataset-in-PCF-Control-300x106.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-Sort-Dataset-in-PCF-Control-768x270.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-Sort-Dataset-in-PCF-Control-1024x360.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-Sort-Dataset-in-PCF-Control-660x232.jpeg 660w\" sizes=\"(max-width: 1137px) 100vw, 1137px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">To keep dataset designing simple and easy to understand we will be using the React Fluent UI framework&#8217;s &#8220;DetailList-Basic&#8221; control in our example. You can also use simple HTML, Javascript, and CSS for dataset grid as per the design.<\/p>\n<p style=\"text-align: justify;\">As we are using DetailList, the &#8216;onColumnClick&#8217; event has parameter that represents the column which is clicked. On this event, we can write the below code:<\/p>\n<p>\/\/ when a column header is clicked sort the items<br \/>\nconst _onColumnClick = (ev?: React.MouseEvent, column?: any): void =&gt; {<br \/>\nlet dataset = props.pcfContext.parameters.sampleDataSet;<br \/>\nconst newValue: any = {<br \/>\nname: column.fieldName,<br \/>\nsortDirection: column.isSortedDescending ? 0 : 1<br \/>\n}<br \/>\nwhile (dataset.sorting.length &gt; 0) {<br \/>\ndataset.sorting.pop();<br \/>\n}<br \/>\ndataset.sorting.push(newValue);<br \/>\n(dataset.paging as any).loadExactPage(1);<br \/>\ndataset.refresh();<br \/>\nlet isSortedDescending = !column?.isSortedDescending;<br \/>\nsetItems(dataset.sorting);<br \/>\nsetColumns(<br \/>\ncolumns.map(col =&gt; {<br \/>\ncol.isSorted = col.key === column?.key;<br \/>\ncol.isSortedDescending = isSortedDescending;<br \/>\nreturn col;<br \/>\n})<br \/>\n);<br \/>\n}<\/p>\n<p style=\"text-align: justify;\">In the above code, we first need to get the current sorting direction of the selected column after which we can change it in the opposite direction. For example, if the current direction is &#8216;ascending&#8217; then we will change it into &#8216;descending&#8217; and vice versa. For that, we need to check dataset.sorting. To change the current SortStatus of the dataset column, we have to pass the object that contains the name (the name of the column) and sortDirection (the current sort direction of the column).<\/p>\n<p><strong>SORTDIRECTION<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<th>Value<\/th>\n<th>Member<\/th>\n<\/tr>\n<tr>\n<td>-1<\/td>\n<td>None<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>Ascending<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Descending<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: justify;\">After changing the SortStatus, we need to call dataset.refresh(), otherwise, there will be no desired effect.<\/p>\n<p style=\"text-align: justify;\">Also, we have to navigate to the first page if we want that sorting to be applied to the entire dataset instead of applied only for dataset visible on the same page.<\/p>\n<p>dataset.sorting.push(newValue);<br \/>\n(dataset.paging as any).loadExactPage(1);<br \/>\ndataset.refresh();<\/p>\n<h2>Conclusion<\/h2>\n<p style=\"text-align: justify;\">By using Sorting, we can easily sort records in the PCF control dataset for a particular column in ascending\/descending order.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/click-2-clone-microsoft-dynamics-crm-records\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27964 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/c2c-2.jpg\" alt=\"click2clone\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/c2c-2.jpg 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/c2c-2-300x75.jpg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/c2c-2-768x192.jpg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/05\/c2c-2-660x165.jpg 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this blog, we will see how we can sort records in the dataset in PCF control. The dataset object does have the property &#8220;sorting&#8221; which we can use to sort records according to a particular column in ascending or descending order. To keep dataset designing simple and easy to understand we will be\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2021\/05\/how-to-sort-dataset-in-pcf-control\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1929],"tags":[1941,2279],"class_list":["post-27962","post","type-post","status-publish","format-standard","hentry","category-pcf","tag-pcf-control","tag-sort-dataset"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/27962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=27962"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/27962\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=27962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=27962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=27962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}