当前行的ng-hide和ng-show

时间:2017-12-04 05:44:24

标签: javascript jquery angularjs

我有一张桌子。而不是使用angular进行行隐藏和显示以进行编辑和保存。 它工作正常。

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


现在,当我点击表格行中的编辑按钮时,将隐藏编辑按钮并显示保存按钮。

$scope.editRow = function(id){
    console.log(id);\\row id will be displayed here
    $scope.editable=false;
}

这里面临一个问题,如果我点击第二行的编辑,只有第二行应该是可编辑的。
我知道我们可以在jquery中使用行ID轻松地做到这一点。但是我不知道如何以角度为ng-hideng-show执行此操作。

任何帮助?谢谢!!

代码:

<table>
<thead>
<tr>                                
    <th>Qualification</th>
    <th>Course</th>
    <th>Grade Attained</th>                                
    <th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="detail in educationDetails">
    <td>
        <span ng-show="editable">{{detail.qualification}}</span>  
        <input type="text" ng-model="detail.qualification" ng-hide="editable"/>
    </td>
    <td>
        <span ng-show="editable">{{detail.education_type}}</span>   
        <input type="text" ng-model="detail.education_type" ng-hide="editable"/>
    </td>
    <td>
        <span ng-show="editable">{{detail.grade}}</span>          
        <input type="text" ng-model="detail.grade" ng-hide="editable"/>
    </td>
    <td>      
        <div ng-show="editable">
            <span><i class='fa fa-trash-o' ng-click="deleteEducationDetail(detail)"></i></span>
            <span><i class='fa fa-pencil' ng-click="editEducationDetail(detail)"></i></span>
        </div>
        <div ng-hide="editable">
            <span><i class='fa fa-floppy-o' ng-click="updateEducationDetail(detail)"></i></span>
            <span><i class='fa fa-times' ng-click="cancelEducationDetail(detail)"></i></span>
        </div>
    </td>
</tr>

</tbody>
</table>





$scope.init = function () {
$scope.editable = true;
}
$scope.editEducationDetail = function (detail) {
detail.editable = false;
}

2 个答案:

答案 0 :(得分:1)

为了使每一行都可编辑,您可以在函数内部传递行对象,并使editable为true,如下所示:

 $scope.editRow = function(rowobject){
    rowobject.editable=true;
 }

代码:

<table>
<thead>
<tr>                                
    <th>Qualification</th>
    <th>Course</th>
    <th>Grade Attained</th>                                
    <th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="detail in educationDetails">
    <td>
        <span ng-show="editable">{{detail.qualification}}</span>  
        <input type="text" ng-model="detail.qualification" ng-hide="editable"/>
    </td>
    <td>
        <span ng-show="editable">{{detail.education_type}}</span>   
        <input type="text" ng-model="detail.education_type" ng-hide="editable"/>
    </td>
    <td>
        <span ng-show="editable">{{detail.grade}}</span>          
        <input type="text" ng-model="detail.grade" ng-hide="editable"/>
    </td>
    <td>      
        <div ng-show="editable">
            <span><i class='fa fa-trash-o' ng-click="deleteEducationDetail(detail)"></i></span>
            <span><i class='fa fa-pencil' ng-click="editEducationDetail(detail)"></i></span>
        </div>
        <div ng-hide="editable">
            <span><i class='fa fa-floppy-o' ng-click="updateEducationDetail(detail)"></i></span>
            <span><i class='fa fa-times' ng-click="cancelEducationDetail(detail)"></i></span>
        </div>
    </td>
</tr>

</tbody>
</table>





$scope.init = function () {
$scope.editable = true;
}
$scope.editEducationDetail = function (detail) {
detail.editable = false;
}

答案 1 :(得分:1)

为了实现这一点,您需要为每一行显示/隐藏属性。您可以在ng-init中初始化属性并使用它来显示/隐藏行。您还可以使用以下代码在html中执行所有显示/隐藏逻辑。

<tr ng-repeat="detail in educationDetails" ng-init="detail.editable = false">
    <td>
        <span ng-show="!detail.editable">{{detail.qualification}}</span>
        <input type="text" ng-model="detail.qualification" ng-show="detail.editable" />
    </td>
    <td>
        <span ng-show="!detail.editable">{{detail.education_type}}</span>
        <input type="text" ng-model="detail.education_type" ng-show="detail.editable" />
    </td>
    <td>
        <span ng-show="!detail.editable">{{detail.grade}}</span>
        <input type="text" ng-model="detail.grade" ng-show="detail.editable" />
    </td>
    <td>
        <div ng-show="editable">
            <span><i class='fa fa-trash-o' ng-click="deleteEducationDetail(detail)"></i></span>
            <span><i class='fa fa-pencil' ng-click="detail.editable = true"></i></span>
        </div>
        <div ng-hide="editable">
            <span><i class='fa fa-floppy-o' ng-click="updateEducationDetail(detail)"></i></span>
            <span><i class='fa fa-times' ng-click="detail.editable = false"></i></span>
        </div>
    </td>
</tr>