14

I am using chosen.jquery.js for select field

<select chosen multiple data-placeholder="Select Body Part(s)"  
ng-options="option.Name as option.Name for option in BodyPartList" ng-model="Body_Part">
      <option value="" disabled>Select Body Part(s)</option> 
</select>

But It shows only data-placeholder value in case of no data in model. I want to show "Select Body Part(s)" as a option in list. And user must not select this. Reason is that, I want to add dynamic "Unknown" value in list of Body_Parts. But it not reflect in list.

Same work for select having single selection.

Pooja-G
  • 518
  • 4
  • 20

3 Answers3

8

I'm not seeing any problems with your code, as such. Like, I'm trying it and getting the visual behaviour I think you're wanting? Am I missing something?

enter image description here

Html just yours with ng-app etc, javascript is:

var myApp = angular.module('myApp', ['localytics.directives']);

myApp.controller('MyController', function($scope) { 
  $scope.BodyPartList = [
    { Name: "Arm" },
    { Name: "Leg" }
  ];

  $scope.Body_Part = [];
});

Not sure if data-placeholder is actually doing anything.

Fiddle: http://jsfiddle.net/7187f6d9/

That said, it's not "working". In the fiddle I put a regular select box alongside the chosen one, and the chosen one doesn't seem to be writing to the ng-model correctly. I'm not sure what's up with that.

Duncan Matheson
  • 1,706
  • 11
  • 25
1

You can choose the below option https://harvesthq.github.io/chosen/#optgroup-support

or push another item to the top of the array from server side as your custom label and disable it on client side with the help of ng-if and $index properties of ng-repeat and angularjs. If this make sense then its fine else i can give you a demo.

karman
  • 346
  • 3
  • 20
1

I hope your query is, you want show a place holder as current selected value but user shouldn't be able to select it.

Instead of making it disabled, make ithidden. Then display an error if a user doesn't select any other options, using the value of placeholder option.

A sample snippet is added below. If value of select is Error, write a case to throw a error back to user.

<select>
  <option value="Error" hidden>Select any Company</option>
  <option value="Toyota">Toyota</option>
  <option value="Nissan">Nissan</option>
  <option value="BMW">BMW</option>
</select>

Hope this helps! If not, ping me with your query. :)

bharadhwaj
  • 2,059
  • 22
  • 35