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

时间:2016-01-12 03:18:31

标签: javascript jquery angularjs lodash linq.js

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

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

目前我有一个点击功能,可以输入匹配的部分历史记录。这是一个看起来像的屏幕截图。

history model

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

new model names

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

工作人员 working 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;
};

3 个答案:

答案 0 :(得分:1)

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

$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

答案 1 :(得分:1)

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

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

-egg.info

答案 2 :(得分:1)

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

检查出来:

<tr>
    <td colspan="10"><button class="copyRow">Copy complete row</button></td>
</tr>

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。我希望它有所帮助