有没有办法遍历选择列表中的项目并计算一个值。例如我有以下数据
“因素”:[20、40、60、80、100]
它目前在多选列表框中显示如下,我在 HTML 语句中计算级别 Level 0: 20 Level 1: 40 Level 2: 60 Level 3: 80 Level 4: 100
我想计算并显示一个起始值,如下所示;0 级:0-20 1 级:21-40 2 级:41-60 3 级:61-80 4 级:81-100
当我显示当前级别时,如何在淘汰赛中恢复到以前的级别。例如在选择列表框中,我显示的是 100,上一个级别是 80,但是当我显示 100 时,我想显示上一个级别 + 1 和当前级别 100,级别 4:81-100
用户可以更改这些值,因此他们可以输入 15、45、70、80、100 的因子。然后我想根据这些数字显示一个范围值,例如。0-15、16-45 等
在淘汰赛/javascript/html 中执行此操作的任何方法
HTML:
<select id="factorsSelect" multiple="multiple" size="10" data-bind="options:factors, selectedOptions:selectedFactors,
optionsText: function(factor) {return 'Level ' + factors().indexOf(factor) + ': ' + factor}"></select>
JavaScript:
function FactorInfo(projectid, adjustmentfactor, listpoints) {
this.projectid = ko.observable(projectid);
this.adjustmentfactor = ko.observable(adjustmentfactor);
this.listpoints = ko.observableArray(listpoints);
this.displayFactorName = ko.dependentObservable(function () {
return this.projectid() + " -- " + this.adjustmentfactor();
}, this);
}
// Factor construction
var Factor = function () {
this.factor = ko.observable();
};
// get project trusted data from wcf service
$.getJSON("../../Home/GetProjectTrustSettings", function (jsonData) {
var mappedGlobal = $.map(jsonData, function (item) {
vm.adjustmentFactor(item.AdjustmentFactor);
vm.mySelectedproject(item.ProjectId);
vm.factors(item.ListPoints);
return new FactorInfo(item.AdjustmentFactor, item.ProjectId, item.ListPoints);
});
});
var vm = {
//app observables
levelIdSize: ko.observable(""),
myGlobal: ko.observableArray([]),
// percent tab observables
percents: ko.observableArray([]),
percentlevelids: ko.observableArray([]),
selectedPercents: ko.observableArray([]).trackDirtyFlag(),
percentItemToAdd: ko.observable(""),
globalPercent: ko.observable("").extend({ required: "Enter a Global Percent, between 1 and 100." }).trackDirtyFlag(),
levelSize: ko.observable(""),
// factor tab observables
factors: ko.observableArray([]),
factorlevelids: ko.observableArray([]),
selectedFactors: ko.observableArray([]).trackDirtyFlag(),
factorItemToAdd: ko.observable(""),
adjustmentFactor: ko.observable("").extend({ required: "Enter an Adjustment Factor, between 1 and 100." }).trackDirtyFlag(),
// project observable
myProject: ko.observableArray([]),
mySelectedproject: ko.observable("1"),
projectIdChange: ko.observable(false),
// check if something changes with project ID
setProjectDirtyFlag: function () {
this.projectIdChange(true);
//console.log(this.projectIdChange());
}
};
使用 MVC 格式: 模型:使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;
namespace AuditReview.Models
{
public class ProjectTrustSetting
{
public string ProjectId { get; set; }
public int AdjustmentFactor { get; set; }
public List<int> ListPoints { get; set; }
}
}
样本控制器数据:
public JsonResult GetProjectTrustSettings()
{
//create list
var trustSetting = new List<ProjectTrustSetting>
{
new ProjectTrustSetting
{
ProjectId = "30729",
AdjustmentFactor = 5,
ListPoints = new List<int>
{ 20, 40, 60, 80, 100}
}
};
return Json(trustSetting, JsonRequestBehavior.AllowGet);
}
寻找创建起始范围值的最佳方法,无论是在淘汰赛、HTML、JavaScript 中。我在想应该有一些方法可以在 HTML 中,在我已经拥有的功能中。
optionsText: function(factor) {return 'Level ' + factor().indexOf(factor) + ': ' + factor},或者在淘汰赛中作为计算出的 observable 或任何其他建议。
谢谢你的帮助