JQuery 对话框中的 MVC Knockout JS

发布于 2024-12-05 03:07:19 字数 1561 浏览 2 评论 0原文

我在视图上使用knockout js来显示字段列表(即名字、姓氏等)。使用可观察数组在剔除模板内列出这些字段。该模板包含以下字段:名称(输入)、翻译(选择)和添加/删除功能。 (见下文)

var viewModel = {
    Fields: ko.observableArray([new Field(2, "First Name", 1), new Field(3, "Last Name", 2)]),
    AvailableTranslations: ko.observableArray([new Translation(1, "Prefixes"), new Translation(2, "Suffixes")]),
    remove: function(item) {
        ko.utils.arrayRemoveItem(this.Fields, item)
    },
    add: function() {
        this.Fields.push(new Field(0, "", ""));
    }
};

var Translation = function(id, name) {
    this.ID = id;
    this.Name = name;
};

var Field = function(id, name, translationID){
    this.ID = ko.observable(id);
    this.Name = ko.observable(name);
    this.TranslationID = ko.observable(translationID);
};

在模板中的翻译选择列表旁边,我想要一个选项来添加不存在的新翻译。单击该按钮时,我想打开一个 jquery UI 对话框,其中包含使用淘汰赛的部分视图。部分视图将包含翻译名称以及旧值和新值(均为文本字段)。旧值和新值是一个可观察的数组。

var viewModelPartialDialog = {
    TranslationName: ko.observable(),
    Values: ko.observableArray([]),
};

var Value = function(id, oldVal, newVal) {
    this.ID = id;
    this.OldVal = oldVal;
    this.NewVal = newVal;
};

当用户提交部分视图的表单时,我希望它进行保存调用并更新AvailableTranslations 可观察数组。

任何人都可以帮助我或指出我正确的方向来实现这一目标吗?


谢谢你的例子。这很有帮助,但不完全是我想要做的。在您的示例中,我无法将 observableArray 添加到产品中,然后在对话框的编辑模板中使用嵌套模板。

回到我原来的示例,我想在 viewModelA 中添加新字段,类似于产品列表的方式。但是,我不想编辑对话框中的字段,而是想打开一个对话框来添加新的翻译。在对话框中打开的新翻译将是一个单独的视图模型 (viewModelB)。添加翻译名称和值后,对话框将异步发布,然后更新原始视图模型 (viewModelA) AvailableTranslations 可观察数组。

您能提供此功能的示例吗?

I am using knockout js on a view to display a list of fields (ie, first name, last name, etc). The fields are listed inside a knockout template using the an observable array. The template contains the following fields: name (input), translation (select), and an add/remove function. (See below)

var viewModel = {
    Fields: ko.observableArray([new Field(2, "First Name", 1), new Field(3, "Last Name", 2)]),
    AvailableTranslations: ko.observableArray([new Translation(1, "Prefixes"), new Translation(2, "Suffixes")]),
    remove: function(item) {
        ko.utils.arrayRemoveItem(this.Fields, item)
    },
    add: function() {
        this.Fields.push(new Field(0, "", ""));
    }
};

var Translation = function(id, name) {
    this.ID = id;
    this.Name = name;
};

var Field = function(id, name, translationID){
    this.ID = ko.observable(id);
    this.Name = ko.observable(name);
    this.TranslationID = ko.observable(translationID);
};

Next to the translation select list in the template, I would like an option to add a new translation that does not exist. When the button is clicked I want to open a jquery UI dialog box containing a partial view that utilizes knockout. The partial view will contain a translation name as well as an old value and a new value (both text fields). The old and new values are an observable array.

var viewModelPartialDialog = {
    TranslationName: ko.observable(),
    Values: ko.observableArray([]),
};

var Value = function(id, oldVal, newVal) {
    this.ID = id;
    this.OldVal = oldVal;
    this.NewVal = newVal;
};

When the user submits the partial view's form, I would like it to make a save call as well as update the AvailableTranslations observable array.

Can anyone help me out or point me in the right direction to accomplish this?


Thanks for the example. It is helpful but not exactly what I'm trying to do. In your example, I wasn't able to add an observableArray to the Product and then have a nested template inside the dialog's edit template.

Going back to my original example, I would like to add new fields in viewModelA, similar to how you have the product list. However, instead of edit the field in the dialog, I'd like to open a dialog to add a new Translation. The new translation that opens in the dialog would be a separate view model (viewModelB). Once the translation name and values are added, the dialog would post asynchronously and then update original view model's (viewModelA) AvailableTranslations observable array.

Can you provide an example of this functionality?

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

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

发布评论

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

评论(1

等风来 2024-12-12 03:07:19

这是一个可能与您正在执行的操作类似的示例:http://jsfiddle.net/rniemeyer/WpnTU/

页面加载时设置对话框,但不打开它。然后,有一个自定义绑定处理程序,每当填充“selectedItem”可观察对象(可能是现有项目或新项目)时就会调用该处理程序。

简单的自定义绑定处理程序如下所示:

//custom binding handler that opens the jQuery dialog, if the selectedProduct is populated
ko.bindingHandlers.openDialog = {
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        if (value) {
            $(element).dialog("open");
        }
    }
}

Here is a sample that might be similar to what you are doing: http://jsfiddle.net/rniemeyer/WpnTU/

It sets up the dialog when the page loads, but doesn't open it. Then, there is a custom binding handler that will get called whenever a "selectedItem" observable is populated (which could be with an existing item or a new item).

The simple custom binding handler looks like:

//custom binding handler that opens the jQuery dialog, if the selectedProduct is populated
ko.bindingHandlers.openDialog = {
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        if (value) {
            $(element).dialog("open");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文