1,804 articles and 14,766 comments as of Tuesday, April 19th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint (Joel Oleson) content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Thursday, August 26, 2010

SharePoint: Extending the DVWP – Part 31: Filling in Default Data on the insert Template with jQuery

Author: Jim Bob Howard

We’ve been building a solution for a division to keep track of its full-time employees. We’ve rearranged the layout of the DVWP, reprogrammed the form action links and even passed them some workflow variables, added cascading drop-downs, created an alternate edit template so that we can record edits AND deletes to an audit list.

Several pieces are beginning to fall into place as we draw nearer to the culmination of the Extending the DVWP series. We now have multiple pages (one per location/manager). Each page can have multiple DVWPs, either because multiple locations are managed together and/or because we keep track of "other" types of employees in separate counts.

As the title indicates, this is Part 31 in the series, so those who have been following along have been at it for several months. If any part of the above seems like Greek (and you don’t speak Greek), scroll down to the bottom of this article until you find "View all entries in this series" and click on the link to see them all.

Simplifying the User Experience

Now that we have separate pages for each location, it probably makes sense that the manager who accesses each page will only be adding new employees to his or her own location. So, let’s set the location by default based on which page we’re on.

Of course, we’ll use jQuery:

  1. Switch to edit mode on the DVWP page, by either
    1. clicking Site Actions -> Edit Page, or
    2. adding ?PageView=Shared&ToolPaneView=2 to the page URL
  2. If you’ve been following along and are using cascading dropdowns, open the existing Content Editor Web Part (CEWP) and let’s consider how we might set some defaults.
  3. (Note: If you haven’t added cascading dropdowns, skip down to Step 3, ignoring the dropdowns, but looking at the code on lines 6-7.)

    Here’s what mine looks like:

    <script src="/js/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="/js/jquery.SPServices-0.5.6.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $().SPServices.SPCascadeDropdowns({
                relationshipWebURL: "/subsite",
                relationshipList: "LocGroup",
                relationshipListParentColumn: "Location",
                relationshipListChildColumn: "Group",
                relationshipListSortColumn: "Group",
                parentColumn: "Location",
                childColumn: "Group"
            });
            $().SPServices.SPCascadeDropdowns({
                relationshipWebURL: "/subsite",
                relationshipList: "GroupPos",
                relationshipListParentColumn: "Group",
                relationshipListChildColumn: "Position",
                relationshipListSortColumn: "Position",
                parentColumn: "Group",
                childColumn: "Position"
            });
        });
    
    </script>
    
  4. The first SPCascadeDropdowns call is to cascade the Group from the Location. Let’s set the Location first so it can limit our Group dropdown based on our default Location setting:
  5.     $(document).ready(function() {
            $("select[title=Location]").val("ACB");
            $("input[title=Location]").val("ACB").next("img").trigger("click");
            $().SPServices.SPCascadeDropdowns({
                relationshipWebURL: "/operations",
                relationshipList: "LocGroup",
                relationshipListParentColumn: "LocDept",
                relationshipListChildColumn: "Group12",
                relationshipListSortColumn: "Group12",
                parentColumn: "Location",
                childColumn: "Group"
            });
            .
            .
            .
        });
    </script>
    

    It looks like we’re setting it twice, but actually we’re dealing with two different methods for drawing the control: one for Internet Explorer (IE), one for Firefox (FF). (Note: I’ve not tested this on Safari or Chrome, but I would expect these two alternatives to cover the bases for a dropdown list.)

    1. Setting the default in FF is pretty straightforward: find the select and set the .val(). Firefox takes care of the rest.
    2.         $("select[title=Location]").val("ACB");
      
    3. In IE, it works a bit differently. If the list is over 20, IE draws it differently: as an input control with some other controls programmatically attached.

    4. For the times when IE draws it as an input, we have to "click" the dropdown arrow to get it really set for use by the form, and especially the cascading dropdown call.

              $("input[title=Location]").val("ACB").next("img").trigger("click");
      

      In this case, after we set the .val(), we look for following sibling that is an img control (.next("img")) and the trigger its click event.

  6. For the main Location tab, that’s all we’ll do to default. But one more thing will ensure that things work well for IE when it draws the dropdown as an input:
  7.         $("input[title=Employee]").focus();
        });
    
    </script>
    

    This will set the focus back to the Employee text control to avoid leaving the programmatic dropdown on the screen.

Houston, We Have a Problem

The above script works great when there’s only one DVWP on the page that has a control named Location. But we’ve been working under the premise that there is more than one DVWP on this page.

If we implement this script, it will set the Location to "ACB" for every insert template generated on the page. In the case of the alternate kinds of employees, we actually want the Location set to the kind, and their Group set to a "home site."

(Ex. A paramedic that picks up shifts among clinics, as needed, is considered a PRN. Even though he may be working at a different clinic each day, one of them (ACB) is considered his home site. For this implementation, we want to consider his Location to be PRN so that we can count the total PRN headcount separate from those who are FTE’s at the same Location. So, we use the Group to indicate his home site of ACB.)

But that won’t work with the above script.

Next time when we continuing Extending the DVWP, we’ll look at a way to know which DVWP we’re working with, so we can set the defaults accordingly.

Author: Jim Bob Howard

Jim Bob Howard is a web designer / webmaster in the healthcare industry. He has been working with SharePoint since March 2009 and enjoys sharing what he has learned. He is a moderator and frequent contributor to Stump the Panel, and answers SharePoint questions on Twitter (@jbhoward) and via email ([email protected]).

View all entries in this series: Extending the DVWP»
Entries in this series:
  1. SharePoint: Extending the DVWP - Part 1: Layout Enhancement - Rearranging Columns - Default and Edit Templates
  2. SharePoint: Extending the DVWP - Part 2: Layout Enhancement - Rearranging Columns - Insert Template
  3. SharePoint: Extending the DVWP – Part 3: Getting it All on One Line - DVWP Function Action Links
  4. SharePoint: Extending the DVWP – Part 4: Turning DVWP Action Links into Buttons
  5. SharePoint: Extending the DVWP – Part 5: Doing Stuff Before Save on Submit - PreSaveAction()
  6. SharePoint: Extending the DVWP – Part 6: Examining the Form Action Links
  7. SharePoint: Extending the DVWP – Part 7: Creating a Form Action Workflow
  8. SharePoint: Extending the DVWP – Part 8: Creating a Form Action Workflow - The After Math
  9. SharePoint: Extending the DVWP – Part 9: Oops! Failed Setting Processor Stylesheet
  10. SharePoint: Extending the DVWP – Part 10: Passing Workflow Variables to a Form Action Workflow
  11. SharePoint: Extending the DVWP – Part 11: Getting More Form Fields to the Workflow
  12. SharePoint: Extending the DVWP – Part 12: Adding More Form Fields from the Data
  13. SharePoint: Extending the DVWP – Part 13: Putting PreSaveAction() to Work – Creating Variables
  14. SharePoint: Extending the DVWP – Part 14: Putting PreSaveAction() to Work with jQuery
  15. SharePoint: Extending the DVWP – Part 15: User-Managed Dropdowns with Site Columns
  16. SharePoint: Extending the DVWP – Part 16: User-Managed Dropdowns - Loading Data
  17. SharePoint: Extending the DVWP – Part 17: User-Managed Dropdowns – Creating a Relationship list
  18. SharePoint: Extending the DVWP – Part 18: User-Managed Dropdowns – Loading the Relationship list – Part 1
  19. SharePoint: Extending the DVWP – Part 19: User-Managed Dropdowns – Loading the Relationship list – Part 2
  20. SharePoint: Extending the DVWP – Part 20: Cascading Dropdowns - Applying the jQuery
  21. SharePoint: Extending the DVWP – Part 21: Cascading Dropdowns - Three-tier Cascade
  22. SharePoint: Extending the DVWP – Part 22: Creating Title Based on Other Fields with jQuery
  23. SharePoint: Extending the DVWP – Part 23: Creating Title Based on Other Fields with a Workflow
  24. SharePoint: Extending the DVWP – Part 24: A Note to Readers
  25. SharePoint: Extending the DVWP – Part 25: Using an Audit Trail by Creating List Items with SPServices
  26. SharePoint: Extending the DVWP – Part 26: Modifying the Edit Template
  27. SharePoint: Extending the DVWP – Part 27: Adding an Alternate Edit Template to a DVWP
  28. SharePoint: Extending the DVWP – Part 28: Massage the Remove Template
  29. SharePoint: Extending the DVWP – Part 29: Modifying Form Action Workflows on the remove Template
  30. SharePoint: Extending the DVWP – Part 30: Using EasyTabs with Filtered DVWPs to Make Data Manageable
  31. SharePoint: Extending the DVWP – Part 31: Filling in Default Data on the insert Template with jQuery
  32. SharePoint: Extending the DVWP – Part 32: Filling in Default Data on the insert Template with Multiple DVWPs
  33. SharePoint: Extending the DVWP – Part 33: Modifying Total and Subtotal Row Layouts in DVWP
  34. SharePoint: Extending the DVWP – Part 34: Using Icons for Form Action Links
  35. SharePoint: Extending the DVWP – Part 35: Putting it All Together
  36. SharePoint: Extending the DVWP – Bonus: Fixing the Insert Form Action When "No Matching Items"
  37. SharePoint: Extending the DVWP – Bonus: Creating a Title Based on Dropdowns with jQuery
 

Please Join the Discussion

4 Responses to “SharePoint: Extending the DVWP – Part 31: Filling in Default Data on the insert Template with jQuery”
  1. Brad says:

    Would using javascript/jQuery to pre-populate my field values cause the textchanged event to allow my update? I’m not sure where to begin, never added javascript to this app yet so I guess I wanted to explore what that might allow me to do before set that up.

    • Brad,

      I can’t say for sure one way or the other. (Marc Anderson might know.) I haven’t tested to see if that gets triggered. But, you could always use jQuery to .trigger() it, if that’s what you need to happen.

      Blessings,
      Jim Bob

Trackbacks

Check out what others are saying about this post...
  1. [...] Last time, we talked about setting a control to a default value using jQuery. In the example, we were setting the value of a dropdown list on which we had implemented Marc Anderson’s SPCascadeDropdowns. It’s a pretty straightforward process with a couple of caveats depending on which browser your users are using and/or how many items are in your dropdown list. [...]

  2. [...] we save the webpart, though, we might want to make a few more changes to the insert template to fill in default data. But since we’re using multiple DVWPs, we’ll need to make special considerations so [...]




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!