Displaying multiple checkbox values in one SharePoint column - InfoPath Dev
in

InfoPath Dev

Displaying multiple checkbox values in one SharePoint column

Last post 05-27-2008 10:47 AM by Mikem. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 02-01-2008 12:04 AM

    Displaying multiple checkbox values in one SharePoint column

    Hello,

    I'm sure lots of people have encountered this problem before and have come up with different, and even the same, solutions to this. This is an account of how I did it...

    Imagine having a form that allows people to pick their ethnicities. Since interracial marriages is a growing trend these days, you'll need to use checkbox controls instead of the old-school radio button controls:

    [ ] African (field name = 'African')
    [ ] Dominican Republican
    (field name = 'Domican_Republican')
    [ ] Irish (field name = 'Irish')
    [ ] Martian (field name = 'Martian')

    When the form is published to SharePoint, you may want to have all of the checked values show up in one column instead of four individual columns. Since checkbox controls, unlike radio button controls, hold a collection of values instead of just one value, you'll need to do some coding in order to accomplish the simple feat of displaying multiple checkbox values in one SharePoint column.

    One way to do it is to create a hidden field; let's call it origins. This field will contain a concatenated, comma-separated list of the checked values that will be displayed on SharePoint. In the Data Source panel, create a container (parent) group for these checkboxes; let's call it origins_group. The origins_group is optional, but is highly recommended unless you are masochistic and want to create an event handler for each checkbox control ;).

    The next step is to create an OnAfterChange event handler for the origins_group group, leveraging the behavior that "bubbles" changes from the nested checkbox controls (children) to the parent origins_group. This function basically walks through the origins_group node and concatenates all of the values in its child nodes that are elements (nodeType = 1) and contain the "true" value (checked). There are a couple of regular expressions that convert underscores to spaces and removes the trailing comma in the concatenated string before it is ouputted to the origins field.

    To prevent the origins_group OnAfterChange event from firing needlessly, you should introduce conditional criteria such as if (eventObj.Operation=='Insert') to ensure that the event is only triggered when a node is being inserted (every checkbox change results in the delete operation of the old value followed by a node insertion of the new value).

    Here's the complete code:

    var oOrigins = XDocument.DOM.selectSingleNode('my:myFields/my:origins');
    var oOriginsGroup = XDocument.DOM.selectSingleNode('my:myFields/my:origins_group');

    function msoxd_my_origins_group::OnAfterChange(eventObj) {
      if (eventObj.IsUndoRedo) {
        // An undo or redo operation has occurred and the DOM is read-only.
        return;
      }
      // A field change has occurred and the DOM is writable. Write code here to respond to the changes.
      if (eventObj.Operation=='Insert') {
        var str = '';
        var arr = oOriginsGroup.childNodes;
        for (var i=0; i<arr.length; i++) {
          if (arr[i].nodeType==1 && arr[i].text=='true') {
            str += arr[i].nodeName.replace(/my:(\w+)/,'$1, ');
          }
        }
        str = str.replace(/_/g,' ');
        str = str.replace(/, $/,'');
        oOrigins.text = str;
      }
    }

    Cheers,
    Tom
  • 02-13-2008 03:06 PM In reply to

    Re: Displaying multiple checkbox values in one SharePoint column

    Thanks for sharing this with us!

  • 02-13-2008 04:53 PM In reply to

    Re: Displaying multiple checkbox values in one SharePoint column

    Greg Collins:

    Thanks for sharing this with us!

    Thanks for all of your insightful articles! I'm just giving back to the community where I can :).

  • 03-24-2008 11:51 AM In reply to

    Re: Displaying multiple checkbox values in one SharePoint column

    this looks like exactly what I've spent days searching for.

    I am not clear however, how one adds the script to the section control.  Right clicking on the control or bringing up properties does not provide a path to a script window.

     

  • 03-24-2008 12:02 PM In reply to

    Re: Displaying multiple checkbox values in one SharePoint column

    found the solution to my newbie quesion.  Thanks

  • 03-24-2008 04:48 PM In reply to

    Re: Displaying multiple checkbox values in one SharePoint column

    Great! I'm happy to hear you got it working!

  • 03-25-2008 01:03 AM In reply to

    Re: Displaying multiple checkbox values in one SharePoint column

    Sorry, I was traveling out of state. In any case, glad you got it working. Post here if you hit any snags.

  • 05-22-2008 10:42 AM In reply to

    • Mikem
    • Not Ranked
    • Joined on 05-22-2008
    • Posts 4

    Re: Displaying multiple checkbox values in one SharePoint column

    Hey 10, great post! Very helpful. I do have one add on question:

     The above method will do a great job of aggregating the ethnicities to an attribute which can than be filtered on. The issue that I'm coming across is that if i wanted to find all people who were a certain ethnicity (including someone who has multiple ethnicities) the filtering won't work. So if I filtered on just Irish I would lose people who were Irish & Martian which in my case is bad.

    Is there a way to get around this? I'm open to changing the controls if necessary but I think the check boxes provide the best interface. I can't filter on a merged repeating area so that's not an option (or is there a trick around this). Ultimately I care about the field containing a certain value as opposed to matching a string of potentially many values. The end user filtering is my concern. I'm deploying the form on SharePoint and would like to avoid creating tons of custom views.

     Thanks,

     Mikem

     



     

  • 05-24-2008 09:12 AM In reply to

    Re: Displaying multiple checkbox values in one SharePoint column

    Mikem,

    I don't have access to SharePoint right now, but I believe what you're looking for already exists as of Sharepoint 2003. Here's how to do a "contains" filter:

    1. From the forms library, switch to the datasheet or datagrid view (I forget the exact terminology in SharePoint, but the view you want is similar to a spreadsheet grid).
    2. Just as you would do in Excel, select the custom filter for the column(s) you want and you should be able to add a "contains" filter.

    Cheers,
    10bt

  • 05-27-2008 10:47 AM In reply to

    • Mikem
    • Not Ranked
    • Joined on 05-22-2008
    • Posts 4

    Re: Displaying multiple checkbox values in one SharePoint column

    10bt,

     This will work for me :) but unfortunately requires everyone else to know how to do this as, to my understanding, this is only a temporary personal view. Given the nature of the application that assumption just won't hold, the end users aren't going to open up different views and apply custom filters. To get around this I'd like to provide a nice user interface just linking to different filter views but this forces me to reinvent the wheel (as I'm essentially ignoring SharePoint's ability to filter document libraries and creating my own views which apply their own filters). As you can imagine this can be quite time consuming and unellegant given the large variety of views necessary to provide a complete capability.

    I'm hoping to find a way around creating so many views. Any ideas?

     Thanks,

    Mikem

     

Page 1 of 1 (10 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.