Skip to content

Instantly share code, notes, and snippets.

@yanhua365
Created January 16, 2014 09:23
Show Gist options
  • Save yanhua365/8452066 to your computer and use it in GitHub Desktop.
Save yanhua365/8452066 to your computer and use it in GitHub Desktop.
KnockoutJS 交换数组里元素的数据(实现上移,下移功能)
function FilesViewModel() {
var self = this;
self.fileServers =ko.observableArray([{"url":"http://192.168.109.130/4337/"},{"url":"http://192.168.109.122/3366/"}});
self.upFileServer = function() {
var index = self.fileServers.indexOf(this);
if(index > 0){
var temp = self.fileServers()[index-1];
self.fileServers.splice(index-1, 1);
self.fileServers.splice(index, 0, temp);
}
};
self.downFileServer = function() {
var index = self.fileServers.indexOf(this);
if(index < self.fileServers().length-1){
var temp = self.fileServers()[index];
self.fileServers.splice(index, 1);
self.fileServers.splice(index+1, 0, temp);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment