5

In my app i have a select html which has following options "Addition","Deletion","Duplicate","Member Duplicate"

Above drop down page is common for both add and edit screen. As of now if we come from any addition click or edit click drop-down has all options. (Note: drop-down binds at the time of loading page itself. we will show/hide depending on click)

As per new requirement I need to remove all other options except "Addition" in addition click and remove "Addition" option in edit click.

select html:

<select name="ReasonID" required ng-model="member.ReasonID" class="form-control" ng-options="reason.ID as reason.Description for reason in reasons |orderBy: reason.Description"></select>

Js

$scope.manageMember = function (member) {
  $scope.showGrid = false; 
  $scope.showForm = true;
  reset();
  $scope.memberTemp = member;
  angular.extend($scope.member, member); };

Please let me know if you need more details from my end.

18
  • select html is: Commented Dec 19, 2016 at 7:40
  • <select class="form-control" ng-model="accountsel" ng-change="getMembership()" ng-options="account.CustomerName for account in accounts | orderBy: account.CustomerName" > </select> Commented Dec 19, 2016 at 7:41
  • When you are referring to addition click what does that mean. Do you mean that when a user selects addition from select box then all other option for the same select box should be removed? Commented Dec 19, 2016 at 7:48
  • Would you like to remove it from DOM or only want to hide. Commented Dec 19, 2016 at 7:50
  • No. There are 2 buttons.. on click of "Add" button then I need to hide/remove all options from select html except "Addtion" option. On click of "edit" then I need to hide/remove only "Addition" option from select html. Commented Dec 19, 2016 at 7:52

3 Answers 3

1

Update :

Here the full sample code and working demo with dummy data.

HTML

<div ng-app>
  <h2>Todo</h2>
  <div ng-controller="TodoCtrl">
    <select name="ReasonID" required ng-model="member.ReasonID" class="form-control" ng-options="reason.ID as reason.Description for reason in reasons |orderBy: reason.Description"></select>
    <br/>
   <input type="button" ng-click="manageMember(undefined)" value="add"/>
    <input type="button" ng-click="manageMember('bla bla bla')" value="edit"/>
  </div>
</div>

JS

function TodoCtrl($scope) {
  $scope.reasons = [{ID:1,Description :"Addition"},  {ID:2,Description :"Deletion"},{ID:3,Description :"Duplicate"},{ID:4,Description :"Member Duplicate"}];

var reasonsTemp =angular.copy($scope.reasons); 

$scope.manageMember = function (member) {
console.log(reasonsTemp)
  $scope.reasons=reasonsTemp;// assign global object to model
  $scope.showGrid = false; 
  $scope.showForm = true;  
  $scope.memberTemp = member;   
  var EditArray=[];
   for(var i = 0 ; $scope.reasons.length>i;i++)
   {
    if($scope.reasons[i].Description === ($scope.memberTemp == undefined ? "Addition" : "bla bla bla"))// condition for is this addition or not
     {
     EditArray = $scope.reasons[i];
     break;   
     }     
     else // if is it not addition, then addition only offect that object. because we were already assigned original value globally
     {   
      if($scope.reasons[i].Description!=="Addition")
      {
        EditArray.push($scope.reasons[i])
      }
     }
   }
   $scope.reasons=EditArray;
   console.log($scope.reasons);
 }
}

Working Demo On console window

Sign up to request clarification or add additional context in comments.

8 Comments

this is how we call manage member function: add button: <div class="col-xs-2"> <button type="button" class="btn btn-success" ng-click="manageMember()" ng-show="accountsel" > <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Member </button> </div>
edit button click (Edit is an image) : <button type="button" class="btn btn-warning" ng-click="manageMember(auditmembership)" > <span class="glyphicon glyphicon-pencil" aria-hidden="true" ></span> </button>
without passing parameter value auditmembership(). how it will be work??
And what is the value of auditmembership?
that's why I wrote this code $scope.memberTemp == undefined ? "Addition" : "bla bla bla . please read my full inline comments and check it out Rani
|
1

Try this, HTML

  <select ng-model="selectedOption">
   <option ng-show="reason.show" ng-repeat="reason.ID as reason.Description for reason in reasons |orderBy: reason.Description">{{reason.ID}}</option> 
  </select>

JS

$scope.manageMember = function (member) {
 $scope.showGrid = false; 
 $scope.showForm = true;
 reset();
 $scope.memberTemp = member;   
 angular.extend($scope.member, member);
 if(member){
    for(var i = 0 ; $scope.reasons.length>i;i++)
     { 
       $scope.reasons[i].show = true;
       if($scope.reasons[i].ID == "Addition"){$scope.reasons[i].show = false;}
     }
   }else{
       for(var i = 0 ; $scope.reasons.length>i;i++)
     { 
       $scope.reasons[i].show = false;
       if($scope.reasons[i].ID == "Addition"){$scope.reasons[i].show = true;}
     }
  }
}

8 Comments

@prameela, please let me know that "Addition" is in your ID or Desrciption.
Hi dude, your answer is just awesome . but remove this code $scope.reasons=reasonsTemp from your answer . because some one find this line from my answer.
Ok @RameshRajendran i thought u used the same (first five lines) as given in question, so took from here, it will no matter for me as i didn't use this variable. BTW thanks to remind :)
Why you using angular.extend($scope.member, member); here?
Because OP wants to copying properties in $scope.member, may be she is using this variable in somewhere in view. It will not reflect my code so did not remove it. But in case of undefined (member) may be it will be a error, will it?
|
0

Suppose you have two buttons as,

<input type="button" ng-click="toAdd=true">Add</input>
<input type="button" ng-click="toAdd=false">Edit</input>

And the select box code should be like,

<select ng-model="selectedOption">
   <option ng-show="toAdd">Addition</option>
   <option ng-show="!toAdd">Deletion</option>
   <option ng-show="!toAdd">Duplicate</option>
   <option ng-show="!toAdd">Member Duplicate</option>
</select>

Hope this helps.

3 Comments

for better understanding I gave options that are getting binded. actually all these comes from database not hardcoded
this is how we bind select html: function getData() { memAuditFactory.getData().then(function (response) { $scope.reasons = response.data.Reasons; }); }
Is it possible to use filter option? if so how to pass it from controller in above method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.