none
Custom List - Date & Time Column - Column Header Filter RRS feed

  • Question

  • Hi All,

    I have an issue that I am hoping there is a simple resolution to.

    I've created a custom list which includes a date based 'Week Commencing' column. The list view has been set to sort items in this column in descending order (see below). 

    Column View    

    Is there a way to set a similar 'descending' sort on the column header filter? As it stands this still displays items in ascending order (see below). This will become frustrating for users as the list of dates increases, as they will need to scroll down an increasingly long list to find the most recent date.  

    Filter View  

    Thanks in advance

    Saturday, August 24, 2019 10:06 AM

Answers

  • Hi Matty,

    You could add a paseUKDate function in the script.

    function  parseUKDate(str) { // parse UK format dd/mm/yyyy
            var arr = str.split('/');
            var date = new Date(arr[2], arr[1] - 1, arr[0]);
            return date.getTime();
        }



    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(function () {                 
                    FilterOMenu=CustomFilterOMenu;
    }, "core.js");
    
    function CustomFilterOMenu(c, a) {
         function  parseUKDate(str) { // parse UK format dd/mm/yyyy
            var arr = str.split('/');
            var date = new Date(arr[2], arr[1] - 1, arr[0]);
            return date.getTime();
        }
        //Be carefull with overriding SharePoint core functions, for now this will work (September 2016 CU). Ensure you test this every time after installing a SharePoint update.
        //SharePoint default (I placed this above, cause it returns.)
        if (a == null)
            return;
        var b = a.tagName == "DIV" ? a.parentNode : a;
        //End SharePoint default
    
        //Custom implementation to sort the filter
        var fieldInternalName = a.getAttribute("name");
    
        //some sort functions
        var ascComparer = function(a,b){return a.text<b.text ? -1:a.text>b.text ? 1 : 0;};
        var descComparer = function(a,b){return a.text>b.text ? -1:a.text<b.text ? 1 : 0;};
        var ascDateComparer = function(a,b){d1 = Date.parse(a.text);d2 = Date.parse(b.text);return d1<d2 ? -1:d1>d2 ? 1 : 0;};
        var descDateComparer = function(a,b){d1 =parseUKDate(a.text);d2 = parseUKDate(b.text);return d1>d2 ? -1:d1<d2 ? 1 : 0;};
    
        var myCustomSort = {};
        //Add the field internal name and give a comparer as value to sort
        //As example the title column as your case is
       // myCustomSort["Test"] = descComparer;
    myCustomSort["WEEK_x0020_COMMENCING"] = descDateComparer;
        //myCustomSort["LinkTitle"] = descComparer;
    
    
        if(typeof c != "undefined" && c != null && typeof myCustomSort[fieldInternalName] != "undefined"){//check if you implemented a custom sort for the current internalname
            //select all items that are checkable (the options)
            var allSelectableItems = c.querySelectorAll('[checked]');
            if(allSelectableItems.length > 0){
                var elementInnerhtmls = [];
                var htmlToReplace = "";
                var htmlToAppend = "";
                for(var i = 0;i<allSelectableItems.length;i++)
                {
                    elementInnerhtmls.push({text: allSelectableItems[i].getAttribute("text"),html:allSelectableItems[i].outerHTML});
                    htmlToReplace += allSelectableItems[i].outerHTML; 
                }
                elementInnerhtmls = elementInnerhtmls.sort(myCustomSort[fieldInternalName]);
                for(var i = 0;i<elementInnerhtmls.length;i++)
                {
                    htmlToAppend += elementInnerhtmls[i].html;
                }
                //replace the original html with the sorted html
                c.innerHTML = c.innerHTML.replace(htmlToReplace,htmlToAppend);
            }
        }
        //SharePoint default
        OMenu(c, b, null, null, -1)
        //End SharePoint default
    } 
    </script>

    Result:

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Matty9999 Wednesday, August 28, 2019 6:48 AM
    Wednesday, August 28, 2019 6:28 AM

All replies

  • Hi Matty,

    It is by default that the order of filter is ascending. As far as I know, there is no OOB ways to change the order of filter.

    You could use custom script to achieve this. Here is a similar post for your reference:

    https://sharepoint.stackexchange.com/questions/137084/how-to-change-the-order-of-filter-in-reverse-order

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, August 26, 2019 6:25 AM
  • Hi Michael,

    Thanks for replying. I appreciate it.

    I've tried to use the snippet from the previous thread, but I'm a bit of a novice coder and can't get it to work on my list unfortunately.

    Which elements of that snippet would I need to change to reflect my list/column?

    Cheers  

    Monday, August 26, 2019 1:54 PM
  • Hi Matty,

    Try this script:

    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(function () {                 
                    FilterOMenu=CustomFilterOMenu;
    }, "core.js");
    function CustomFilterOMenu(c, a) {
        //Be carefull with overriding SharePoint core functions, for now this will work (September 2016 CU). Ensure you test this every time after installing a SharePoint update.
        //SharePoint default (I placed this above, cause it returns.)
        if (a == null)
            return;
        var b = a.tagName == "DIV" ? a.parentNode : a;
        //End SharePoint default
    
        //Custom implementation to sort the filter
        var fieldInternalName = a.getAttribute("name");
    
        //some sort functions
        var ascComparer = function(a,b){return a.text<b.text ? -1:a.text>b.text ? 1 : 0;};
        var descComparer = function(a,b){return a.text>b.text ? -1:a.text<b.text ? 1 : 0;};
        var ascDateComparer = function(a,b){d1 = Date.parse(a.text);d2 = Date.parse(b.text);return d1<d2 ? -1:d1>d2 ? 1 : 0;};
        var descDateComparer = function(a,b){d1 = Date.parse(a.text);d2 = Date.parse(b.text);return d1>d2 ? -1:d1<d2 ? 1 : 0;};
    
        var myCustomSort = {};
        //Add the field internal name and give a comparer as value to sort
        //As example the title column as your case is
       // myCustomSort["Test"] = descComparer;
    myCustomSort["Week_x0020_Commencing"] = descDateComparer;
        //myCustomSort["LinkTitle"] = descComparer;
    
    
        if(typeof c != "undefined" && c != null && typeof myCustomSort[fieldInternalName] != "undefined"){//check if you implemented a custom sort for the current internalname
            //select all items that are checkable (the options)
            var allSelectableItems = c.querySelectorAll('[checked]');
            if(allSelectableItems.length > 0){
                var elementInnerhtmls = [];
                var htmlToReplace = "";
                var htmlToAppend = "";
                for(var i = 0;i<allSelectableItems.length;i++)
                {
                    elementInnerhtmls.push({text: allSelectableItems[i].getAttribute("text"),html:allSelectableItems[i].outerHTML});
                    htmlToReplace += allSelectableItems[i].outerHTML; 
                }
                elementInnerhtmls = elementInnerhtmls.sort(myCustomSort[fieldInternalName]);
                for(var i = 0;i<elementInnerhtmls.length;i++)
                {
                    htmlToAppend += elementInnerhtmls[i].html;
                }
                //replace the original html with the sorted html
                c.innerHTML = c.innerHTML.replace(htmlToReplace,htmlToAppend);
            }
        }
        //SharePoint default
        OMenu(c, b, null, null, -1)
        //End SharePoint default
    } 
    </script>
    

    Test result:

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, August 27, 2019 9:46 AM
  • Hi Michael,

    Thanks for the update. Much appreciated.

    Your screenshot is exactly what I'm looking to achieve, but when I add your code snippet to a script editor I'm not having the same result. My filter list is still in ascending order unfortunately.

    Any idea why that might be? Is the fact that I'm on SP2013 an issue?

       

    Tuesday, August 27, 2019 9:58 AM
  • Hi,

    I think you may used the wrong field internal name in the script. What's the field internal name of the 'Week Commencing' column? You could check it in the url of the column setting.

    Then change it in the script:

    myCustomSort["WEEK_x0020_COMMENCING"] = descDateComparer;

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, August 28, 2019 1:17 AM
  • Thanks Michael. That's really helpful.

    I'd forgotten that at some point I'd changed the column name from 'Date' to 'Week Commencing' and that the internal field wouldn't have changed.

    Updating the internal field name in the code has helped to display the filter dates in descending order. However there is still one slight problem - because I'm in the UK, the dd/mm/yyyy format is resulting in the dates being sorted in descending order by 'day'. For example, 26/08/2019 is appearing appear above 02/09/2019 in the list (see below).

      

    Is it possible for the code be tweaked to recognise the UK date format?

    Thanks again for your help. 


    • Edited by Matty9999 Wednesday, August 28, 2019 5:48 AM
    Wednesday, August 28, 2019 5:40 AM
  • Hi Matty,

    You could add a paseUKDate function in the script.

    function  parseUKDate(str) { // parse UK format dd/mm/yyyy
            var arr = str.split('/');
            var date = new Date(arr[2], arr[1] - 1, arr[0]);
            return date.getTime();
        }



    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(function () {                 
                    FilterOMenu=CustomFilterOMenu;
    }, "core.js");
    
    function CustomFilterOMenu(c, a) {
         function  parseUKDate(str) { // parse UK format dd/mm/yyyy
            var arr = str.split('/');
            var date = new Date(arr[2], arr[1] - 1, arr[0]);
            return date.getTime();
        }
        //Be carefull with overriding SharePoint core functions, for now this will work (September 2016 CU). Ensure you test this every time after installing a SharePoint update.
        //SharePoint default (I placed this above, cause it returns.)
        if (a == null)
            return;
        var b = a.tagName == "DIV" ? a.parentNode : a;
        //End SharePoint default
    
        //Custom implementation to sort the filter
        var fieldInternalName = a.getAttribute("name");
    
        //some sort functions
        var ascComparer = function(a,b){return a.text<b.text ? -1:a.text>b.text ? 1 : 0;};
        var descComparer = function(a,b){return a.text>b.text ? -1:a.text<b.text ? 1 : 0;};
        var ascDateComparer = function(a,b){d1 = Date.parse(a.text);d2 = Date.parse(b.text);return d1<d2 ? -1:d1>d2 ? 1 : 0;};
        var descDateComparer = function(a,b){d1 =parseUKDate(a.text);d2 = parseUKDate(b.text);return d1>d2 ? -1:d1<d2 ? 1 : 0;};
    
        var myCustomSort = {};
        //Add the field internal name and give a comparer as value to sort
        //As example the title column as your case is
       // myCustomSort["Test"] = descComparer;
    myCustomSort["WEEK_x0020_COMMENCING"] = descDateComparer;
        //myCustomSort["LinkTitle"] = descComparer;
    
    
        if(typeof c != "undefined" && c != null && typeof myCustomSort[fieldInternalName] != "undefined"){//check if you implemented a custom sort for the current internalname
            //select all items that are checkable (the options)
            var allSelectableItems = c.querySelectorAll('[checked]');
            if(allSelectableItems.length > 0){
                var elementInnerhtmls = [];
                var htmlToReplace = "";
                var htmlToAppend = "";
                for(var i = 0;i<allSelectableItems.length;i++)
                {
                    elementInnerhtmls.push({text: allSelectableItems[i].getAttribute("text"),html:allSelectableItems[i].outerHTML});
                    htmlToReplace += allSelectableItems[i].outerHTML; 
                }
                elementInnerhtmls = elementInnerhtmls.sort(myCustomSort[fieldInternalName]);
                for(var i = 0;i<elementInnerhtmls.length;i++)
                {
                    htmlToAppend += elementInnerhtmls[i].html;
                }
                //replace the original html with the sorted html
                c.innerHTML = c.innerHTML.replace(htmlToReplace,htmlToAppend);
            }
        }
        //SharePoint default
        OMenu(c, b, null, null, -1)
        //End SharePoint default
    } 
    </script>

    Result:

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Matty9999 Wednesday, August 28, 2019 6:48 AM
    Wednesday, August 28, 2019 6:28 AM
  • That has worked perfectly.

    Thanks so much for your help (and patience with me!) over the past few days Michael. It has been very much appreciated.

    Wednesday, August 28, 2019 6:48 AM