javascript - 使用 Knockout 从 JSON 预填充选定值以进行多项选择

标签 javascript json knockout.js

我正在尝试根据提供的 json 填充已选择的选项,对于任何给定的多重选择。如果我只选择一个选择选项,它似乎工作正常,但如果我选择多个选项,我不会得到任何结果。另外,我试图在多选下方的文本中显示所选选项,但如果我更改所选选项,这似乎不起作用。

我知道我已经很接近了,因为我没有收到任何错误......那么我做错了什么?

这是我的代码:

knockout

var OptionsModel = function(options) {
var self = this;
self.options = ko.observableArray(options);
self.allFacilityCodes = ko.observable("");
self.facilityCode = ko.observable("");

self.selectedFacilityCode = ko.observable();

self.addOption = function() {
    self.options.push({
        facilityCode: ["A116", "A118", "A120", "A122", "A124", "A125", "A126", "A127", "A128", "A130", "A132", "A134", "A138", "A139", "A140", "A142", "A143", "A144", "A146", "A148", "A152", "A154", "A270", "A364", "A365", "A366", "A370"],
        accountType: "",
        customCode: "",
        facilityPT: "",
        selectedFacilityCode: ""
    });
};

self.removeOption = function(option) {
    self.options.remove(option);
};

self.save = function(form) {
    alert("Could now transmit to server: " + ko.utils.stringifyJson(self.options));
    // To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.options);
};
};

var viewModel = new OptionsModel([
{ facilityCode: ["A116", "A118", "A120", "A122", "A124", "A125", "A126", "A127", "A128", "A130", "A132", "A134", "A138", "A139", "A140", "A142", "A143", "A144", "A146", "A148", "A152", "A154", "A270", "A364", "A365", "A366", "A370"],
selectedFacilityCode: ["A116", "A125", "A127"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
{ facilityCode: ["A116", "A118", "A120", "A122", "A124", "A125", "A126", "A127", "A128", "A130", "A132", "A134", "A138", "A139", "A140", "A142", "A143", "A144", "A146", "A148", "A152", "A154", "A270", "A364", "A365", "A366", "A370"], 
selectedFacilityCode: ["A270"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
{ facilityCode: ["A116", "A118", "A120", "A122", "A124", "A125", "A126", "A127", "A128", "A130", "A132", "A134", "A138", "A139", "A140", "A142", "A143", "A144", "A146", "A148", "A152", "A154", "A270", "A364", "A365", "A366", "A370"], 
selectedFacilityCode: ["A139", "A140", "A148"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
{ facilityCode: ["A116", "A118", "A120", "A122", "A124", "A125", "A126", "A127", "A128", "A130", "A132", "A134", "A138", "A139", "A140", "A142", "A143", "A144", "A146", "A148", "A152", "A154", "A270", "A364", "A365", "A366", "A370"],    
selectedFacilityCode: ["A148"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
{ facilityCode: ["A116", "A118", "A120", "A122", "A124", "A125", "A126", "A127", "A128", "A130", "A132", "A134", "A138", "A139", "A140", "A142", "A143", "A144", "A146", "A148", "A152", "A154", "A270", "A364", "A365", "A366", "A370"],
    selectedFacilityCode: ["A130"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"}
]);
ko.applyBindings(viewModel);

// Activate jQuery Validation
$("form").validate({ submitHandler: viewModel.save });

http://jsfiddle.net/mujaji/oc9oohoh/7/

任何帮助都会很棒!

谢谢 乔

最佳答案

好吧,检查一下,我想我得到了你正在寻找的东西,减去验证。

我看到的主要问题是您确实想要选项的实例,而不仅仅是一件大事。而且,facilityCode 看起来像是一遍又一遍地重复,因此我将其设置为 OptionModel.facilityCode observable 中的文字。

将点击功能带到 Root View 模型。为其他所有内容创建一个“数据”数组,并将其映射到 viewModel 中的 self.options observableArray 中。必须稍微修改一些 html,但我认为这应该能让你继续下去。

var model;
var data = [
    { selectedFacilityCode: ["A116", "A125", "A127"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
    { selectedFacilityCode: ["A270"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
    { selectedFacilityCode: ["A139", "A140", "A148"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
    { selectedFacilityCode: ["A148"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"},
    { selectedFacilityCode: ["A130"], accountType: "Account Type text goes here", customCode: "Custom Code text goes here", facilityPT: "0"}
];

var OptionsModel = function(options) {
    var self = this;

    self.facilityCode = ko.observable(["A116", "A118", "A120", "A122", "A124", "A125", "A126", "A127", "A128", "A130", "A132", "A134", "A138", "A139", "A140", "A142", "A143", "A144", "A146", "A148", "A152", "A154", "A270", "A364", "A365", "A366", "A370"]);
    self.accountType = ko.observable(options.accountType);
    self.customCode = ko.observable(options.customCode);
    self.facilityPT = ko.observable(options.facilityPT);

    self.selectedFacilityCode = ko.observable();

};

var viewModel = function (definition) {
    var self = this;

  // Create an observableArray, using var data to store instances of OptionsModel
  self.options = ko.observableArray(ko.utils.arrayMap(definition, function(item) {
    return new OptionsModel(item);
  }));

  // Remove selected option
  self.removeOption = function(option) {
    self.options.remove(option);
  };

  // Add new option, could be extended with a small form instead of object literal
  // Form fields will still work, whatever you fill out will show when you hit submit
  self.addOption = function () {
    var emptyOption = {
      accountType: '',
      customCode: '',
      facilityCode: '',
    }
    self.options.push(new OptionsModel(emptyOption));
  }

  // Alert options observable array
  self.save = function(form) {
        alert("Could not transmit to server: " + ko.toJSON(self.options));
        // To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.options);
    };
};

model = new viewModel(data);
ko.applyBindings(model);

// Activate jQuery Validation
$("form").validate({ submitHandler: viewModel.save });

jsfiddle.net/oc9oohoh/22/

关于javascript - 使用 Knockout 从 JSON 预填充选定值以进行多项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36019445/

相关文章:

javascript - 无法在 TS 脚本中导入 Bootstrap 库

javascript - jQuery 工具选项卡自定义动画

javascript - sigma.js 如何在我自己的页面上插入插件

javascript - 从 Firebase JSON 制作 Handlebars 模板

java - 将当前时区设置为@JsonFormat 时区值

javascript - knockout js,从 TreeView 中选择的项目中找到所有父节点id

javascript - 处理滚动条和 jquery .width() 方法

javascript - 使用knockout JS和文件API验证输入文件

javascript - Knockout w/Bootstrap Modals - 模态出现两次

json - Swift 中使用 Campaign Monitor 进行 HTTP 身份验证请求