简体   繁体   English

按文本值剔除选定的选项

[英]Knockout Selected Option By Text Value

I have an application that allows users to make Courses with multiple course areas. 我有一个允许用户制作具有多个课程区域的课程的应用程序。 A course area is selected from a select drop down list. 从选择下拉列表中选择一个课程区域。 The list is data-binded using knockout. 该列表使用敲除进行了数据绑定。

HTML 的HTML

<select id="courseAreaType" name="selectCourseArea" class="form-control" data-bind="options:$parent.courseAreaTypes, optionsText: 'name', optionsValue:'id', optionsAfterRender:$parent.setIconPath, value:typeid"></select>

What I need to do is when the course is loaded, load the different course areas, which I have done. 我需要做的是在加载课程时,加载我已经完成的不同课程区域。 For each course area it needs to load its values into its various controls. 对于每个课程区域,需要将其值加载到其各个控件中。

I have been able to load the select drop down with the list of values from the DB, and allow when a user selects an option and saves the course area, it successfully saves the courseArea name. 我已经能够从数据库中加载带有值列表的select下拉列表,并允许当用户选择一个选项并保存课程区域时,它成功保存了courseArea名称。

Now what I need to do is when each course area is loaded again, it selects the option from the dropdown, based on courseArea.Name 现在,我需要做的是再次加载每个课程区域时,它基于courseArea.Name从下拉菜单中选择选项。

Knockout 昏死

var CourseViewModel = function (courseIn) {
    var self = this;
    if (courseIn === undefined) {
        courseIn = {};
    }

    self.id = courseIn.Id;
    self.name = ko.observable(courseIn.Name);
    self.postalCode = ko.observable(courseIn.PostalCode);
    self.city = ko.observable(courseIn.City);
    self.province = ko.observable(courseIn.Province);
    self.courseId = ko.observable(courseIn.CourseId);
    self.courseAreas = ko.observableArray();
    self.courseAreaTypes = ko.observableArray();

    $.each(courseIn.CourseAreas, function(index, courseArea) {
        self.courseAreas.push(new CourseAreaViewModel(courseArea));
    });

    $.getJSON("/Course/GetCourseAreaTypes", function (types) {
        $.each(types, function (index, type) {
            self.courseAreaTypes.push(new CourseAreaTypeViewModel(type));
        });
    });

    $.each(courseIn.CourseAreas, function (index, courseArea) {
        console.log(courseArea.Name);
        var list = $('#courseAreaType');
        var options = $('option', list); // This is where my problem is
    });

    self.setIconPath = function (option, courseAreaType) {
        $(option).data("icon",  courseAreaType.iconPath)
        //console.log($(option).data("icon"));
    }
}

var CourseAreaTypeViewModel = function (courseAreaTypeIn) {
    var self = this;

    if (courseAreaTypeIn === undefined) {
        courseAreaTypeIn = {};
    }

    self.id = courseAreaTypeIn.ID;
    self.name = courseAreaTypeIn.Name;
    self.iconPath = courseAreaTypeIn.iconPath;
}

var CourseAreaViewModel = function(courseAreaIn) {
    var self = this;
    if (courseAreaIn === undefined) {
        courseAreaIn = {};
    }
    self.id = courseAreaIn.Id;
    self.name = ko.observable(courseAreaIn.Name);
    self.acreage = ko.observable(courseAreaIn.Acreage);
    self.goals = ko.observable(courseAreaIn.Goals);
    self.typeid = ko.observable(courseAreaIn.TypeID);
    self.selectedOption = ko.observable();
}

I've tried changing it based on getting the element id, I've tried .val, .prop, .attr without any success. 我尝试根据获取元素ID对其进行更改,但尝试了.val,.prop,.attr却没有成功。 When I log the data to the console of the element, it's not showing the values in the dropdown, so I have a feeling knockout is adding the values on a script level, and jquery is unable to see those values. 当我将数据记录到元素的控制台时,它没有在下拉列表中显示值,因此我感到淘汰赛是在脚本级别添加值,而jquery无法看到这些值。

The basic approach to this is that you need to bind the value of the select element to an observable that holds the ID of the course type that should be selected. 实现此目的的基本方法是,您需要将select元素的值绑定到一个可观察的对象,该对象包含应选择的课程类型的ID。

I've doe a simple fiddle that shows this in action: 我做了一个简单的小提琴,演示了这一点:

http://jsfiddle.net/9a1njj1g/2/ http://jsfiddle.net/9a1njj1g/2/

The JS is: JS是:

function CourseArea(types) {
   this.types = ko.observableArray(types);
   this.typeid = ko.observable(2); 
};


var types = [
    { id: 1, name: "one" },
    { id: 2, name: "two" },
    { id: 3, name: "three" }
];

ko.applyBindings(new CourseArea(types));

The HTML is: HTML是:

<select data-bind="options: types, optionsText: 'name', optionsValue: 'id', value: typeid"></select>

To apply this to your code, I don't think you have much to do. 将此应用到您的代码中,我认为您没有太多工作要做。 Maybe refactor your JS slightly, so you only process your CourseAreas once you've loaded all your CourseAreaTypes: 也许稍微重构一下JS,所以只有在加载了所有CourseAreaTypes之后,您才处理CourseAreas:

$.getJSON("/Course/GetCourseAreaTypes", function (types) {
    // Load all the types first.
    $.each(types, function (index, type) {
        self.courseAreaTypes.push(new CourseAreaTypeViewModel(type));
    });

    // Continue here, to be sure you have a complete list of types to work with.
   $.each(courseIn.CourseAreas, function (index, courseArea) {
        self.courseAreas.push(new CourseAreaViewModel(courseArea));
    });
});

Also ensure the ID you're selecting in CourseAreaViewModel.typeid has an equivalent exact value in a CourseAreaTypeViewModel.id. 还要确保您在CourseAreaViewModel.typeid中选择的ID在CourseAreaTypeViewModel.id中具有等效的精确值。

And ensure your data matches up to the code OK... eg the types data must supply its ID in capitals for self.id = courseAreaTypeIn.ID to be useful. 并确保您的数据符合代码OK ...例如,类型数据必须以大写形式提供其ID,以使self.id = courseAreaTypeIn.ID有用。 If not then you won;t get an error, because self.id will be quietly set to undefined . 否则,您将不会出错,因为self.id将被悄悄地设置为undefined

Here's a fiddle showing all this working with Courses, Areas and Types: 这是一个小提琴,显示了与课程,区域和类型一起使用的所有内容:

http://jsfiddle.net/vpe4ut3m/1/ http://jsfiddle.net/vpe4ut3m/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM