【问题标题】:angularjs filter two valuesangularjs过滤两个值
【发布时间】:2015-09-28 02:06:00
【问题描述】:

如果解决起来不容易,我会遇到问题。在我的数据库中,我在组织中有一个表,其中一些可能是“隐藏的”。除非输入管理员提供的唯一代码,否则您可能不会在列表中看到它们。如果用户仅键入代码,则该组织可能会出现在列表中。

我的列表现在看起来像这样:

<a class="item item-avatar" ng-repeat="lugar in organizations_all | filter:search" ng-click="mostrarAreas(lugar.id)" ng-if="organizations_filter !=''" ng-show="!lugar.hide">
  <img image-lazy-src="{{lugar.photo}}" style="border-radius: 0 !important;">
  <button class="button button-icon icon ion-plus-circled" ng-click="agregarLugar(lugar.id,lugar.private);$event.stopPropagation()" style="float: right;"></button>
  <h2>{{lugar.name}}</h2>
  <p class="icon ion-ios-locked" style="font-size:20px;"  ng-show="lugar.private"></p>
</a>

Y mi json viene asi :

{ "Count" : 5,
"Organization" : [ { "code" : "",
    "hide" : false,
    "id" : "1",
    "name" : "Odebret Advisors Ltda.",
    "private" : false
  },
  { "code" : "",
    "hide" : false,
    "id" : "2",
    "name" : "MOP Ruta 5 Sur (SCL - Talca)",
    "private" : false
  },
  { "code" : "",
    "hide" : false,
    "id" : "3",
    "name" : "MOP Ruta 5 Norte (SCL - Los Vilos)",
    "private" : false
  },
  { "code" : "",
    "hide" : false,
    "id" : "4",
    "name" : "Lixsys SpA",
    "private" : true
  },
  { "code" : "lxspa",
    "hide" : true,
    "id" : "5",
    "name" : "Prueba Oculta",
    "private" : false
  }
],
"status" : 1
}

如您所见,如果为真,则有一个“隐藏”字段,必须隐藏该组织,并且“代码”字段是唯一要查找的代码,必须完整输入,而如果我们按名称过滤,您可以区分1分。

【问题讨论】:

    标签: angularjs filter ionic


    【解决方案1】:

    编辑:这是假设代码包含在您的 filter:search 参数中,因为您没有指定用户输入代码的位置。

    您最好创建自己的过滤器:

    .filter('esVisible', function () {
      return function (lugares, search) {
        var lugaresVisibles = [];
        for (var i = 0, ii = lugares.length; i < ii; i++) {
          if (!lugares[i].hide) {
            lugaresVisibles.push(lugares[i]);
          } else if (lugares[i].code == search) {
            lugaresVisibles.push(lugares[i]);
          }
        }
        return lugaresVisibles;
      };
    });
    

    如果您使用另一个 JS 实用程序库以及 LazyJS,您可能可以很容易地简化 for 循环。

    最后你的 ngRepeat 看起来像这样:

    ng-repeat="lugar in organizations_all | esVisible:search"
    

    注意:未经测试,但希望您能理解。

    【讨论】:

      【解决方案2】:

      只需在 ng-show 中使用 && 语句即可

      所以如果你有搜索输入

      ng-model="searchInput"
      

      在你拥有的控制器中

      $scope.searchInput
      

      然后在 ng-show 你可以做

      ng-show="!lugar.hide && lugar.code == searchInput"
      

      只有当 hide 设置为 false 并且他们输入了代码时,它才会显示它们。或者如果你想显示它,即使隐藏是真的,但他们输入了你可以做的代码

      ng-show ="!lugar.hide || lugar.code == searchInput"
      

      【讨论】:

      • 甚至不适合我,我正在努力适应我的问题。
      • 好吧,我在多个应用程序中多次使用它,它确实有效。你用过&& ||之前的比较和逻辑运算符?
      猜你喜欢
      • 2020-03-21
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多