【问题标题】:How to get row data from table using angularjs如何使用angularjs从表中获取行数据
【发布时间】:2014-10-12 20:42:13
【问题描述】:

我是 angularjs 的新手。我面临一个问题。我有一张带有一个单选按钮的桌子。当用户选择任何单选按钮并单击提交按钮时,我需要显示所选行的详细信息。

HTML代码:

<table>
    <thead>
        <tr>
            <th>Select</th> 
            <th>First Name</th> 
            <th>Last Name</th> 
            <th>Unique Reference ID</th> 
            <th>Country</th> 
            <th>Branch</th> 
            <th>Card No</th> 
       </tr>
    </thead>

    <tr ng-repeat="d in Employees">
        <td><input type="radio"></td>
        <td>{{d.fname}}</td>
        <td>{{d.lname}}</td>
        <td>{{d.uniqueid}}</td>
        <td>{{d.country.name}}</td>
        <td> {{d.branch.name}}</td>
        <td>{{d.cardno}}</td> 
    </tr>
</table>

<table>
    <tr>
        <td><input type="button" value="Edit" ng-click="Edit()"></td>
    </tr>
</table>

控制器代码:

$scope.Edit=function()
{
    //need id of selected row ?
};

【问题讨论】:

  • 您将复选框和编辑功能放在 ng-repeat 中,您可以在其中访问单个员工项目 d,还可以访问数组的 $index
  • @Mohammad 我无法更改要求。这只能是上述格式。谢谢你的建议
  • 请详细说明不清楚。您已经在表格中显示了详细信息

标签: angularjs


【解决方案1】:

类似这样的:

<input type="radio" ng-click="editData.employee = d">

还有控制器:

$scope.editData = {};

$scope.Edit = function() {
  var id = $scope.editData.employee.id;
};

我刚刚回答了另一个问题,即ng-repeat 为每个列表项创建一个新范围,这就是您需要editData 对象使其工作的原因。它可以帮助您了解为什么需要它:Angular replace data from ng-repeat item

【讨论】:

  • 得到错误 $scope.employeeToEdit undefined..你能详细说明你的代码
  • 抱歉,忘记 employeeToEdit 属性是在 ng-repeat 范围内设置的,而不是在您的 Edit 方法所在的范围内。我已经更新了我的答案。
  • 当我尝试使用“警报”打印 id 时,它给出了“未定义”值。
  • 试试console.log($scope.editData.employee)。看起来你的 id 叫做uniqueid,所以你应该使用.uniqueid 而不是.id
  • 对不起我的打字错误..谢谢。你真的拯救了我的一天。
【解决方案2】:

您需要在无线电输入中使用ng-modelng-value 属性;见http://docs.angularjs.org/api/ng/input/input%5Bradio%5D

您还需要在格式化您的 html 时至少保持理智。

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多