Javascript 在敲除JS中更改下拉列表时未更新网格值

Javascript 在敲除JS中更改下拉列表时未更新网格值,javascript,knockout.js,Javascript,Knockout.js,请在评论部分找到更新的小提琴 我已经提供了HTML代码和ViewModel。在更改下拉列表中的值时,我希望更新网格中的pip值。正在为新的下拉列表值计算pip值,但返回值无法将该值绑定到网格。要在dropdownAValue更改上的UI中更改seats,您需要先声明空的可观察数组: self.seats = ko.observableArray(); 并在subscribe函数中更新它,而不是每次创建新的observableArray: self.seats([new PipCalculati

请在评论部分找到更新的小提琴


我已经提供了HTML代码和ViewModel。在更改下拉列表中的值时,我希望更新网格中的pip值。正在为新的下拉列表值计算pip值,但返回值无法将该值绑定到网格。

要在
dropdownAValue
更改上的UI中更改
seats
,您需要先声明空的可观察数组:

self.seats = ko.observableArray();
并在subscribe函数中更新它,而不是每次创建新的
observableArray

self.seats([new PipCalculation(self.rates[0], self.rates[0].price, self.rates, self.newLot[0],currentlotvalue) ]);

请参见

要在
dropdownAValue
上的UI中更改
座位
更改,您需要首先声明空的可观察数组:

self.seats = ko.observableArray();
并在subscribe函数中更新它,而不是每次创建新的
observableArray

self.seats([new PipCalculation(self.rates[0], self.rates[0].price, self.rates, self.newLot[0],currentlotvalue) ]);

请参见

要在
dropdownAValue
上的UI中更改
座位
更改,您需要首先声明空的可观察数组:

self.seats = ko.observableArray();
并在subscribe函数中更新它,而不是每次创建新的
observableArray

self.seats([new PipCalculation(self.rates[0], self.rates[0].price, self.rates, self.newLot[0],currentlotvalue) ]);

请参见

要在
dropdownAValue
上的UI中更改
座位
更改,您需要首先声明空的可观察数组:

self.seats = ko.observableArray();
并在subscribe函数中更新它,而不是每次创建新的
observableArray

self.seats([new PipCalculation(self.rates[0], self.rates[0].price, self.rates, self.newLot[0],currentlotvalue) ]);

参见

我的答案将是对Max Brodin给出的内容的补充

我们可以通过这样做进一步规范您的代码

查看模型:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
工作小提琴

要做的事情:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
  • 无需另外编写订阅下拉列表来订阅更改。您需要做的就是在您的案例中进一步发送right
    DD
    value实例
  • 当您在DD subscribe中填充observablearray时,即每次下拉列表更改时,性能都会受到影响。这可以通过像我上面提到的那样来避免
  • 与其推进到可观测阵列中,不如推进到普通阵列中,然后再使用该阵列(性能得到改进)-max在其评论中提到
还有一种方法可以这样做(我以前是这样做的)


有关这方面的更多信息,请参考我的答案。

我的答案将是对
Max Brodin
给出的内容的补充

我们可以通过这样做进一步规范您的代码

查看模型:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
工作小提琴

要做的事情:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
  • 无需另外编写订阅下拉列表来订阅更改。您需要做的就是在您的案例中进一步发送right
    DD
    value实例
  • 当您在DD subscribe中填充observablearray时,即每次下拉列表更改时,性能都会受到影响。这可以通过像我上面提到的那样来避免
  • 与其推进到可观测阵列中,不如推进到普通阵列中,然后再使用该阵列(性能得到改进)-max在其评论中提到
还有一种方法可以这样做(我以前是这样做的)


有关这方面的更多信息,请参考我的答案。

我的答案将是对
Max Brodin
给出的内容的补充

我们可以通过这样做进一步规范您的代码

查看模型:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
工作小提琴

要做的事情:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
  • 无需另外编写订阅下拉列表来订阅更改。您需要做的就是在您的案例中进一步发送right
    DD
    value实例
  • 当您在DD subscribe中填充observablearray时,即每次下拉列表更改时,性能都会受到影响。这可以通过像我上面提到的那样来避免
  • 与其推进到可观测阵列中,不如推进到普通阵列中,然后再使用该阵列(性能得到改进)-max在其评论中提到
还有一种方法可以这样做(我以前是这样做的)


有关这方面的更多信息,请参考我的答案。

我的答案将是对
Max Brodin
给出的内容的补充

我们可以通过这样做进一步规范您的代码

查看模型:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
工作小提琴

要做的事情:

    self.dropdownAValue = ko.observable(0.1);

    self.seats = ko.observableArray([]);

    var newItems = ko.utils.arrayMap(self.rates,function(item){
    return new PipCalculation(item, item.price, self.rates, self.newLot[0], self.dropdownAValue)
    });
    self.seats(newItems);

    //computed code
    self.formattedPrice2 = ko.computed(function() {
           //alert("hi");
            var cur = self.rate().pair;
            //var price = self.rate().price;
            //alert(self.myQuote());
            var price = self.myQuote();
            var pip = 1;
            var lot1 =currentlotvalue().lotSize;
  • 无需另外编写订阅下拉列表来订阅更改。您需要做的就是在您的案例中进一步发送right
    DD
    value实例
  • 当您在DD subscribe中填充observablearray时,即每次下拉列表更改时,性能都会受到影响。这可以通过像我上面提到的那样来避免
  • 与其推进到可观测阵列中,不如推进到普通阵列中,然后再使用该阵列(性能得到改进)-max在其评论中提到
还有一种方法可以这样做(我以前是这样做的)


有关这方面的更多信息,请参考我的答案。

是的,它有效(是的,我们需要首先声明),但我们为什么要使用
self.seats([new xyz()])
而不是
self.seats.push(new xyz())
。第二种方法没有显示任何错误,但网格不会更新。它们是不同的
Push
将值添加到数组中,而
self.seats([new xyz()])
将旧值替换为新值。两者都应该工作,但您应该注意性能问题,请参阅本文@supercool您的代码按预期工作它会在
seats
数组的末尾添加值。为了替换数组值,您有以下选项:1。清除数组并将项目推送到它
self.seats([])
self.seats.push(
)。这不是最好的选择,因为它会迫使用户界面在每次推送值时更新,所以速度很慢。2.创建临时平面值数组并用它更新
seats
。请参阅感谢man+1,因为它如此清晰。ac