{"id":1826,"date":"2017-07-19T17:45:48","date_gmt":"2017-07-19T15:45:48","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1826"},"modified":"2017-12-28T10:55:17","modified_gmt":"2017-12-28T09:55:17","slug":"change-a-choice-with-fill-in-field-into-a-magic-dropdown-sharepoint","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2017\/07\/19\/change-a-choice-with-fill-in-field-into-a-magic-dropdown-sharepoint\/","title":{"rendered":"Change a &#8220;Choice with Fill-In&#8221; field into a magic dropdown [Sharepoint]"},"content":{"rendered":"<p>Sometimes you want to modify the options for a dropdown box by removing some existing options, however you want to be able to keep these old values for reporting or whatever reasons.<\/p>\n<p>Below is a way to do it by using jQuery and <a href=\"https:\/\/aymkdn.github.io\/SharepointPlus\/\">SharepointPlus<\/a>. It will hide the &#8220;Specify your own value&#8221; stuff, and add the old value (if any) into the dropdown selection.<\/p>\n<p>For example if you have a Choice field with options &#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;. Your item ID #1 has value &#8220;B&#8221;.<br \/>\nAfter a while you decide to delete &#8220;B&#8221; and add &#8220;D&#8221;, but you want to be able to find items with the &#8220;B&#8221; value.<br \/>\nSo you choose &#8220;Choice with Fill-In&#8221; and apply the below code:<\/p>\n<pre class=\"brush:javascript\">\r\n\/\/ For flexibility reasons we create them as a Choice with Fill-In option,\r\n\/\/    however we hide the free text field and we show only the dropdown\r\n\/\/    in case of the fill-in has a value, then we add it into the list of options\r\n$SP().formfields([\"My Fist Dropdown\", \"Another One\"]).each(function() {\r\n  var $e = this.elem();\r\n  \/\/ hide all except the dropdown\r\n  \/\/$e.not('select').hide().filter(':radio:last').closest('tr').hide(); \/\/ SP2010\r\n  $e.eq(0).closest('table').find('tr:gt(0)').hide(); \/\/ SP2013\r\n  \/\/ if we have a value into the fill-in box, then:\r\n  \/\/   - add it into the options\r\n  \/\/   - when another value is selected we check the first checkbox\r\n  var fillValue = $e.last().val();\r\n  if (fillValue) {\r\n    $e.filter('select').append('&lt;option data-fillin=\"true\">'+fillValue+'&lt;\/option>').each(function() {\r\n      var $this=$(this);\r\n      $this.find('option:last').prop(\"selected\", true);\r\n      $this.removeAttr(\"onclick\").on('change', function(event) {\r\n        var $opt = $(this).find(':selected');\r\n        if ($opt.data(\"fillin\")) {\r\n          $e.filter(':radio:last').prop(\"checked\", true);\r\n        } else {\r\n          $e.filter(':radio:first').prop(\"checked\", true);\r\n        }\r\n      });\r\n    });\r\n  }\r\n})\r\n<\/pre>\n<p>So now, if you create a new item you&#8217;ll see a dropdown with &#8220;A&#8221;, &#8220;C&#8221;, and &#8220;D&#8221; only.<br \/>\nBut if you edit your item #1, you&#8217;ll have a dropdown with &#8220;A&#8221;, &#8220;C&#8221;, &#8220;D&#8221; and &#8220;B&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to modify the options for a dropdown box by removing some existing options, however you want to be able to keep these old values for reporting or whatever reasons. Below is a way to do it by using jQuery and SharepointPlus. It will hide the &#8220;Specify your own value&#8221; stuff, and add [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[15,170,13,33],"tags":[123,24,152,158,117],"class_list":["post-1826","post","type-post","status-publish","format-standard","hentry","category-astuce","category-english","category-niveau-intermediaire","category-programmation","tag-english","tag-javascript","tag-niveau-intermediaire","tag-programmation","tag-sharepoint"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1826"}],"version-history":[{"count":6,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1826\/revisions"}],"predecessor-version":[{"id":1875,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1826\/revisions\/1875"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}