【问题标题】:knockout js button click select all the checkbox淘汰js按钮单击选中所有复选框
【发布时间】:2016-10-27 11:55:08
【问题描述】:

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

<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)
            });
    }
})
});

【问题讨论】:

  • 您确定您的代码在上述问题之外还能正常工作吗?我试图创建一个小提琴并发现了很多问题
  • @yes 它与所有其他功能一起工作正常

标签: javascript jquery knockout.js knockout-2.0


【解决方案1】:

在你的函数范围开始之前你不能定义一个变量,你把 { 放在哪里。下面的减速是错误的

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();

【讨论】:

  • @感谢您的宝贵时间,这真的很有帮助,但我无法选择个人复选框“
    ,当我单击复选框时,我得到了单独的值,现在我想调用新函数,该函数应该获取单独的复选框数据
  • 我已将点击事件添加到复选框,以获取与该复选框相关的单个值,当我单击复选框时,我会获得单个值,但未选中复选框需要帮助
  • 感谢马特您的时间,但我根据我的要求以不同的方式解决了
【解决方案2】:

您需要向您的userViewModel 添加一个新方法:

self.SelectAll = function() {
    self.items().forEach(function(item) {
        item.Selected(true);
    });
};

【讨论】:

  • @thanks 您的时间,它不工作,它没有达到功能,当我点击全选按钮时,因为它在 foreach 项目之外
  • @Nikil,由于“它在 foreach 项目之外”,因此您的范围已经是 $root,如果您确实在 self 上声明了 SelectAll,那么您应该没问题。否则提供更多代码。
  • @Nikil,您是否缺少self 的声明(类似于var self = this)?首先检查您的控制台是否有错误。
  • ,不,我已经声明了,我在发布这个问题时错过了声明部分
【解决方案3】:

@haim770 是正确的,您必须遍历元素并设置item.Selected(true)。现在的问题是:

<input  type="checkbox" data-bind="value:userId(),click:$root.toggle" />
  • 首先,复选框映射到userId,而不是Selected
  • 其次,复选框没有值绑定。它具有checked 绑定。所以应该是:

&lt;input type="checkbox" data-bind="checked:$data.Selected,click:$root.toggle" /&gt;

另外你应该避免使用$root。您可以将其替换为$parent.toggle

Working Fiddle

【讨论】:

  • @thanks,但我不知道为什么选择按钮无法点击功能 self.SelectAll
  • 我试过了,但是当我在 foreach "foreach:items" 循环中添加 selectAll 按钮时,它的工作,如果我在 foreach 循环之前添加它,它不起作用
  • @Nikil 范围肯定有问题。尝试在跨度中记录一个临时变量。
猜你喜欢
相关资源
最近更新 更多
热门标签