2

I have two select boxes in my project. First one shows formats and the second one has two options, namely, "Yes" and "No". I have used angular chosen on both these boxes.

Initially, the "Yes" option from the second select box is disabled. I want to enable that option when the user selects "PDF" as format from the first select box.

These are my select boxes

//first  
<select name="exporType" id="exporType" ng-model="interactor.parameters.exporType" ng-options="format for format in formatOptions" ng-change="checkDisable();" chosen>
        <option value=""></option>
</select>

//second
<select name="maskAccountNumber" id="maskAccountNumber" ng-model="interactor.parameters.maskAccountNumber" style="width:145px;" chosen>
    <option value=""></option>
    <option value="N">No</option>
    <option value="Y" ng-disabled="disableoption">Yes</option>
</select>

I am calling ng-change on first select box which would set the "Yes" option ( $scope.disableoption ) to true or false based on its selection

The function is as follows

$scope.checkDisable = function() {

        console.log("Export type is "+$scope.interactor.parameters.exporType);
        if($scope.interactor.parameters.exporType == "PDF")
            $scope.disableoption = false;
        else
            $scope.disableoption = true;
};

The problem is that when I select "PDF" as option from first select box the "Yes" option doesn't update.

If I remove chosen from my select boxes it works fine but not with chosen

EXAMPLE WITH CHOSEN

EXAMPLE WITHOUT CHOSEN

Nishant123
  • 1,968
  • 2
  • 26
  • 44
  • Hi @Nishant123, that's a real bug and the answer from Tim should work. There's a bug within the directive. Could you please open an issue for me. I'll have to give a deep look into: https://github.com/leocaseiro/angular-chosen/issues/ thanks – Leo Caseiro Sep 22 '16 at 03:26
  • I fixed the code. Test it here https://plnkr.co/edit/lRiqxi?p=preview – Leo Caseiro Sep 22 '16 at 04:08

1 Answers1

0

I have been advised on this site several times against using actual <option> tags to build a <select> with Angular, because you lose some of the dynamic power which the controller can have. In my solution below, I completely control the state of both selects from the controller. Follow the Fiddle for a working demo.

HTML:

<select name="exporType" id="exporType"
        ng-model="exporType"
        ng-options="format.value as format.name for format in formatOptions"
        ng-change="checkDisable()" style="width:145px;">
</select>

<select name="maskAccountNumber" id="maskAccountNumber"
        ng-model="maskAccountNumber"
        ng-options="mask.value as mask.name disable when mask.disabled for mask in maskOptions"
        ng-change="checkDisable()" style="width:145px;">
</select>

Controller:

function MainController($scope) {
    $scope.disableoption = true;
    $scope.formatOptions = [{value: "BAI", name: "BAI"},
                            {value: "CSV", name: "CSV"},
                            {value: "PDF", name: "PDF"},
                            {value: "QBO", name: "QBO"},
                            {value: "QFX", name: "QFX"},
                            {value: "XLS", name: "XLS"}];

    $scope.maskOptions = [{value: "N", name: "No", disabled: false},
                          {value: "Y", name: "Yes", disabled: true}];

    $scope.exporType = "BAI";
    $scope.maskAccountNumber = "N";

    $scope.checkDisable = function() {
    if ($scope.exporType == "PDF") {
        // show the 'Yes' option for PDF
        $scope.maskOptions[1].disabled = false;
    }
    else {
        // hide the 'Yes' option if not PDF
        $scope.maskOptions[1].disabled = true;

        // change the mask to 'No' when switching to anything
        // other than PDF, since 'Yes' cannot be selected
        $scope.maskAccountNumber = "N";
    }
};

Demo here

JSFiddle

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    The problem is that it doesn't work with angular chosen. It's not about `select`. – a better oliver Sep 08 '16 at 08:07
  • @zeroflagL I have the Fiddle completely working now, and I believe you were wrong, as the ` – Tim Biegeleisen Sep 08 '16 at 08:45
  • @Tim Biegeleisen: You updated fiddle is perfect and it is working fine, no doubt in that, but my question is regarding angular chosen. I have updated your fiddle with "chosen" attribute and it is not working. Look here https://jsfiddle.net/Kunalh/zkeqxkd1/30/ I want to make it work with angular chosen – Nishant123 Sep 08 '16 at 09:03
  • Again: The question is about Angular chosen. Your fiddle doesn't use Angular chosen. So it doesn't matter if your code works or not. Maybe your solution solves the problem, but your code doesn't prove that. – a better oliver Sep 08 '16 at 09:03
  • @Nishant123 Well maybe this answer will be useful to you if you need a workaround the `chosen` directive. – Tim Biegeleisen Sep 08 '16 at 15:23
  • Thre's a bug in the directive. I'll have to call chosen:update to send the disable value. Please report on https://github.com/leocaseiro/angular-chosen/issues/ – Leo Caseiro Sep 22 '16 at 03:27
  • I believe it's something related to JSFiddle. The exatcly same code from @TimBiegeleisen works fine on plunker https://plnkr.co/edit/jwLKxH?p=preview. Perhaps AngularJS version? I can't find on JSFiddle which angular is loading. – Leo Caseiro Sep 22 '16 at 05:00
  • @LeoCaseiro Hey thanks for your help. I guess it works only with ng-options. I placed `ng-disabled` within the `options` and it didn't work. Have a look here https://plnkr.co/edit/8ivqmeWGFOOIV2eqkqAa?p=preview Is there any way to make it work without `ng-options`? The thing is that I have many such `selects` with `Yes` and `No` options and creating array only for two `options` doesn't seem feasible... – Nishant123 Sep 22 '16 at 05:56
  • @LeoCaseiro Hey as asked by you, I haved posted the issue [here](https://github.com/leocaseiro/angular-chosen/issues/218) – Nishant123 Sep 22 '16 at 06:55