绑定到类外部按钮的公共属性与实例化对象时内部绑定的按钮的值不同
Public property bound to button outside of class doesn't have the same value as button bound internally when object is instantiated
总的来说,我有一个跟踪所选复选框ID的对象。它这样做的方式是将 ID 推入/切入/切出数组,$Grid.selectedRows
.此对象还将"刷新"方法绑定到刷新按钮。我有一个类,我从中创建该对象,KendoGridSelection
.
我的问题是类内部绑定的按钮显示正确的数组值,而类外部绑定的具有 public selectedRows
属性的按钮在单击refresh
按钮后不再更新。
出于测试目的,我有两个seeSelectedRowsArray
按钮:
-
seeSelectedRowsArray
按钮(内部绑定( -
seeSelectedRowsArray2
按钮(在类外绑定(
我正在Chrome版本28.0.1500.95 m中进行测试
这是我的代码:
.JS
var KendoGridSelection = function (gridID, pagerSelector) {
var $Grid = this;
$Grid.selectedRows = [];
$Grid.gridID = gridID;
$Grid.pagerSelector = pagerSelector;
$Grid.grid = $($Grid.gridID).data('kendoGrid');
$Grid.pager = $($Grid.pagerSelector).data('kendoPager');
$Grid.gridCheckboxes = $('input[type=checkbox]', $Grid.gridID);
$Grid.gridRows = $('table tbody tr', $Grid.gridID);
$Grid.refreshButton = $('.refreshButton', $Grid.gridID);
$Grid.bindUIEvents = function () {
$Grid.gridCheckboxes = $('input[type=checkbox]', $Grid.gridID);
$Grid.gridRows = $('.row', $Grid.gridID);
// Row click event
/*$($Grid.gridRows).click(function (e) {
if (!$(e.target).parent().hasClass('k-hierarchy-cell')) $(this).find('input[type=checkbox]').click();
});*/
// Checkbock click event
$($Grid.gridCheckboxes).click(function (e) {
console.log('checkbox clicked!');
e.stopPropagation();
var $t = $(this);
var checkboxID = $t.attr('id');
var thisRow = $t.closest('tr');
if ($t.is(':checked')) {
thisRow.addClass('k-state-selected');
// add to selected[]
if ($.inArray(checkboxID, $Grid.selectedRows) === -1) $Grid.selectedRows.push(checkboxID);
} else {
thisRow.removeClass('k-state-selected');
// remove from selected[]
$Grid.selectedRows.splice($.inArray(checkboxID, $Grid.selectedRows), 1);
}
});
}
$Grid.gridPersistSelected = function () {
$.each($Grid.selectedRows, function () {
var $t = $('#' + this);
if ($t) $t.click();
});
}
$Grid.pagerChange = function () {
$Grid.bindUIEvents();
$Grid.gridPersistSelected();
}
$Grid.refresh = function () {
$Grid.selectedRows = [];
$Grid.gridCheckboxes.attr('checked', false);
console.log('Refresh clicked.');
console.log('$Grid.selectedRows: '+$Grid.selectedRows);
}
// Init
$Grid.pagerChange();
// $Grid.pager.bind("change", $Grid.pagerChange);
// Unbind refresh button, then rebind
// Refresh button
$Grid.refreshButton.click(function(){
console.log('reset!');
$Grid.refresh();
});
$('.seeSelectedRowsArray').click(function(){
console.log($Grid.selectedRows);
});
return {
selectedRows: $Grid.selectedRows,
refresh: $Grid.refresh,
}
}
$(function(){
window.activeThreatsGrid = new KendoGridSelection('.grid', '.pager');
$('.seeSelectedRowsArray2').click(function(){
console.log(activeThreatsGrid.selectedRows);
});
});
.HTML
<div class='grid'>
<div class='row'>
<label><input type="checkbox" id="item1"> </label>
</div>
<div class='row'>
<label><input type="checkbox" id="item2"> </label>
</div>
<div class='row'>
<label><input type="checkbox" id="item3"> </label>
</div>
<div class='row'>
<label><input type="checkbox" id="item4"> </label>
</div>
<div class='row'>
<label><input type="checkbox" id="item5"> </label>
</div>
<div class='pager'>
<input type="button" value="refresh" class="refreshButton">
</div>
<div><input type="button" value="seeSelectedRowsArray" class="seeSelectedRowsArray"></div>
<div><input type="button" value="seeSelectedRowsArray2" class="seeSelectedRowsArray2"></div>
</div>
.CSS
.row{background:blue; height:20px; width:100px; margin-bottom:5px;}
JSFiddle
演示
发生了什么事情:
- 当我单击多个复选框,然后单击
seeSelectedRowsArray
时,我在数组中得到正确的值。但是我可以多次执行此操作,并且仍然可以获得正确的值。 - 当我点击
refresh
按钮时,我的console.log
告诉我我的selectedRows
数组是空的。然后当我单击seeSelectedRowsArray
时,数组为空(预期(。当我单击seeSelectedRowsArray2
时,数组中仍然有值。
更新 1
我发现,如果我$Grid.selectedRows
绑定到类中的按钮单击,它总是获得最新的值,即使在刷新后也是如此。如果我将公共selectedRows
绑定到类外部的按钮单击,则在单击refresh
按钮后,selectedRows
不再更新并卡在刷新前的值。
为什么内部绑定的按钮显示正确的数组值,而使用 public selectedRows
属性绑定在类外部的按钮在单击refresh
按钮后不再更新?
任何帮助将不胜感激!
您的问题在于如何返回/跟踪对对象的引用。
在构造函数中,设置$Grid = this
但是,作为函数的结果,您将返回一个新对象:
return {
selectedRows: $Grid.selectedRows,
refresh: $Grid.refresh,
}
该返回的对象现在仅包含对当前值 $Grid.selectedRows
的引用
当您的refresh
方法将$Grid.selectedRows
设置为新数组时,它会断开返回对象的关联值,该对象仍设置为原始数组。
将刷新从以下位置更改为:
$Grid.selectedRows = []
自:
$Grid.selectedRows.length = 0;
演示
- 将值动态绑定到jquery中的切换按钮
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- Aurelia绑定-点击按钮返回查看模型
- 如何防止 jQuery-UI 按钮集破坏 AngularJS 绑定
- 将 checkedValue 绑定与单选按钮一起使用
- 对触发其onclick功能的按钮进行键绑定
- 将按钮绑定到几个基于文本框中值的大小而激活的文本框
- 在Angular中绑定剑道网格按钮's ng click
- 使用XML将头文本、段落文本和按钮和每个图像绑定
- 将单选按钮值绑定到控制器(使用angularjs 1.5)
- 将事件绑定到Bootstrap 3(button.js)按钮无线电时出现问题
- 将javascript绑定到提交按钮时出现问题
- 如何在选择的按钮上动态/不断地绑定jQuery事件处理程序
- 动态绑定onclick事件为for循环中的所有按钮提供相同的值
- 选择绑定到同一范围的按钮,单击时会相互触发.如何从angular.js中的两个选择列表按钮解除范围绑定
- 数据绑定禁用属性在 foreach 内的按钮中不起作用
- 创建没有业务逻辑的干净挖空绑定(弹出框 - 单选按钮)
- 无法使用 jquery 绑定按钮上的单击事件
- 如何重新绑定按钮's使用.on/off单击事件
- 如何再次绑定按钮的click事件