javascript - knockout js按钮点击全选复选框

标签 javascript jquery knockout.js knockout-2.0

我正在使用 Knockout js 进行绑定(bind),我正在使用复选框绑定(bind)数据列表,我正在获取并绑定(bind)到列表项的数据,包括所有项目的复选框,现在我需要创建新的按钮名称“全选”和当我单击该按钮时,我需要选中所有复选框,并且我需要从选中的复选框中获取所有数据如何实现此需要帮助..

<div>
<input type="button" value="Select-All" data-bind="click:SelectAll" />
</div>
<div  id="List" data-bind="foreach:items">
<ul>
<li>
<span data-bind="text:userId" style="display:none"></span>
<span style="margin-top: 10px;font-size: 22px;font-weight: bold;padding-left:0px;" data-bind="text:username"></span>
<input  type="checkbox" data-bind="value:userId(),click:$root.toggle" />                   
</li>
</ul>
</div>

 //ViewModel
function userViewModel()
 var self=this;
     {
    function userList(userId, userName)
     {
      var self=this;
     self.userID=ko.observable(userId);
     self.userName=ko.observable(userName);
     self.Selected = ko.observable(false);
     }
   self.toggleAssociation = function (item) {
     //here i am getting the individual selected checkbox Item 
   }

 //This part is not working 
  self.SelectAll = function() {
  console.log("in")
 self.items().forEach(function(item) {
    item.Selected(true);
});
  };

   self.items = ko.observableArray();
     self.add = function (userId, userName,){
     self.items.push(new FriendsContact(userId, userName)
   }
};

 //Page Load  
 $(function () {
var viewModel = new userViewModel();
ko.applyBindings(viewModel);

 //Get the data from database//
 $.ajax({
    type: "GET",
    dataType: "json",
    contentType: 'application/json; charset=utf-8',      
    url: baseUrl + 'xxx/xxx/xxxxx',
    success: function (data) { 
            $.each(data, function (key, value) {
                viewModel.add(value.userId, value.userName)
            });
    }
})
});

最佳答案

您不能在函数作用域开始于放置 { 的位置之前定义变量。低于减速是错误的

function userViewModel()
  var self=this;
     {

根据我从您的代码中看到的内容,我将向您展示如何处理此类问题。

示例:https://jsfiddle.net/kyr6w2x3/103/

HTML:

 <div>
      <input type="button" value="Select-All" data-bind="click:SelectAll,visible:ShowSelectAllBtn" />
      <input type="button" value="UnSelect-All" data-bind="click:UnSelectAll,visible:ShowSelectAllBtn() == 0" />
    </div>
    <div>
      <input type="button" value="Add" data-bind="click:add" />
    </div>
    <div  id="List" data-bind="foreach:items">
      <ul>
        <li>
          <span data-bind="text:userId"></span>
          <span style="margin-top: 10px;font-size: 22px;font-weight: bold;padding-left:0px;" data-bind="text:userName"></span>
          <input  type="checkbox" data-bind="checked:Selected" />                   
        </li>
      </ul>
    </div>

JS:

var data = [{userId:1 , userName : "UserName1"},{userId:2 , userName : "UserName2"}];

function userViewModel(){
   var self = this;
   self.items = ko.observableArray();
   self.ShowSelectAllBtn = ko.observable(true);
   self.loadData = function (){
     // here you have your ajax 
     //maping fake data assuming it is inside the ajax's success
     self.items($.map(data, function (item) {
       return new userItemViewModel(item);
     }));
   }
  self.SelectAll = function() {
    self.ShowSelectAllBtn(false);
    ko.utils.arrayForEach(self.items(), function (item) {
      item.Selected(true);
    });
  };
  self.UnSelectAll = function() {
    self.ShowSelectAllBtn(true);
    ko.utils.arrayForEach(self.items(), function (item) {
      item.Selected(false);
    });
  };
  var i = 2;
  self.add = function (userId, userName){
    // creating a fake json object with a userId and a userName
    userId = i++;
    userName = "userName" + i;
    obj = {userId:userId , userName : userName} ;
    self.items.push(new userItemViewModel(obj) );
  }
};

var userItemViewModel = function (data){
    var self = this;
    self.userId = ko.observable(data.userId);
    self.userName = ko.observable(data.userName);
    self.Selected = ko.observable(false);
    self.Selected.subscribe(function(newValue){
     //individual selected checkbox 
       console.log(newValue);
       console.log(self.userId());
       console.log(self.userName());
    }, self);
}

var viewModel = new userViewModel();
ko.applyBindings(viewModel);
// Load data
viewModel.loadData();

关于javascript - knockout js按钮点击全选复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40283972/

相关文章:

javascript - 如何使用 jQuery 同时制作 2 个动画

javascript - 我可以动态更新 Google 表单列表框的内容吗

javascript - 如何使用 JSDoc 注释 Express 中间件?

javascript - 过滤网格 : getElementsByTagName is not a function

javascript - 仅在移动设备中添加事件类(class)

javascript - 无法在 View 中的 foreach 内设置 knockout 绑定(bind)

javascript - 我可以在 knockout.js 中创建使用其他绑定(bind)的自定义绑定(bind)吗

javascript - 如何在 Knockout JavaScript 表格中创建文本链接?

javascript - 第一次后切换绝对 DIV 不起作用

ajax - 没有数据参数的 jQuery PUT/POST ajax CORS 请求