How to filter projects in project picker for custom field

kmashkov January 14, 2019

Hi everyone, I have a custom field with project picker type. How can I filter projects in dropdown list?

I tried to look into project-columnview.vm, but there is no logic with project items just some $projectGroupsJson variable:

#disable_html_escaping()
#customControlHeader ($action "$!{displayParameters.fieldNamePrefix}$field.id" $i18n.getText($field.nameKey) true $displayParameters $auiparams)
<input data-container-class="project-ss" type="text" class="project-field" value="$!textutils.htmlEncode($project.toString())"
       id="$!{displayParameters.fieldNamePrefix}$field.id" name="$!{displayParameters.fieldNamePrefix}pid"/>
<div id="$!{displayParameters.fieldNamePrefix}${field.id}-options" data-suggestions="$textutils.htmlEncode($projectGroupsJson)"></div>
#customControlFooter ($action "$!{displayParameters.fieldNamePrefix}$field.id" "" $displayParameters $auiparams)

 I thought I could filter by project.name for my custom field.id, but I just can't suppose how to do it.

1 answer

1 accepted

2 votes
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 16, 2019

I'm not sure how you can do this via the .vm templates.  It might be possible, but I think it would be more feasible to do this in a javascript snippet of code.

Side note: This particular field type is only designed to show the end user projects that they can see in Jira.  Hence if they don't have the browse permission for a project, they wouldn't be able to see the project nor see that project as an option in Jira.   So that might be something to keep in mind when wanting to restrict possible values for this particular kind of field.

 

That said, I had some success in a similar question using javascript to hide select field options.  Check out https://community.atlassian.com/t5/Jira-questions/Re-Re-Hide-some-link-types-from-issue-link-screen/qaq-p/840201/comment-id/270479#M270479

In that post I found that inspecting the elements on the page could be helpful to understand how you can then craft your own javascript in Jira.  In my case, I was able to come up with this javascript&colon;

<script type="text/javascript">
jQuery(document).ready( function($) {
    var rmvProjectpick = setInterval(function() {
AJS.$("#customfield_10800.select option[value='10001']").remove();
    },1000);
});
</script>

I added this javascript to the description of the custom field itself.   In my case, my BIZ project has an ID of 10001, and my project picker field is customfield_10800.   I found out both of these details by going to that field on a Jira issue, then selecting the project picker drop down box, and then right clicking to select inspect element.   Each project listed there is being represented by its 5 digit unique ID.  I'm sure if you were to do this in your own Jira instance, you would have different values for the customfield and the option(s) you want to hide from the end user.

Another note here: This approach would only help in the web UI for Jira.  It doesn't prevent the REST API or JAVA APIs in Jira from using all the possible field options available to that end users.

I hope this helps.  However it has occurred to me that perhaps you're creating a plugin to Jira, and as such this approach might not be as applicable in that case.  If you were just a Jira admin trying to customize what options your end users can select, this might be a better approach for you.

kmashkov January 21, 2019

Thank you for such a complete answer! I have already found a temporary solution in JS, as you actually wrote.

$(document).ajaxComplete(function () {
// in edit issue form
var customfield_10316 = $("#tab-0 select#customfield_10316");
if (customfield_10316 && customfield_10316[0]) {
//com.atlassian.auiplugin:aui-select2();
AJS.$("#customfield_10316").auiSelect2();
$('#s2id_customfield_10316').css("max-width","500px");
var childNodes = customfield_10316[0].childNodes;
for (var i = 0, len = childNodes.length; i < len; i++) {
childNodes[i] && childNodes[i].text && childNodes[i].value !== "-1" && !childNodes[i].text.toUpperCase().includes('PRJ') && childNodes[i].remove();
}
}
// in view issue form (live edit)
var customfield_10316_live = $("#customfield_10316");
if (customfield_10316_live && customfield_10316_live[0]) {
var childNodes = customfield_10316_live[0].childNodes;
for (var i = 0, len = childNodes.length; i < len; i++) {
childNodes[i] && childNodes[i].text && childNodes[i].value !== "-1" && !childNodes[i].text.toUpperCase().includes('PRJ') && childNodes[i].remove();
}
}
});
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 22, 2019

I am glad to hear my answer helped.  You can accept my answer as a solution if it helped.

Thanks for posting your solution as well.  I really like your approach!  It appears that your method can remove the projects listed in this field by their project key, and isn't dependent on the ID of the project like my approach is.

kmashkov January 22, 2019

Thanks. Already accepted;)

Suggest an answer

Log in or Sign up to answer