简体   繁体   English

如何在ObservableArray中用'-'替换'/'? 淘汰赛js

[英]How to replace ' / ' with '-' inside ObservableArray ? Knockout js

Well i am trying to pass an observable array via ajax call to my controller but i get every value there except date . 好吧,我试图通过ajax调用将一个可观察的数组传递给我的控制器,但是我得到除date之外的所有值。 i get something like '01-01-01' etc . 我得到类似'01 -01-01'之类的东西。

I found the issue but unable to fix that as i dont know how to replace / with - . 我发现了问题,但无法解决,因为我不知道如何用-替换/

My ObservableArray have around 10 list items each list item holds a many properties out of those startDate holds the date like ("23/10/2014") . 我的ObservableArray大约有10个列表项,每个列表项都具有许多属性,而startDate具有类似("23/10/2014")的日期。 i just need something like ("23-10-2014") . 我只需要类似("23-10-2014")东西。

Tought of posting my function's and more i hope thats not required in this case i believe . 我想发布我的函数的想法,我希望在这种情况下不需要这样做。

Let me explain with bit of code and sample data : 让我用一点代码和示例数据来解释一下:

function myarray()
{
var self=this;
self.startDate=ko.observable("");
self.name=ko.observable("");
self.place=ko.observable("");
}

MyObservableArray : MyObservableArray:

self.Main= ko.observableArray();

In between i do some stuff to load Data into self.Main and i am sending self.Main to controller having data like below : 在这两者之间,我做了一些事情将数据加载到self.Main并且我正在将self.Main发送到具有如下数据的控制器中:

self.Main[0] holds : 

startDate() -->gives you  "23/10/2014" //individual observables inside onservable array
name() --> "jhon"
place()--> "croatia"

Likely 可能

self.Main[9] holds :
startDate() --> "29/05/2012" 
    name() --> "pop"
    place()--> "usa"

I am trying like i want to alter the self.Main() and replace the startDate and use the same self.Main to send to my controller . 我正在尝试像我想要更改self.Main()并替换startDate并使用相同的self.Main发送到我的控制器。 Once after replacing in self.Main when i check date the / should be replaced with - . self.Main替换一次后,当我检查日期时, /应该替换为-

Possible solution : i can use a different observable array and push all the VM's of Main into it but i am trying to do on self.Main without using other . 可能的解决方案:我可以使用其他可观察的数组,并将Main所有VM推入其中,但是我尝试对self.Main进行操作,而不使用other。

If someone can show some light it is much appreciated . 如果有人可以显示一些光,那将是万分感谢。

What I got that you are facing problem in escaping / in replace. 我得到的是您在转义/替换中面临问题。

Try this 尝试这个

"(23/10/2014)".replace(/\//g,"-") //returns "(23-10-2014)"

I tried something for you using simple JS 我使用简单的JS为您尝试了一些东西

var arr = [{date:"(23/10/2014)"},{date:"(23/10/2014)"},{date:"(23/10/2014)"},{date:"(23/10/2014)"}];

arr.forEach(function(obj){obj.date = obj.date.replace(/\//g,"-")});

console.log(arr) //will print date field as "(23-10-2014)" for all objects.

One solution would be to add a computed value that returns the array with the right values. 一种解决方案是添加一个computed值,该值将返回具有正确值的数组。

self.Main = ko.observableArray([...values here...]);

self.MainComputed = ko.computed(function() { 
  var computedArray = [];

  self.Main().forEach(function(item) {
    var newItem = myarray(); //Create a new item.
    newItem.name(item.name());
    newItem.place(item.place());
    newItem.startDate(item.startDate().replace(/\//g,"-"));
    computedArray.push(newItem);
  });

  return computedArray;
});

Then use the computed value in the places where you need the values with - . 然后在需要使用-的地方使用计算值。


I can think of two other ways to solve your issue, when taken into account that you want to use self.Main : 考虑到您想使用self.Main ,我可以想到另外两种方法来解决您的问题:

  1. Replace the / with - before setting startDate on your item. 在项目上设置startDate之前,将/替换为-
  2. Change startDate to a computed value while storing the original value in another variable. 将原始值存储在另一个变量中时,将startDate更改为计算值。

The first solution should be pretty straight forward (provided that it is a valid solution). 第一个解决方案应该非常简单(前提是它是有效的解决方案)。

The second solution would look something like this: 第二种解决方案如下所示:

function myarray()
{
  var self=this;
  self.originalStartDate = ko.observable("");
  self.name = ko.observable("");
  self.place = ko.observable("");

  self.startDate = ko.computed(function() {
    if(self.originalStartDate()) {
      //We can only replace if the value is set.
      return self.originalStartDate().replace(/\//g,"-");
    }
    else {
      //If replace was not possible, we return the value as is.
      return self.originalStartDate();
    }
  });
}

Now when you set the values you do something like: 现在,当您设置值时,您将执行以下操作:

var item = myarray();
item.originalStartDate = "01/01/2014";

Then when you get the value of startDate you would get "01-01-2014" . 然后,当您获得startDate的值时,您将获得"01-01-2014"

I haven't used Knockout.js but you can do this with a Javascript replace: 我没有使用过Knockout.js,但是您可以使用Javascript替换来做到这一点:

var str = [your array value] ;
var res = str.replace("/", "-");

For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace 有关更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace

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

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