如何绑定ng-repeat中动态命名输入的数据

我的目标是能够将数据从表行复制到另一个表行。 表行

如果2015年的数据从2016年开始没有变化,则用户需要快速将值复制到2016年输入字段中。 为这些表单动态创建模型。 您在此图像中看到的数据将分配给一个部分。 输入模型名称为“price_min + section_id”,price_max + section_id’等…历史模型没有将section_id添加到模型名称的末尾。 所以需要一个我需要帮助的映射function。 我需要将历史值映射到当前模型约定并使用值更新视图。

目前我有一个点击function,带来匹配的部分历史记录。 这是一个看起来像的屏幕截图。

历史模型

在同一个函数中,我有2016年的对象数组与当前的模型命名约定。

新型号名称

我需要将历史值复制到inputArray中。 我怎么做这个,我不知道? 我完全控制了它的工作原理。 在plunker你会看到我是如何做到这一点的。 如果我需要改变其他东西来使这项工作,那就没关系。 javascript,jquery,lodash,linq.js目前正在项目中使用。

工作的plunker 工作plunker

$scope.copyHistoryData = function (section) { var selected = Enumerable.From(sectionsHistory).Where("x => x.section_id == '" + section.section_id + "'").ToArray(); selected = selected[0]; var inputArry = section.sectionInputs; }; 

我不确定你为什么使用这种复杂的数据结构,但这是我的看法

 $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; }); }; 

http://plnkr.co/edit/OOEmgzKB1pqKjSJMayVF?p=preview

我同意@ssh。 数据结构很乱。 我认为这是一个更好的代表它应该是什么样子。 可能不是最好的,但你不应该遍历数据,然后像那样显示它。

http://plnkr.co/C9DWV1dSvkk8lcYdm0An?p=preview

 
  • SecID
    {{section.section_id}}
  • Section Name
    {{section.section_name}}

{{label.label}}
{{section.section_history[label.index]}}

我检查了你的代码,我同意@Steven Kaspar,每一行中的锚都没有多大意义。 我用jQuery解决了它(我知道它不遵循你的Angular方案,但它是另一个解决方案)。 我添加了一个新的

来在其中添加一个button

看一下这个:

    

app.js

 $(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("$", ""))); }); }) 

这是更新的plunker 。 我希望它有所帮助