可观察的嵌套数组集合在淘汰赛中不起作用

发布于 2024-12-27 11:19:13 字数 2189 浏览 0 评论 0原文

我有公司列表,当点击每个公司时需要显示该公司的销售人员。

我为此尝试了淘汰赛。下面是脚本,您可以在 http://jsfiddle.net/habdulha/gkqeD/29/ 中找到整个脚本

初始公司已列出,但单击销售人员未加载的公司。

<!-- language: lang-js -->

var rootViewModel = function(companies) {
this.companiesModel = ko.observableArray(companies);
}

var companyModel = function(salesPersonModel) {
CompanyId = ko.observable();
Name = ko.observable();
SalesPersonList = ko.observableArray(salesPersonModel);
LoadSalesPerson = function() {

    var self = this;
    var postData = {
        companyId: this.CompanyId()
    }
    /// ajax to get the emp name based on company id
    var data = $.ajax({
        type: 'GET',
        url: '/echo/json/',
        data: postData,
        success: function(result) {
            /// result is based on companyid
            var ajaxReply = {
                "empName": "John",
                "empName": "John 1"
            };


            self.SalesPersonList = ko.observableArray(ko.mapping.fromJS(ajaxReply)());


        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
    }
  }

 var salesModel = function() {
  empName = ko.observable();
 }


  var myCompanyModelArray = new Array();
  var mySalesModelArray = new Array();
  mySalesModelArray[0] = new salesModel();
  myCompanyModelArray[0] = new companyModel(mySalesModelArray);
  var mainViewModel = new rootViewModel(myCompanyModelArray);
  ko.applyBindings(mainViewModel);

  $(document).ready(function() {
      var data = $.ajax({
      type: 'GET',
      url: '/echo/json/',
      //// get all the companies
      success: function(data) {
        var ajaxReply = {
            {
                "CompanyId ": "1",
                "Name ": "Comp 1"
            }, {
                "CompanyId ": "2",
                "Name ": "Comp 2"
            }
        };
        residencyViewModel.yearGroupModel(ko.mapping.fromJS(ajaxReply)());

    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
   });

  });

I have company list, when clicking on each company need to show the salespersons of that company.

I tried knockout for this. below is script and the whole you can find in http://jsfiddle.net/habdulha/gkqeD/29/

Initial company is listed but clicking on the company the salespersons are not loaded in.

<!-- language: lang-js -->

var rootViewModel = function(companies) {
this.companiesModel = ko.observableArray(companies);
}

var companyModel = function(salesPersonModel) {
CompanyId = ko.observable();
Name = ko.observable();
SalesPersonList = ko.observableArray(salesPersonModel);
LoadSalesPerson = function() {

    var self = this;
    var postData = {
        companyId: this.CompanyId()
    }
    /// ajax to get the emp name based on company id
    var data = $.ajax({
        type: 'GET',
        url: '/echo/json/',
        data: postData,
        success: function(result) {
            /// result is based on companyid
            var ajaxReply = {
                "empName": "John",
                "empName": "John 1"
            };


            self.SalesPersonList = ko.observableArray(ko.mapping.fromJS(ajaxReply)());


        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
    }
  }

 var salesModel = function() {
  empName = ko.observable();
 }


  var myCompanyModelArray = new Array();
  var mySalesModelArray = new Array();
  mySalesModelArray[0] = new salesModel();
  myCompanyModelArray[0] = new companyModel(mySalesModelArray);
  var mainViewModel = new rootViewModel(myCompanyModelArray);
  ko.applyBindings(mainViewModel);

  $(document).ready(function() {
      var data = $.ajax({
      type: 'GET',
      url: '/echo/json/',
      //// get all the companies
      success: function(data) {
        var ajaxReply = {
            {
                "CompanyId ": "1",
                "Name ": "Comp 1"
            }, {
                "CompanyId ": "2",
                "Name ": "Comp 2"
            }
        };
        residencyViewModel.yearGroupModel(ko.mapping.fromJS(ajaxReply)());

    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
   });

  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2025-01-03 11:19:13

问题出在这行代码中:

self.SalesPersonList = ko.observableArray(ko.mapping.fromJS(ajaxReply)());

SalesPersonList 是一个可观察数组,因此当它绑定到您的 UI 时,Konckout 将观察该数组的变化。在上面的代码中,您只需用新实例替换数组。为了完成这项工作,您需要替换绑定到 UI 的数组的内容,而不是替换数组本身......

self.SalesPersonList.RemoveAll();
for(var item in ko.mapping.fromJS(ajaxReply)()) {
  self.SalesPersonList.push(item);
}

The problem is in this line of code:

self.SalesPersonList = ko.observableArray(ko.mapping.fromJS(ajaxReply)());

SalesPersonList is an observable array, so when it is bound to your UI, Konckout will observe the array for changes. In the code above you are simply replacing your array with a new instance. To make this work you need to replace the contents of the array bound to your UI rather than replacing the array itself ...

self.SalesPersonList.RemoveAll();
for(var item in ko.mapping.fromJS(ajaxReply)()) {
  self.SalesPersonList.push(item);
}
网名女生简单气质 2025-01-03 11:19:13

我遇到了同样的问题,但 @ColinE 的答案对我不起作用,但这个想法是正确的...

在我的例子中,我收到 JSON 形式的项目列表,并在映射后将它们添加到可观察数组中。

$.getJSON("/Path/To/Json/Service", function (r) {

    self.SalesPersonList.removeAll();

    // loop through each item returned
    $.each(r, function (i, v) {
        // add a mapped item to the array
        self.SalesPersonList.push(ko.mapping.fromJS(v)); 
    });

});

I had the same problem but @ColinE's answer wasn't working for me but the idea was right...

In my case i am receiving the list of items as JSON and adding them to the observable array after mapping.

$.getJSON("/Path/To/Json/Service", function (r) {

    self.SalesPersonList.removeAll();

    // loop through each item returned
    $.each(r, function (i, v) {
        // add a mapped item to the array
        self.SalesPersonList.push(ko.mapping.fromJS(v)); 
    });

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文