【问题标题】:Filter user details when clicking on a radio button?单击单选按钮时过滤用户详细信息?
【发布时间】:2017-04-11 23:22:47
【问题描述】:

单击单选按钮时如何过滤用户详细信息?

Plunker

*实际上,我想在列表页面中过滤身份验证用户问题,所以我创建了一个 plunker。如果我是经过身份验证的用户(或)管理员,我只想在列表页面中过滤 myquestions

例如:- 在 plunker 我的displayNameTable 1 意味着如果我点击My Question 单选按钮,我的问题只需要filter

我的 HTML 单选按钮:-

<input type="radio" name="myquestion" data-ng-model="myquestion.user.displayName" value="myquestion" >

我的 HTML 过滤器:-

    ng-repeat="question in questions | filter: myquestion"

**My Html Data:-**

<div ng-repeat="question in questions | filter: myquestion">
    <small>
                      Posted on
                      <span data-ng-bind="question.created | date:'mediumDate'"></span>
                      <span data-ng-bind="question.user.displayName"></span>
                  </small>

  </div>

我的控制器数据:-

$scope.questions = [
{
"_id": "583433ddc021a5d02949a51b",
"user": {
"_id": "5834336ac021a5d02949a51a",
"displayName": "mani R",
"location": "ICF",
"dob": "1991-10-05T18:30:00.000Z",
"religion": "Christian",
"roles": [
"user"
],
"profileImageURL": "modules/users/client/img/profile/default.png"
},
"__v": 0,
"upvoters": [],
"category": "Moral Ethics",
"content": "Dhoni",
"title": "which batsman is best in the world?",
"created": "2016-11-22T12:02:37.376Z"
},
{
"_id": "582c34b3ff26bd603e1e5383",
"user": {
"_id": "58072aba0f82a61823c434df",
"displayName": "Table 1",
"dob": "1991-10-04T18:30:00.000Z",
"location": "Icf",
"religion": "Hindu",
"roles": [
"admin"
],
"profileImageURL": "./modules/users/client/img/profile/uploads/c756711556ab6864f408697c7a98aedc"
},
"__v": 1,
"upvoters": [],
"users": [],
"comments": [
{
"created": 1479365394684,
"email": "ms@e21designs.com",
"name": "Table 1",
"commentText": "Dhoni"
}
],
"friend_tag": [],
"emotion": "Confused",
"category": "Environment & Health",
"content": "sachin?",
"title": "who is best player in this world?",
"created": "2016-11-16T10:28:03.859Z"
},
{
"_id": "582c3418ff26bd603e1e5382",
"user": {
"_id": "582c1f4b53cf7fec2ddf282e",
"displayName": "selvam R",
"dob": "1991-10-04T18:30:00.000Z",
"roles": [
"kp"
],
"profileImageURL": "modules/users/client/img/profile/default.png"
},
"__v": 0,
"upvoters": [],
"category": "Environment & Health",
"content": "he is tennis player",
"created": "2016-11-16T10:25:28.835Z"
},
{
"_id": "582ad554714543e037cf3bf2",
"user": {
"_id": "58072aba0f82a61823c434df",
"displayName": "Table 1",
"dob": "1991-10-04T18:30:00.000Z",
"location": "Icf",
"religion": "Hindu",
"roles": [
"admin"
],
"profileImageURL": "./modules/users/client/img/profile/uploads/c756711556ab6864f408697c7a98aedc"
},
"__v": 1,
"upvoters": [
"ms@e21designs.com"
],
"upvotes": 1,
"category": "Moral Ethics",
"created": "2016-11-15T09:28:52.403Z"
},
{
"_id": "5821e7c667b70aac2b8fdfdc",
"user": {
"_id": "58072aba0f82a61823c434df",
"displayName": "Table 1",
"dob": "1991-10-04T18:30:00.000Z",
"location": "Icf",
"religion": "Hindu",
"roles": [
"admin"
],
"profileImageURL": "./modules/users/client/img/profile/uploads/c756711556ab6864f408697c7a98aedc"
},
"__v": 1,
"upvoters": [
"ms@e21designs.com"
],
"upvotes": 1,
"category": "Religion & Culture",
"created": "2016-11-08T14:57:10.354Z"
}
]
  • 我们尝试了alt="{{vm.authentication.user.displayName}}",但效果不佳。

【问题讨论】:

    标签: html css angularjs meanjs


    【解决方案1】:

    我认为您要做的是根据登录用户的 displayName 过滤列表,对吧?我不知道你们是否有用户身份验证服务我只是假设表 1 是给定的登录用户。我使用ng-change 来检测单选按钮是否更改为我过滤所有和我的问题。

    在我的控制器上:

    $scope.authUser = {
        "_id": "58072aba0f82a61823c434df",
        "displayName": "Table 1",
        "dob": "1991-10-04T18:30:00.000Z",
        "location": "Icf",
        "religion": "Hindu",
        "roles": [
          "admin"
        ],
        "profileImageURL": "./modules/users/client/img/profile/uploads/c756711556ab6864f408697c7a98aedc"
    };
    

    在 html 上:

       <div class="col-md-3" id="donate">
          <label class="green">
            <input type="radio" name="myquestion" ng-model="myQuestion" value="myquestion" ng-change="stateChange(myQuestion)">
            <span>My Questions</span>
          </label>
          <label class="green">
            <input type="radio" name="myquestion" ng-model="myQuestion" value="all" ng-change="stateChange(myQuestion)">
            <span>All</span>
          </label>
       </div>
    
       <div ng-repeat="question in questions | filter: { user.displayName: searchText }">
          <small>
            Posted on
            <span data-ng-bind="question.created | date:'mediumDate'"></span>
            <span data-ng-bind="question.user.displayName"></span>
          </small>
       </div>
    

    对于工作示例,我编辑了您的 plunkr。链接在这里 http://plnkr.co/edit/U5M29NMfvtY0vTnQyjmT?p=preview

    【讨论】:

    • 感谢您的宝贵回答,请看我的 plunker,我在那边给了单选按钮,我们期待 onclickingradio button 它应该只过滤记录的用户问题....例如Table 1 已记录意味着table 1 的详细信息只需要显示...您的答案几乎完成...但是缺少单选按钮过滤方法的点击,所以请帮助我...然后我们有@987654329 @ 也请给我确切的解决方案谢谢....
    • 有什么解决办法吗? ...在我的local host 中,过滤器使用这个过滤器ng-repeat="question in vm.questions | filter: vm.authentication.user.displayName" 可以完美地工作,但是我们期望on clickingradio button 它需要过滤......所以我如何在无线电输入字段中使用它...... .please 查看我的 plunker 并更新解决方案...谢谢..
    • 抱歉回复晚了。我更新了我的答案,希望对您有所帮助。
    • 请注意:过滤时要小心,不要指定可能会得到错误数据的字段,因为它会比较所有字段:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多