推入前敲除检查是否存在于observableArray中 [英] Knockout Check If Existing in observableArray before Push

查看:110
本文介绍了推入前敲除检查是否存在于observableArray中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Knockout的新手,但我在阻止我完成简单的项目时遇到了一些问题.
我有一个名为 loanDeductions 的observableArray,该数组在带有foreach绑定的表中显示.
我有一个称为的 loanDeductionsList 另一个observableArray这也是从我的第一observableArray的JSON数据,我用它在我的下拉列表,其当选择一个值,它会推所选择的数据到我的桌子上.如果由于我无法真正清楚地解释它而没有意义,这是我的javascript文件:

I am new to Knockout and I have a little problem stopping me from completing my simple project.
I have an observableArray called loanDeductions that I display in a table with foreach binding.
I have another observableArray called loanDeductionsList which is also from the json data of my first observableArray, I used it in my drop down list which when a value is selected, it will push the selected data to my table. If it didn't make sense as I cannot really explain it clearly, this is my javascript file:

var deductionLine = function (deductionID, deductionName, amount) {
    self = this;
    self.deductionID = ko.observable(deductionID);
    self.deductionName = ko.observable(deductionName);
    self.amount = ko.observable(formatCurrency(amount));
};

function LoanDeductions(deductions) {
    var self = this;
    self.loanDeductions = ko.observableArray(ko.utils.arrayMap(deductions, function (deduction) {
        return new deductionLine(deduction.deductionID, deduction.deductionName, deduction.amount)
    }));
    self.loanDeductionsList = ko.observableArray(ko.utils.arrayMap(deductions, function (deduction) {
        return new deductionLine(deduction.deductionID, deduction.deductionName, deduction.amount)
    }));

    self.selectedDeduction = ko.observable();

    self.selectedDeduction.subscribe(function (data) {
        self.loanDeductions.push({
            deductionID: data.deductionID,
            deductionName: data.deductionName,
            amount: data.amount,
        });
    });
}

能帮我找到一种使我的函数selectedDeduction.subscribe()仅当我的loanDeductions observableArray中不存在要推送的项目时才推送数据的方法.
任何帮助将不胜感激.谢谢!

Can you help me find a way to make my function selectedDeduction.subscribe() push the data ONLY when the item to be pushed is not existing in my loanDeductions observableArray.
Any help will be greatly appreciated. Thank you!



我有点意识到,填充下拉列表的方法可能不是最好的方法,我愿意提出一种更好的方法来重写程序.



I am somewhat aware that the way I populate my dropdown list may o may not be the best way to do it, I am open to suggestion of a better way and rewrite my program.

推荐答案

只是为了分享我在selectedDeduction.subscribe()中添加的这一行的内容:

just to share what I did I added this line in my selectedDeduction.subscribe():

self.selectedDeduction.subscribe(function (data) {
        var match = ko.utils.arrayFirst(self.loanDeductions(), function(deduction) {
            return deduction.deductionID() === data.deductionID(); 
        });
        if (match) {
            alert(data.deductionName() + ' already exists!');
        } else {
            self.loanDeductions.push({
                deductionID: data.deductionID,
                deductionName: data.deductionName,
                amount: data.amount,
            });
            }
});

这篇关于推入前敲除检查是否存在于observableArray中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆