Angular正在提交包含数据的表
Angular Submitting table with data
我有一个要求,我有一张包含(8*6)48个单元格的表,所有单元格都在<td>
中有输入标记,在提交该表时,我的js文件必须获取所有数据并将其POST到我的后端控制器。我的问题是,由于所有单元格都是输入文本类型,angular无法获取其中的值
var isrcorderapp = angular.module('plunker', []);
/*
isrcorderapp.directive("test1", function(){
return {
restrict:'A',
link: function($scope, $elem) {
var arr = ($($elem[0]).find('tbody tr'));
var coll = [];
for(var i=0; i < arr.length; i++){
var tr = arr[i];
var tdColl = $(tr).find('td');
var obj = [];
for(var y = 0; y < tdColl.length; y++ ){
obj.push(tdColl[y].innerHTML);
}
coll.push(obj)
}
console.log(coll);
}
}
});
*/
isrcorderapp.controller("isrcorders", function($scope,$http,$compile) {
$scope.myVal='submit';
$scope.coll={};
$scope.myFn=function(){
var total=angular.element( document.querySelector( '.table' ) ).find('tbody tr');
var coll = [];
console.log('totla'+total);
for(var i=0; i < total.length; i++){
console.log('totla'+total);
var tr = total[i];
var tdColl = $(tr).find('td');
console.log(tdColl);
var obj = [];
for(var y = 0; y < tdColl.length; y++ ){
obj.push(tdColl[y].innerHTML);
}
coll.push(obj);
console.log(coll);
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="jquery@2.0.3" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js" ></script>
<script src="app.js"></script>
</head>
<body ng-controller="isrcorders">
<form ng-submit="myFn()">
<table test1="" id="isrctable" class="table" >
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Age</th>
<th>Position</th>
<th>Office</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td><input type="text" id="row-1-age" name="row-1-age" value="61"></td>
<td><input type="text" id="row-1-position" name="row-1-position" value="System Architect"></td>
<td><select size="1" id="row-1-office" name="row-1-office">
<option value="Edinburgh" selected="selected">
Edinburgh
</option>
<option value="London">
London
</option>
<option value="New York">
New York
</option>
<option value="San Francisco">
San Francisco
</option>
<option value="Tokyo">
Tokyo
</option>
</select></td>
</tr>
</tbody>
</table>
<input type="submit" id="submit" value={{myVal}} />
</form>
</body>
</html>
当我尝试使用innerHTML属性进行查询时,我得到了完整的HTML标记,如
<input type="text" id="row-32-position" name="row-32-position" value="Developer">
我需要查询值。。需要您对此的帮助
代替:
obj.push(tdColl[y].innerHTML);
尝试:
var td = tdColl.eq(y);
var input = td.find('input,select');
if (input.length) {
obj.push(input.val());
} else {
obj.push(td.text());
}
我从你的描述中了解到了什么。
HTML:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
<script src="app.js"></script>
</head>
<body ng-controller="isrcorders">
<form ng-submit="myFn()">
<table test1="" id="isrctable" class="table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Age</th>
<th>Position</th>
<th>Office</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>
<input type="text" ng-model="table.rowone.age">
</td>
<td>
<input type="text" ng-model="table.rowone.position">
</td>
<td>
<select ng-model="table.rowone.office">
<option value="Edinburgh">
Edinburgh
</option>
<option value="London">
London
</option>
<option value="New York">
New York
</option>
<option value="San Francisco">
San Francisco
</option>
<option value="Tokyo">
Tokyo
</option>
</select>
</td>
</tr>
<tr>
<td>yoyo baby</td>
<td>
<input type="text" ng-model="table.rowtwo.age">
</td>
<td>
<input type="text" ng-model="table.rowtwo.position">
</td>
<td>
<select ng-model="table.rowtwo.office">
<option value="Edinburgh">
Edinburgh
</option>
<option value="London">
London
</option>
<option value="New York">
New York
</option>
<option value="San Francisco">
San Francisco
</option>
<option value="Tokyo">
Tokyo
</option>
</select>
</td>
</tr>
</tbody>
</table>
<button ng-click="submitTable(table)">Submit</button>
</form>
<pre>{{tableval | json}}</pre>
</body>
</html>
角度代码:
var app = angular.module('plunker', []);
app.controller('isrcorders', function($scope) {
$scope.submitTable = function(tablevalues) {
// $http.post('/url', tablevalues)
// .success(function(data, status) {
// })
// .error(function(data, status) {
// });
$scope.tableval = tablevalues;
console.log(tablevalues);
}
});
PLnkr:http://plnkr.co/edit/VCKrwe4pTjVdQiqzWpVn?p=preview
相关文章:
- Json数据包含日期和时间格式
- 数据绑定:'系统Char'不包含名为'xxxxx'
- 如何创建一个包含许多数组和数据的json对象
- 使用jQuery发布的AJAX数据包含引号
- 选项中包含数据的自定义标记
- 需要一个用于AJAX的Javascript库,其中包含多部分/表单数据
- php函数的XMLHttpRequest响应不包含任何数据
- 如何在系列数据中包含字符串以提取工具提示点格式
- AJAX数据包含一个或多个单词.
- 为什么jqPlot显示了一个错误的饼图,其中包含通过AJAX检索的数据
- 将所有angular2捆绑包包含在index.html中会使导入从内存获取数据,而不是再次从服务器请求数据
- 将.js文件中的数据包含到链接中
- 我如何采用两个日期选择器并使用我的开始和结束日期对数据进行排序,但 json 数据包含 5/12/2016,而角度仅排序
- 剪贴数据包含javascript
- 如何将数据转换为json类型,其中数据包含变量
- 获取JQuery数据-包含有空格的值
- 如果数据包含URL, Javascript将无法解析JSON
- 如何使用jsweet::is-my-json-valid糖果验证JSON数据(包含完整的错误报告)?
- 如何避免当数据包含'@'登录显示标签的链接
- 当数据包含连续问号时,jQuery $.ajax()无法理解的行为