【问题标题】:ng-repeat highlight all rows but also deselect, selectng-repeat 突出显示所有行,但也取消选择,选择
【发布时间】:2018-02-14 03:41:34
【问题描述】:

我有一个基本的 html 表,我需要在创建表时首先突出显示所有行。此外,如果用户单击该行,它将取消突出显示并再次单击突出显示。

我点击了一行,它突出显示。如果您再次单击它会突出显示。 我只需要最初可能通过 ng-repeat 突出显示所有行。它还需要在再次单击该行时释放突出显示,然后再突出显示。 userData 只是每一行的一行文本

HTML

<table class="superusertable" cellpadding="5" cellspacing="0">
  <tbody class="table-font">
  <tr ng-init="" ng-repeat="source in userData" 
                 ng-model="source.fromSourceID" 
                 ng-class="{'sourcesSelected': source.sourcesSelected}" 
                 ng-click="select(source)">
  <td width="290px">
 <div class="action-checkbox"; width="290px">{{source.fromSourceID}}
</div>
</td>
</tr>
</tbody>
</table>

角度

 $scope.select = function(item) {
    item.sourcesSelected ? item.sourcesSelected = false : item.sourcesSelected = true;
};

【问题讨论】:

  • 不确定您的问题是什么,但您的代码确实冗长且令人困惑。写item.sourcesSelected = !item.sourcesSelected;

标签: html angularjs


【解决方案1】:

您可以在 tr 的 ng-init 属性中添加一个函数。只需传入您的项目并将其设置为 true。然后就像 Aluan 在评论中所说,您可以通过 item.sourcesSelected = !item.sourcesSelected 使您的 ng-click 功能更简单。

html

<table class="superusertable" cellpadding="5" cellspacing="0">
    <tbody class="table-font">
        <tr ng-init="init(source)" 
            ng-repeat="source in userData" 
            ng-model="source.fromSourceID" 
            ng-class="{'sourcesSelected': source.sourcesSelected}" 
            ng-click="select(source)">
            <td width="290px">
                <div class="action-checkbox"; width="290px">{{source.fromSourceID}}</div>
            </td>
        </tr>
    </tbody>
</table>

角度

$scope.select = function(item) {
    item.sourcesSelected = !item.sourcesSelected;
};

$scope.init = function(item) {
    item.sourcesSelected = true;
}

附带说明,您可以通过在检索数据时设置item.sourcesSelected = true 来完全消除ng-initinit 函数。

【讨论】:

  • 我实际上想通了,并在收到此答案时将其放入编辑中。我使用了 ng-init="highlightRows(source)" 但我会接受您的回答
【解决方案2】:

我能观察到的错误太多了。

  1. ng-init="" 不需要
  2. 三元运算符错误,你应该做以下事情:

    item.sourcesSelected = item.sourcesSelected ?假:真;

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2016-06-14
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多