【问题标题】:How to display nested object using ui-select's group by property如何使用 ui-select 的 group by 属性显示嵌套对象
【发布时间】:2018-05-04 02:03:27
【问题描述】:

所以我有一个 ui 选择组件,它显示一个具有多个属性的对象列表,一个属性是另一个对象,我需要的是显示由嵌套对象中的一个属性分组的数组对象,看:@ 987654321@。 请我真的需要帮助。提前致谢。 这就是我尝试的原因:

<ui-select-match placeholder="Elija un Nutriente...">           
    {{$select.selected.nombre}}
</ui-select-match>
<ui-select-choices repeat="a in allNutrientes| filter: $select.search" group-by="'idTiposDatosAlimentos.nombreTipoDato'"> 
    <strong>{{a.abreviatura}} </strong>
      {{a.nombre}}
    <small><strong>Tipo de Dato: </strong>
      {{a.idTiposDatosAlimentos.nombreTipoDato}}
    </small>
</ui-select-choices>

【问题讨论】:

  • 要开始使用,您可以查看ui select examples
  • 我做了,但在示例中,它们仅按数组中的属性分组,而不是嵌套数组中的属性。所以它对我不起作用,虽然我尝试了一些东西,但也没有用,而且我只是一个学生而且我是 Angular 新手,所以我的代码不太好,这就是我在这里寻求帮助的原因
  • 你能添加你的尝试吗,这样我就不会重复你已经尝试过的东西了?
  • 好的,就是这样

标签: angularjs json ui-select


【解决方案1】:

您正在使用 ui select 可用的 group by string 方法。但是,他们也有一个允许您使用函数的示例。您可以将一个函数添加到您的控制器,并将您的 group-by= 属性替换为函数名称。这通过将每个项目传递给您的函数来工作,因此该函数应该除了该项目的单个参数。对于您的情况,您可以简单地从您想要分组的字段中返回值。

yourcontroller.js

// the rest of your controller code, left out for brevity
$scope.groupByNombreTipoDato = function (item) {
    // by returning this, it will attach this as the group by key
    // and automatically group your items by this
    return item.idTiposDatosAlimentos.nombreTipoDato;
}

yourhtml.html

// rest left out for brevity
// we are using the function we added to the controller
// we don't include the parameter, it calls it properly with just the name
<ui-select-choices repeat="a in allNutrientes| filter: $select.search" group-by="groupByNombreTipoDato">

【讨论】:

  • 完美,成功了,再次感谢您的耐心和时间 PS:我赞成您的回答,但我还没有 15 名声望,所以不会显示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多