Javascript 如何在ng repeat中绑定来自动态命名输入的数据
我的目标是能够将数据从一个表行复制到另一个表行。 如果2015年的数据与2016年相比没有变化,用户需要一种快速的方法将值复制到2016年的输入字段中。这些模型是为这些表单动态创建的。您在此图像中看到的数据被指定给一个截面。输入模型包括名称“价格最低+区段id”、“价格最高+区段id”等。。。 历史模型没有将节id添加到模型名称的末尾。所以需要一个映射函数,我需要帮助。我需要将历史值映射到当前模型约定,并使用这些值更新视图 目前,我有一个点击功能,带来了匹配的部分历史。这是一个屏幕截图 在同一个函数中,我使用了2016对象数组和当前的模型命名约定 我需要将历史值复制到inputArray中。我不知道我是怎么做的?我完全可以控制它的工作原理。在《劫掠者》中,你会看到我是如何做到这一点的。如果我需要改变一些其他的东西来让它工作,那就没问题了。javascript、jquery、lodash、linq.js目前正在项目中使用 工作打捞器Javascript 如何在ng repeat中绑定来自动态命名输入的数据,javascript,jquery,angularjs,lodash,linq.js,Javascript,Jquery,Angularjs,Lodash,Linq.js,我的目标是能够将数据从一个表行复制到另一个表行。 如果2015年的数据与2016年相比没有变化,用户需要一种快速的方法将值复制到2016年的输入字段中。这些模型是为这些表单动态创建的。您在此图像中看到的数据被指定给一个截面。输入模型包括名称“价格最低+区段id”、“价格最高+区段id”等。。。 历史模型没有将节id添加到模型名称的末尾。所以需要一个映射函数,我需要帮助。我需要将历史值映射到当前模型约定,并使用这些值更新视图 目前,我有一个点击功能,带来了匹配的部分历史。这是一个屏幕截图 在同
我不知道你为什么要使用如此复杂的数据结构,但以下是我对它的看法
$scope.copyHistoryData = function (section, input) {
var historyId=input.model.split('-')[0];
var historyVal=section.sectionHistory[section.sectionHistory.length-1][historyId];
$scope.model[input.model]=historyVal;
};
要填充所有字段,请执行以下操作:
$scope.copyHistoryData = function (section) {
angular.forEach(section.sectionHistory[section.sectionHistory.length-1], function (historyVal, historyId) {
var inputModel=historyId+"-"+section.section_id;
$scope.model[inputModel]=historyVal;
});
};
我同意@ssh。数据结构一团糟。我认为这是一个更好的代表它应该是什么样子。可能不是最好的,但是您不应该遍历数据然后这样显示它
-
SecID
{{section.section_id}}
-
节名
{{section.section_name}
复制行
{{label.label}
{{section.section_history[label.index]}
复制
我已经检查了您的代码,我同意@Steven Kaspar的观点,每行的锚都没有多大意义。我已经用jQuery解决了这个问题(我知道它不符合您的Angular方案,但这是另一个解决方案)。
我添加了一个新的
,在里面添加了一个按钮
看看这个:
<tr>
<td colspan="10"><button class="copyRow">Copy complete row</button></td>
</tr>
这里是最新的。我希望它能有所帮助如果不是太多,你能为此制作一个.git回购协议吗?这里有一个教程,但我认为这太费劲了。无论如何,为了澄清,您希望将第一行中的值复制到第二行?是的,正确。谢谢你的链接。我想听听你的建议。这个函数能满足你的需要吗?我能一次复制整行吗。我只是把ng点击每个td,因为它是快速感谢的建议。我一直在寻找更简单、更好的做事方式
<div class="hpanel" ng-repeat="section in sections">
<div class="panel-body">
<div class="row">
<div class="col-xs-12">
<ul class="list-inline">
<li>
<h5>
<b>SecID</b>
</h5>
<span>{{section.section_id}}</span>
</li>
<li>
<h5>
<b>Section Name</b>
</h5>
<span>{{section.section_name}}</span>
</li>
</ul>
<hr/>
<button ng-click="section.new_section_history = section.section_history">copy row</button>
<table>
<tr>
<td ng-repeat="label in labelIndex">
{{label.label}}
</td>
</tr>
<tr>
<td ng-repeat="label in labelIndex">
{{section.section_history[label.index]}}
</td>
</tr>
<tr>
<td ng-repeat="label in labelIndex">
<input ng-model="section.new_section_history[label.index]"/>
</td>
</tr>
<tr>
<td ng-repeat="label in labelIndex">
<button ng-click="section.new_section_history[label.index] = section.section_history[label.index]">copy</button>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<tr>
<td colspan="10"><button class="copyRow">Copy complete row</button></td>
</tr>
$(document).on("click", ".copyRow", function(){
var $btn = $(this),
$tbody = $btn.parent().parent().parent(),
$trs = $tbody.find("tr");
$.each($trs.eq(0).find("td"), function(index, td){
$trs.eq(1).find("td").eq(index).find("input").val(parseFloat($(td).text().replace("$", "")));
});
})