基于不工作的条件删除所有功能

问题描述 投票:0回答:1

我使用observableArray编写了一个带有Knockout的程序,我想根据条件从该数组中删除一个元素。但它没有用,我的程序是这样的:

 var app = function (appname, appdesc,isactive) {
            this.appname = appname;
            this.appdesc = appdesc;
            this.active = isactive;
        }
        var mainVM = function () {
            var app1 = new app('sap', 'sap b1', false);
            var app2 = new app('Magento', 'Magento 1', false);
            //this.appList = ko.observableArray();
            this.appList = ko.observableArray([app1, app2]);
        }
        var appVM = new mainVM();
        ko.applyBindings(appVM)

$('#btnDel').click(function () {

            appVM.appList.removeAll([{ "active": false }]);
        })
javascript knockout.js knockout-3.0
1个回答
0
投票

您正在尝试通过发送参数removeAll来执行{'active': false}函数。换句话说,你要求Knockout确定app中的任何appList实例是否等于对象{'active': false}。他们不平等!

这是app1:

{
   appname: 'sap',
   appdesc: 'sap b1',
   active: false
}

这是app2:

{
   appname: 'Magento',
   appdesc: 'Magento 1',
   active: false
}

你传递用于比较的参数是这样的:

{
   active: false
}

他们是非常不同的。


即使您创建具有相同输入的2个实例,它们也不相等。看这里:

var app = function (appname, appdesc,isactive) {
    this.appname = appname;
    this.appdesc = appdesc;
    this.active = isactive;
}

var app1 = new app('moo', 'coo', false);
var app2 = new app('moo', 'coo', false);

document.getElementById('secret').innerHTML = (app1 === app2);
is app1 equal to app2?

<br><br>

<span id="secret"></span>

因此,当您处理对象时,我建议的原始选项remove对您来说是正确的功能。

var app = function (appname, appdesc,isactive) {
            this.appname = appname;
            this.appdesc = appdesc;
            this.active = isactive;
        }
        var mainVM = function () {
            var app1 = new app('sap', 'sap b1', true);
            var app2 = new app('Magento', 'Magento 1', false);
            //this.appList = ko.observableArray();
            this.appList = ko.observableArray([app1, app2]);
        }
        var appVM = new mainVM();
        ko.applyBindings(appVM)

        $('#btnDel').click(function () {

            appVM.appList.remove(function(item){
              return !item.active;
            });
        })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<!-- ko foreach: appList -->
<span data-bind="text: ko.toJSON($data)"></span>
<br>
<!-- /ko -->

<br><br>
<button id="btnDel">Delete</button>

带参数的removeAll仅在这种情况下有用,它是一种原始数据类型:

var mainVM = function () {
            
            this.appList = ko.observableArray([false, true]);
        }
        var appVM = new mainVM();
        ko.applyBindings(appVM)

        $('#btnDel').click(function () {

            appVM.appList.removeAll([false]);
        })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<!-- ko foreach: appList -->
<span data-bind="text: ko.toJSON($data)"></span>
<br>
<!-- /ko -->

<br><br>
<button id="btnDel">Delete</button>
© www.soinside.com 2019 - 2024. All rights reserved.