This article includes both 2010 and 2013 examples.
Let's say you wanted to display a Date/Time down to the minute or second, but you wanted the column heading to filter by days or months. By default the column filter dropdown displays one each of what ever it finds in the column, or in the case of dates, the nearest day.
But if you want just the months or years in the dropdown, then maybe something like this:
It's not to hard to do in SharePoint 2010, and fairly easy in SharePoint 2013
In SharePoint 2010 we still have a fully working SharePoint Designer that can easily edit web parts. In SharePoint 2013, Designer is pretty much broken for web part work, which is just as well as we should be looking at the newest tools in 2013 for our customizations. In this example we will be using JS Link for 2013.
Steps for SharePoint 2010
The basic steps:
- Create a new list named "Event Announcements".
- Add a Date and Time column named "EventDate".
- Add a Calculated column named "Event Date" to display a YYYY-MM version of the date that can be used to filter by month.
- Use SharePoint Designer to display the full date in the calculated column by hand customizing the XSLT.
Note: Although the names of my columns differ only by a space, you can use any column names you like. I will use EventDate for data entry and Event Date for display.
Steps:
- Go to your site and create a test list. For this example an Announcements list would be a good choice.
- Create a new column named "EventDate". (You will enter your date data here.)
- In the List ribbon click Create Column.
- Enter "EventDate" as the name.
- Set the type to Date and Time.
- Set Date and Time Format to Date & Time.
- Click OK.
- Create a new column named "Event Date". (This column will control the filter grouping.)
- In the List ribbon click Create Column.
- Enter "Event Date" as the name.
- Set the type to Calculated.
- Enter this formula:
=TEXT([EventDate],"yyyy-mm")
or for years instead of months:
=TEXT([EventDate],"yyyy") - Set The data type returned from this formula to Single line of text.
- Click OK.
- Add some sample data with dates spread across two or three different months.
- Open SharePoint Designer 2010 and open your site.
- Click Lists and Libraries and click your list.
- Find and click your view (I used All Items).
- If the screen is not in Split view click Split at the bottom of the screen.
- In the Code view pane click in the web part code. (The code without the yellow background.)
- In the List View Tools / Design ribbon click Customize XSLT and Customize Entire View.
- In the design view click on any one of the dates in the EventDate column.
- Verify the field name by looking up a few lines for FieldRef_ValueOf. My field is EventDate.
- In the design view click on any one of the dates in the Event Date column. (the calculated column) This will highlight a line in the Code view.
- Edit the "value-of" element to change from this:
<xsl:value-of select="$thisNode/@*[name()=current()/@Name]"/>
to this:
<xsl:value-of select="$thisNode/@*[name()='EventDate']"/>
Note that you are replacing the expression "current()/@Name" to the name of the field wrapped in single quotes. I.e. 'EventDate'. EventDate is column from which you want to copy the data. - Save the file.
- Return to the browser and test the view. You can edit the view and remove the EventDate column as we only need the Event Date column.
Steps for SharePoint 2013
The basic steps:
- Create the test list and columns. (Same steps as for 2010)
- Create the JS Link JavaScript file.
- Upload or save the file to a SharePoint library.
- Edit the view's web part and link to the JS File.
Steps:
- Go to your site and create a test list. For this example an Announcements list would be a good choice.
- Create a new column named "EventDate". (You will enter your date data here.)
- In the List ribbon click Create Column.
- Enter "EventDate" as the name.
- Set the type to Date and Time.
- Set Date and Time Format to Date & Time.
- Click OK.
- Create a new column named "Event Date". (This column will control the filter grouping.)
- In the List ribbon click Create Column.
- Enter "Event Date" as the name.
- Set the type to Calculated.
- Enter this formula:
=TEXT([EventDate],"yyyy-mm")
or for years instead of months:
=TEXT([EventDate],"yyyy") - Set The data type returned from this formula to Single line of text.
- Click OK.
- Add some sample data with dates spread across two or three different months.
- Open SharePoint Designer 2013 and your site.
- In the Navigation area click Site Assets. (or any other library)
- Right-click in any empty space in the file area and select New and HTML.
- Right-click the new file and rename it to GroupByMonthJSLink.js (any name will do)
- Click the new file and then click Edit File.
- Select all of the HTML and delete it.
- Enter the JavaScript listed below.
- Save the file.
- Go to the new list.
- Click Settings (gear) and Edit Page.
- In the list’s web part click the dropdown and click Edit Web Part.
- In the web part properties panel expand Miscellaneous.
- Enter the following path into the JS Link box:
~site/SiteAssets/GroupByMonthJSLink.js
Note: "~site" points to current site/subsites URL while "~sitecollection" points to the top level site's URL. - Click Apply. (You should see the change to the Event Date column.)
- Click OK
- In the Page ribbon click Stop Editing.
- Test! You can edit the view and remove the EventDate column as we only need the Event Date column.
The JavaScript File
(function () { // do all of the setup work... var TTNctx = {}; TTNctx.Templates = {}; // configure the Event_x0020_Date field to copy the EventDate values // into the Event_x0020_Date column TTNctx.Templates.Fields = { "Event_x0020_Date":
{ "View": function (ctx) {return ctx.CurrentItem.EventDate} } }; // register the override SPClientTemplates.TemplateManager.RegisterTemplateOverrides(TTNctx); })(); // end of function
.
No comments:
Post a Comment
Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.