0

I want the select and checkbox button to be selected depending on the given JSON format.

  {
    "autoselect": [
      "doloremque", 
      "amet natus aut", 
      "tenetur"
    ], 
    "component": "checkbox", 
    "description": "blanditiis exercitation quidem molestiae eius aliquam deserunt consequat", 
    "editable": false, 
    "label": "vel qui autem", 
    "options": [
      "mollitia voluptatum", 
      "doloremque", 
      "amet natus aut", 
      "inventore", 
      "tenetur"
    ], 
    "required": true
  },

  {
    "autoselect": [
      "debitis exercitationem"
    ], 
    "component": "select", 
    "description": "nihil animi ut qui consequuntur velit", 
    "editable": false, 
    "label": "dolorum quo", 
    "options": [
      "aliquid", 
      "eius", 
      "voluptatem aliqua vel", 
      "earum voluptatem", 
      "debitis exercitationem"
    ], 
    "required": true
  }, 
  {
    "autoselect": [
      "doloremque", 
      "amet natus aut", 
      "tenetur"
    ], 
    "component": "checkbox", 
    "description": "blanditiis exercitation quidem molestiae eius aliquam deserunt consequat", 
    "editable": false, 
    "label": "vel qui autem", 
    "options": [
      "mollitia voluptatum", 
      "doloremque", 
      "amet natus aut", 
      "inventore", 
      "tenetur"
    ], 
    "required": true
  }

Both select and checkbox button should display on the basis of number of element in options array. And the value of radio button based on autoselect value. If autoselect value match with any value of options then corresponding option select and checkbox button will be true and remaining will false.

And if JSON object does not contain autoselect value then none of the select and checkbox button should be selected initially.

Contoller.js

var app = angular.module('myApp',[]);
            app.controller('MainCtrl', function ($scope, $http, $log) {
                $scope.selected = [];
                $http({
                        method: 'GET',
                        url: 'data.json'})
                    .then(function(response) {
                        $scope.error = response;
                        $scope.renderTags = response.data.data;
                        $log.info(response);
                    }, function(reason){
                        $scope.error = reason.data;
                    }); 

                $scope.clicked=function(option){
                    console.log(option);
                }
            });

HTML code

<div class="mainBody" ng-repeat="tag in renderTags.form_fields track by $index">
                <div ng-switch on="tag.component">
                    <div ng-switch-when="checkbox">
                        {{tag.label}}: </br>
                        <div ng-repeat="select in tag.options" ng-disabled="!tag.editable" ng-required="tag.required">
                            <span ng-if="tag.autoselect!== null">
                                <span ng-if="tag.autoselect[0] === select">
                                    <input type="checkbox" checked ng-value="select" name="$index" ng-click="clicked(select)"/>{{select}}
                                </span>
                                <span ng-if="tag.autoselect[0] !== select">
                                    <input name="$index" type="checkbox" ng-value="select">{{select}}
                                </span>
                            </span>
                        </div>
                    </div></br>
                    <div ng-switch-when="select">
                        {{tag.label}}: 
                        <select ng-disabled="!tag.editable" ng-required="tag.required" ng-selected="selection">
                            <option ng-repeat="choice in tag.options">{{choice}}</option>
                        </select>
                    </div></br>
                    <div ng-switch-default>

                    </div>
                </div>          
            </div>

when autoselect values is not there that time my code is not working.

Here is the link to plunkr

Thanks in advance..

3
  • What is exactly not working? checkbox code, drop-down code or both? Commented Mar 21, 2017 at 12:03
  • @MansiParekh Both are not working. i tried each and every method but no success. Check box is able to select only value even though multiple autoselect are being passed and drop-down doesn't even select any value. Kindly help. Commented Mar 22, 2017 at 7:09
  • Added a link to plunkr Commented Mar 22, 2017 at 7:41

1 Answer 1

1

ng-modeland ng-checkedconditions are added. Also I have modified checkbox clicked method.

plunker link

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

1 Comment

Thank you @Mansi. It is now working as smooth as butter.

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.