0

I am loading a table from an API call , table rows are dynamic and it is based on the returned values from API call. I am displaying sort order and value should be unique and user shouldn't select a previously selected values. I tried to follow as per this (http://jsfiddle.net/jnash21/oqezom4y/) but i am not able to achieve as mine is dynamic.

I tried this (http://embed.plnkr.co/QU4r05n9rQprwyL9Ltxh/) .

editor.controller('EditorController', function($scope) {

  $scope.entities = [{name:"pencil",sortOrder:""} ,{name:"notepad",sortOrder:""} ,
  {name:"bookshelf",sortOrder:""}
  ];

  $scope.sortOrderValues=[1,2,3];
});

    <table>
  <tr ng-repeat="x in entities">
    <td>{{ x.name }}</td>
    <td><select ng-model="x.sortOrder"
                ng-options="col for col in sortOrderValues"> 
       </select>
          <span ng-show="!x.sortOrder"> * sort order required</span>  
    </td>

  </tr>
</table>

How can I prevent a user from selecting same sort order in each row using angular js?

2
  • 1
    The best solution I can think of is to put a copy of that sortOrderValues list onto each of the entities being displayed in the table. Then, I think you'll have to loop through those entities every time a value is changed in one of those dropdowns and update each dropdown to have the correct options. Commented Apr 15, 2019 at 20:32
  • 1
    You can just have a master set that controls selected. Then when you select one you add that value to the set. Change the NG options to an NG repeat and add a NG disable based on the value being in the set. You can ngif based on whether you want to hide or disable the options. Users choice I guess. Commented Apr 15, 2019 at 23:01

1 Answer 1

1

This plunker might help you.

First of all, genereate an array from 1 to entities.length (this case, 3). When you select an option, tirgger the optionSelected function. This function will generate your inital array and calculate the used sortOrders by your entities. Then it filters the second ones from the first array.

HTML

<div ng-controller="EditorController">
  <table>
    <tr ng-repeat="x in entities">
      <td>{{ x.name }}</td>
      <td><select ng-model="x.sortOrder"
                  ng-options="col for col in sortOrderValues"
                  ng-change="optionSelected()"> 
         </select>
            <span ng-show="!x.sortOrder"> * sort order required</span>  
      </td>

    </tr>
  </table>
</div>

CONTROLLER

editor.controller('EditorController', function($scope) {

  $scope.entities = [{name:"pencil",sortOrder:""} ,{name:"notepad",sortOrder:""} ,
  {name:"bookshelf",sortOrder:""}
  ];

  // Genereate all the numbers between 1 and $scope.entities.length
  $scope.sortOrderValues= $scope.entities.map(
    function (item, index) {
      return index + 1;
    }
  );

  // Function executed when you select a sortOrder
  $scope.optionSelected = function () {
    // Genereate all the numbers between 1 and $scope.entities.length
    var allIndexes = $scope.entities
      .map(function (entity, index) { return index + 1; });

    // Get all the sortOrder used
    var usedIndexes = $scope.entities
      .map(function(e) { return e.sortOrder; });

    // Remove from the [1, .., $scope.entities.length] array all the sortOrder used
    $scope.sortOrderValues = allIndexes
      .filter(function (order) {
        return !usedIndexes.find(function(index) { return index === order; });
      });
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help , once value is selected , it is removed from list , and selected value disappears , this is not the expected outcome .
So you want leave already selected values on the list, but not be abke to select them?
Yes , i managed achieve , will post it here. thanks for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.