简体   繁体   English

使用关联数组中的其他项计算值

[英]Compute value using other items in associative array

I have been struggling these past few days trying to solve this problem and I'm hoping you all can assist. 在过去的几天里,我一直在努力解决该问题,希望大家能提供帮助。

I am trying to store a dynamic value in an associative array that is computed using 2 other values from the same array. 我正在尝试在关联数组中存储动态值,该关联数组是使用同一数组中的其他2个值计算的。

Here is the code: 这是代码:

model.TimeLog = {
 StartTime: "2015-01-01T00:00:00",
 EndTime: "2015-01-01T00:00:00",
 TimeDifference: ko.pureComputed(function() {
  var from = StartTime() || "2015-01-01T00:00:00";
  from = new Date(from);
  var to = EndTime() || "2015-01-01T00:00:00";
  to = new Date(to);
  var difference;
  if ((to - from) >= 0)
    difference = to - from;
  else
    difference = 0;
  var timespan = +(difference / 1000 / 60 / 60).toFixed(2);
  return timespan;
 })
}

--------- Section of the HTML page referenced later -------------

<td><input type="text" data-bind="event {onchange: $root.calculateTimeSpan(this,EndTime)}"></input></td>
<td><input type="text" data-bind="event {onchange: $root.calculateTimeSpan(StartTime,this)}"></input></td>

The goal is to have the user pick a start time and end time (using a date/calendar picker) then display the time difference, in hours, to a read only field when either date field is edited. 目的是让用户选择开始时间和结束时间(使用日期/日历选择器),然后在编辑任一日期字段时将时差(以小时为单位)显示到只读字段。

As to why I am trying to complete the function in this model view as opposed to directly on the page or in a separate script file is because on the page there can be multiple instances of this model and I need the values computed on each instance. 至于为什么要在此模型视图中完成功能而不是直接在页面上或在单独的脚本文件中完成功能的原因,是因为在页面上可以存在该模型的多个实例,因此我需要在每个实例上计算值。

Some other pieces of information that may help: 其他一些信息可能会有所帮助:

  • Webpage runs on HTML5, nothing older 网页在HTML5上运行,没有较旧的版本
  • Knockout JS v3.3.0 淘汰赛JS v3.3.0
  • Designed for latest versions of Chrome and Firefox; 专为最新版本的Chrome和Firefox设计; no IE 没有IE

I hope this is enough information for you all. 我希望这对你们所有人来说都是足够的信息。 If not, let me know and I will try my best to provide it. 如果没有,请告诉我,我会尽力提供。

Update 更新资料

  • Issue is no values are displayed in the read only field. 问题是只读字段中未显示任何值。 No errors on the back-end and only error in the browser is "ReferenceError: StartTime is not defined" 后端无错误,浏览器中只有错误“ ReferenceError:未定义StartTime”
  • I believe StartTime and EndTime are global variables. 我相信StartTime和EndTime是全局变量。 I can access them in other files after accessing the model, ie model.StartTime 访问模型后,我可以在其他文件中访问它们,即model.StartTime

Things I have tried 我尝试过的事情

  • Instead of running the function on a model, I tried data-binding a function on the html page (using a separate javascript file) (See section above) 我没有在模型上运行该函数,而是尝试在html页上使用一个单独的javascript文件对一个函数进行数据绑定(请参见上文)

NOTE: The html page uses some custom built tags which I cannot post the code of. 注意:html页面使用一些自定义构建的标记,这些标记无法发布。 To access functions in my javascript file I need to use '$root.[function]'. 要访问我的javascript文件中的函数,我需要使用'$ root。[function]'。 Any values that are within the 'model' are accessible without the use of 'model.[key]'. 无需使用“ model。[key]”就可以访问“ model”中的任何值。

Since I am unable to post code samples or screenshots of how the html page renders I'll describe the functionality it has that I am working with: 由于我无法发布有关html页面呈现方式的代码示例或屏幕截图,因此我将描述其正在使用的功能:

In the view that I am working on, there is a collection/list of instances of my model. 在我看来,这里有我的模型实例的集合/列表。 By default no input fields are displayed and there is always a button fixed to the bottom of the page allowing the user to generate a new instance of the model. 默认情况下,不显示任何输入字段,并且页面底部始终固定有一个按钮,允许用户生成模型的新实例。 As the user creates a new instance, that instance of the model is stored in an array, in my case it is called TimeLogGroup . 当用户创建一个新实例时,该模型实例存储在一个数组中(在我的情况下称为TimeLogGroup) When the user goes to save the page this group, TimeLogGroup , is passed to the back-end to be stored in the database. 当用户保存页面时,该组TimeLogGroup被传递到后端以存储在数据库中。

So to reiterate the issue, referencing the paragraph above, on each instance of the model there is a start and end time and I need to calculate and display the time-span in a non-editable/read-only field. 因此,要重申此问题,请参考上面的段落,在模型的每个实例上都有一个开始和结束时间,我需要在不可编辑/只读字段中计算并显示时间跨度。 I have the code to calculate, tested and tried, but am unable to have the value display in the required field. 我有要计算,测试和尝试的代码,但是无法在必填字段中显示值。

I hope this further helps you guys. 希望这对您有所帮助。

Your computed will only be updated when observables that it accesses are updated. 仅当您访问的观察值更新时,您的计算结果才会更新。 You need StartTime and EndTime to be observables. 您需要StartTime和EndTime是可观察的。 You also need to reference them using their qualified names. 您还需要使用它们的限定名称来引用它们。

Update: oh, and I forgot that the computed will have to be defined outside the object, because it will be evaluated upon creation, and the object members won't exist. 更新:哦,我忘记了计算值必须在对象外部定义,因为它将在创建时进行评估,并且对象成员将不存在。

model.TimeLog = {
 StartTime: ko.observable("2015-01-01T00:00:00"),
 EndTime: ko.observable("2015-01-01T00:00:00")
};
model.TimeLog.TimeDifference = ko.pureComputed(function() {
  var from = new Date(model.TimeLog.StartTime());
  var to = new Date(model.TimeLog.EndTime());
  var difference;
  if ((to - from) >= 0)
    difference = to - from;
  else
    difference = 0;
  var timespan = +(difference / 1000 / 60 / 60).toFixed(2);
  return timespan;
 });

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

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