Display selected select and checkbox button from JSON in angularJS

。_饼干妹妹 提交于 2019-12-25 05:23:22

问题


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..


回答1:


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

plunker link



来源:https://stackoverflow.com/questions/42924844/display-selected-select-and-checkbox-button-from-json-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!