Sharepoint Dropdown Choices Filter

824 views Asked by At

I have a dropdown on a form. I have it set as unique entry for the list. That works fine, when a user selects an option that was already selected in a previous list entry they are notified when the save or submit the form. However I would rather remove the choice from the dropdown list if that selection was already made and exists in the list, that way they can't select something already selected. Thanks for your help

1

There are 1 answers

0
LZ_MSFT On

We can use REST API to get all the exists dropdown items and then remove the downdown options in the new/edit form page. The following code for your reference.

<script src="//code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    removeDuplicateDropDowm("FilterDropDown");
});
function removeDuplicateDropDowm(fieldName){
    var listId = _spPageContextInfo.pageListId.replace("{","").replace("}","");
    var fieldHTML="";
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'"+listId+"')/items?$select="+fieldName;
    $.ajax({
        url: url,
        method: "GET",
        async:false,
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {       
            var items = data.d.results;
            $("select[title='"+fieldName+"'] option").each(function(){
                for(var i=0;i<items.length;i++){
                    if(items[i][fieldName]==$(this).val()){
                        $(this).remove();
                    }
                }
            });             
        },
        error: function (error) {
            console.log(JSON.stringify(error));
        }
    });
    return fieldHTML;
}
</script>

enter image description here