热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Angular过滤器服务器端生成select(下拉列表)-Angularfilterserversidegeneratedselect(dropdown)

Iwonderifitispossibletohavea<select>which<option>elementsaregeneratedont

I wonder if it is possible to have a 哪个

Let's assume the following scenario. We have two 元素。




So when parent option is selected then filter the child

Should I make a routine to read all the options to an arrays in the Javascript?

我应该制作例程来读取Javascript中数组的所有选项吗?

Or should I make ajax calls to the server to obtain the data as json and then populate it(this I saw is the traditional way of doing the trick and then the HTML of the selects is generated on client side)?

或者我应该对服务器进行ajax调用以获取json的数据,然后填充它(我看到这是传统的做法,然后在客户端生成选择的HTML)?

I would like to keep HTML been generated on the server side. BTW server is Apache and I use PHP.

我想保持HTML在服务器端生成。 BTW服务器是Apache,我使用PHP。

Thanks.

2 个解决方案

#1


0  

I highly recommend you to use AngularJS to generate HTML on the client side, and get the JSON data from the server.

我强烈建议您使用AngularJS在客户端生成HTML,并从服务器获取JSON数据。

Trigger action on parent select change


This will trigger filterChildSelect(parentSelectValue) on your controller when the value selected changes.

当选定的值发生更改时,这将触发控制器上的filterChildSelect(parentSelectValue)。

Populate child select with server data

In your controller define the variable with your JSON data. For the sake of simplicity I will define an static array, but this should be the JSON data from your server

在您的控制器中使用您的JSON数据定义变量。为了简单起见,我将定义一个静态数组,但这应该是来自服务器的JSON数据

$scope.childDataOriginal = [
    {
        name: "Child name 1",
        value: "child1"
    }, {
        name: "Child name 2",
        value: "child2"
    }
];
// Iterave over a copy of the original data in order not to modify it when filtering
$scope.childData = angular.copy($scope.childDataOriginal);

Then iterave over it in your view:

然后在您的视图中迭代它:


Define your filtering function in your controller

Define a function which iterates over your original data and pushes to a temprary array the elements which pass some filter. Then assign that temprary array to your $scope.childData array.

定义一个迭代原始数据的函数,并将通过某些过滤器的元素推送到temprary数组。然后将该temprary数组分配给$ scope.childData数组。

$scope.filterChildSelect = function (parentSelectedValue) {
    var i,
        childDataTmp = []

    for (i = 0; i <$scope.childDataOriginal.length; i = i + 1) {
        if (someCondition) {
            childDataTmp.push($scope.childDataOriginal[i]);
        }
    }

    $scope.childData = childDataTmp;
}

#2


0  

I think that a problem with generating this server-side is after you have selected an option in the parent (which filters the child options), and then select another option in paren, you need to keep track of what options were there to begin with.

我认为生成这个服务器端的问题是在父选择了一个选项(过滤子选项),然后在paren中选择另一个选项后,你需要跟踪哪些选项开始。

I would suggest providing angular with the options for both 提供angular选项。

If angular has the list of options and they are structured something like this, the filtering would be trivial.

如果angular具有选项列表并且它们的结构类似于此,则过滤将是微不足道的。

...
function controller() {
  var vm = this;
  vm.parentValue = null;
  vm.childValue = null;

  vm.parentOptiOns= [
    {key: 'parent 1', value: 1},
    {key: 'parent 2', value: 2},
  ];
  vm.childOptiOns= [
    {key: 'parent 1 child 1', value: 'parent1_child1', category: 1},
    {key: 'parent 1 child 2', value: 'parent1_child1', category: 1},
    {key: 'parent 2 child 1', value: 'parent2_child1', category: 2},
    {key: 'parent 2 child 2', value: 'parent2_child2', category: 2},
  ];
}
...


...
...

推荐阅读
  • Itwasworkingcorrectly,butyesterdayitstartedgiving401.IhavetriedwithGooglecontactsAPI ... [详细]
  • 移动端常用单位——rem的使用方法和注意事项
    本文介绍了移动端常用的单位rem的使用方法和注意事项,包括px、%、em、vw、vh等其他常用单位的比较。同时还介绍了如何通过JS获取视口宽度并动态调整rem的值,以适应不同设备的屏幕大小。此外,还提到了rem目前在移动端的主流地位。 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • 在Android中解析Gson解析json数据是很方便快捷的,可以直接将json数据解析成java对象或者集合。使用Gson解析json成对象时,默认将json里对应字段的值解析到java对象里对应字段的属性里面。然而,当我们自己定义的java对象里的属性名与json里的字段名不一样时,我们可以使用@SerializedName注解来将对象里的属性跟json里字段对应值匹配起来。本文介绍了使用@SerializedName注解解析json数据的方法,并给出了具体的使用示例。 ... [详细]
  • Javascript中带有加号 - 减号(±)的极坐标曲线方程 - Polar curve equation with plus-minus sign (±) in Javascript
    IamtryingtodrawpolarcurvesonHTMLcanvasusingJavascript.WhatshouldIdowhenIwanttoco ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 本文介绍了如何使用jQuery和AJAX来实现动态更新两个div的方法。通过调用PHP文件并返回JSON字符串,可以将不同的文本分别插入到两个div中,从而实现页面的动态更新。 ... [详细]
  • 设计模式——模板方法模式的应用和优缺点
    本文介绍了设计模式中的模板方法模式,包括其定义、应用、优点、缺点和使用场景。模板方法模式是一种基于继承的代码复用技术,通过将复杂流程的实现步骤封装在基本方法中,并在抽象父类中定义模板方法的执行次序,子类可以覆盖某些步骤,实现相同的算法框架的不同功能。该模式在软件开发中具有广泛的应用价值。 ... [详细]
  • 大厂首发!思源笔记docker
    JVMRedisJVM面试内存模型以及分区,需要详细到每个区放什么?GC的两种判定方法GC的三种收集方法:标记清除、标记整理、复制算法的 ... [详细]
  • php连接mysql显示数据,php连接mysql数据库的算法思想
    本文目录一览:1、怎么用php显示mysql数据表数据 ... [详细]
  • 《PHP精粹:编写高效PHP代码》——2.7节设计数据库
    本节书摘来自华章社区《PHP精粹:编写高效PHP代码》一书中的第2章,第2.7节设计数据库,作者:(美 ... [详细]
author-avatar
lksxq_468
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有