SPServices v0.6.1ALPHA1 Released with New SPFilterDropdown Function

I wanted to do a quick post about SPServices v0.6.1ALPHA1. The one change in this ALPHA is to add a new function called SPFilterDropdown. I’ve had a lot of requests for this functionality lately, though it has come up on and off in the past as well.

Note that this is an ALPHA release, and I don’t recommend using it in any production environment yet. I wanted to let everyone know that SPFilterDropdown was available because of the requests for it and also in hopes of having some real world testing.

As described in the preliminary documentation, the SPFilterDropdown function allows you to filter the values available in a Lookup column using CAML against the Lookup column’s source list.  This function works with all three types of “dropdown”: <20 options (simple select), 20+ options (complex select), and multi-select.

<UPDATE date=”2011-04-03″>I just made some edits and posted v0.6.1ALPHA2. It’s embarassing to admit, but ALPHA1 had some significant bugs in it. Usually I think I do much better. Luckily only two of you downloaded it, so sorry @ToniFrankola and whomever the other person is. ALPHA2 should be good to rock.</UPDATE>

Here’s an example:

We’ll filter the lookup column called “Country” so that we only include those Countries where the Active column is set to ‘Yes’. In the screenshot below, you can see that Canada is not currently ‘Active’.

image

In the Sales Opportunities list, we have a lookup column called Country, which gets its values from the Title column in the Countries list.

image

If we add the call to SPFilterDropdown below…

$().SPServices.SPFilterDropdown({
  relationshipList: "Countries",
  relationshipListColumn: "Title",
  columnName: "Country",
  CAMLQuery: "<Eq><FieldRef Name='Active' /><Value Type='Boolean'>1</Value></Eq>",
  completefunc: null,
  debug: false
});

…then ‘Canada’ is filtered out of the available values because it is not ‘Active’.

image

Give it a try and report any issues in the Discussions on the Codeplex site.

Similar Posts

4 Comments

  1. I have used SPServices on MOSS 2007 for a custom new form, there is a problem: if another field on the custom form has a validator (needed field, for example) if i compile the field for which i have used
    SPRequireUnique and i don’t compile the other field i got the error for other field, then there is a js error “Unable to get value of the property ” on SPRequireUnique , so the unique validation does no more works and i can insert a duplicate value.
    i use

  2. I have seen that my previous post was cut, i was inserting the html tags .
    I ‘m using jquery 1.5 for spservices 0.6

  3. What if I have a customer lookup column and when I select that it filters my products lookup column? I have this working with two drop down lookups. I now need to make the products lookup column a multi select lookup and have it populated in the same way when I click on a specific customer in the ddl then it will display all corresponding products on the left of the multi select lookup.

    Can I use this SPServices.SPFilterDropdown to do that?

    below is my caml query including the code that rebuilds the products drop down just so you have an idea of what I am doing at the moment.

    function GetSupplierProducts(SupplierID) {

    if (SupplierID != “” && SupplierID!= null && SupplierID!= undefined)
    {
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle(‘Products2’);

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(“” + SupplierID + “Folder”);
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    }
    }

    function onQuerySucceeded(sender, args) {
    //alert(‘Query Success’);
    var products = “”;
    var listItemInfo = ”;
    var listItemEnumerator = this.collListItem.getEnumerator();
    var product = ‘(None)|0’;
    var productOther = “(None)”;
    while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    var productname = oListItem.get_item(‘Title’);
    var productid = oListItem.get_item(‘ID’);
    product = product + ‘|’ + productname + ‘|’ + productid;
    productOther = productOther + “” + productname + “”;

    } // end while
    SetSupplierLookup(“Products”,product, productOther);

    } // End Function

    function SetSupplierLookup(fieldTitle, filteredchoices, filteredchoicesOther)
    {

    //Set default value for lookups with less that 20 items
    if ( $(“select[title='” +fieldTitle+ “‘]”).html() !== null)
    {
    $(“select[title='” +fieldTitle+ “‘]”).html(filteredchoicesOther)
    $(“select[title='”+ fieldTitle +”‘]”).val(lookupVal);
    //hide the field row
    $(“select[title=’IssueID’]”).closest(“tr”).hide();

    }
    else
    {

    //This rewires the available choices.
    $(“input[title='” +fieldTitle +”‘]”).attr(“choices”,filteredchoices);

    choices = $(“input[title='” +fieldTitle +”‘]”).attr(“choices”);

    hiddenInput = $(“input[title='” +fieldTitle +”‘]”).attr(“optHid”);
    $(“input[id='” +hiddenInput +”‘]”).attr(“value”,lookupVal)

    choiceArray = choices.split(“|”);
    for (index = 1; index < choiceArray.length; index = index + 2)
    {
    if (choiceArray[index] == lookupVal){
    $("input[title='" +fieldTitle +"']").val(choiceArray[index – 1]);
    //hide thie field row
    $("input[title='IssueID']").closest("tr").hide();
    }
    }
    }
    }

    function CustomerChanged(obj) {

    SP.UI.Notify.addNotification("Loading…", false);
    GetSupplierProducts(obj.options[obj.selectedIndex].text);

    }

    1. Sorry I found the solution

      I used SPServices.SPFilterDropdown and then I used this……

      function SetProductLookup(fieldTitle, filteredchoicesOther)
      {

      $(“#ctl00_m_g_d9b029d6_d68f_4dfa_8b0e_ede1e919e6a6_ctl00_ctl05_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_SelectCandidate”).html(filteredchoicesOther);

      }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.