简体   繁体   English

如何传递Json值并获取各自的数据

[英]How to pass Json value and get respective data

Am using Django in my project. 我在我的项目中使用Django。 I have a resources.py(JSON file) which consists of all my required data from the database. 我有一个resources.py(JSON文件),其中包含数据库中所有必需的数据。

I have an html file which consists of several dropdown buttons, my aim is to once I select one dropdown option it will pass that value and get values for successive dropdown button dynamically. 我有一个包含几个下拉按钮的html文件,我的目的是一旦选择一个下拉选项,它将传递该值并动态获取连续的下拉按钮的值。

My HTML: 我的HTML:

<div class="wall" ng-controller="LayerCtrl" >
    <table>
        <tr>
            <td>
                <div class="input-group">
                    <span class="input-group-addon">Fab</span>
                    <select class="form-control" name="fab">
                        {% for f in fab %}
                        <option value="{{f}}">{{f}}</option>
                        {% endfor %}
                    </select>
                </div>
            </td>

            <td>
                <div class="input-group">
                    <span class="input-group-addon">Technode</span>
                    <select class="form-control" ng-model="selected_technode" ng-options="l.value as l.label for l in technodes"></select>
                </div>
            </td>

            <td>
                <div class="input-group">
                    <span class="input-group-addon">Layer</span>
                    <!--<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">-->
                    {%verbatim%}
                    <!--<select class="form-control"  name="layer" >-->
                        <!--<option ng-repeat="l in layer_list" value="{{l}}">{{l}}</option>-->
                    <!--</select>-->
                    <select class="form-control" ng-model="selected_layer" ng-options="l as l for l in layerlist"></select>
                    {%endverbatim%}
                </div>
            </td>
        </tr>
    </table>
        </div>

and my JS: 和我的JS:

{{ ngapp }}.controller("LayerCtrl", function ($scope, $http, $resource){

var layerresource_url = $resource("{% url 'api_dispatch_list' 'v1' 'layer' %}");

$scope.technodes = [
    {'value': 22, 'label': 22},
    {'value': 28, 'label': 28},
];

console.log('initializing....')

$scope.$watch('selected_technode', function () {
    <!--alert($scope.selected_technode);-->
    $scope.update_layer();
});

$scope.update_layer = function(){;
      console.log('Stage1: Initializing Primary Data... ');
      layerresource_url.get({techtype__contains: $scope.selected_technode, limit:1500},
          function(data){
            $scope.layerlist = data['objects'][0]['layer'];
            console.log($scope.layerlist);
          },function(data, status){
            console.log('Stage1: Internal error while loading initial data:'+status );
            alert('internal error');
          }
      );
    };
});

I need to pass the value get from the technode dropdown button and display the respective results in lalyer dropdown box. 我需要传递从technode下拉按钮获取的值,并将相应的结果显示在lalyer下拉框中。 How can I do that? 我怎样才能做到这一点? Thanks in advance. 提前致谢。

EDITTED 已编辑

Now I hardcoded the options in JS file. 现在,我将选项硬编码在JS文件中。 But I need to replace this options in python way (Like I did to get the fab). 但是我需要以python方式替换此选项(就像我确实得到fab一样)。 How to do that? 怎么做?

The usage for select fields is: 选择字段的用法是:

<select
  ng-model="string"
  [name="string"]
  [required="string"]
  [ng-required="string"]
  [ng-change="string"]
  [ng-options="string"]>
...
</select>

Here is the documentation: https://docs.angularjs.org/api/ng/directive/select 这是文档: https : //docs.angularjs.org/api/ng/directive/select

<select class="form-control" ng-model="selected_technode" name="technode">
                {% for t in technode %}
                <option  value="{{f}}">{{t}}</option>
                {% endfor %}
            </select>

In your controller you can watch for changes and trigger an ajax call: 在控制器中,您可以监视更改并触发ajax调用:

$scope.$watch('selected_technode', function (newVal) {
    updateLayer(newVal);
});

function updateLayer(technode) {
    var m = $resource(layerresource_url);
    var data = m.get({format: json, techtype__contains: technode});
    $scope.layer = data.objects[0];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM