jquery ui-(滑块)自定义事件绑定不起作用
我在弄清楚为什么我的自定义事件无法触发并正常工作时遇到了一些问题...我的自定义事件应该附加到每一列,如何让滑块仅绑定到一列(下面的 cols 对象) ?使用我当前的代码,如果我单击任何列,所有列都会调整大小...
我正在尝试使用在小 div 内打开的滑块动态调整表格列的大小,如果单击表格,则会加载我的小对话框,然后获取所有当前列宽度并添加事件处理程序我的代码是:
//param: data = current table
function DisplayColumnData(data) {
var cols = $(data).find("col");
//attach slider to each col
$(cols).each(function(i) {
var that = this;
var coldata = $("<div class='ui-col-data' />");
var colvalue = $("<span class='ui-col-value' />");
colvalue.text($(that).attr("width"));
coldata.text(" >col" + i + ": ").append(colvalue);
$(".ui-resizer-cols").append(coldata);
//handle column resizing, bind each col to the slider...
$(coldata).click(function() {
$(".ui-col-data").unbind("resizehandler");
//remove all other binded events then trigger the new event only...
$(coldata).trigger("resizehandler", that);
});
});
}
$(".ui-col-data").live("resizehandler", function(event, data) {
var $parent = $(this).closest(".ui-resizer-cols");
//remove classes from all other cols so only the clicked col is resized
$parent.find(".ui-col-data").removeClass("ui-col-border");
$parent.find(".ui-col-value").removeClass("ui-resizer-val");
$(this).addClass("ui-col-border"); //now the active col
var valfield = $(this).find("span.ui-col-value");
valfield.addClass("ui-resizer-val");
//update whatever...
$("#ui-resizer").slider('value', valfield.text() );
$("#ui-resizer").bind("slide", function(e, ui) {
valfield.text(ui.value);
$(data).attr("width", ui.value); //update col
});
});
TABLE:
<table>
<colgroup>
<col width="25%">
<col width="25%">
<col width="25%">
<col width="25%">
</colgroup>
<tbody>
<tr>...</tr>// 4 td's here as normal
<tr>...</tr>
</tbody>
</table>
Slider:
<div class="ui-resizer-content">
<div id="ui-resizer" class="ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all">
<div class="ui-slider-range ui-slider-range-max ui-widget-header" style="width: 100%;"></div><a href="#" class="ui-slider-handle ui-state-default ui-corner-all" style="left: 0%;"></a></div>
<div class="ui-resizer-cols"></div>
<p> <a href="#" id="lnkResizer" style="float: right;">close</a>
</p>
</div>
I have a bit of a problem working out why my custom event does not trigger and work correctly... My custom event should be attached to each column, how can I get the slider to be bound to only one column (cols object below)? With my current code if I click on any col all cols are resized...
I am trying to dynamically resize a table column using a slider that opens inside a little div, if a table is clicked my little dialog is loaded up then to get all current column width and add an event handler my code is:
//param: data = current table
function DisplayColumnData(data) {
var cols = $(data).find("col");
//attach slider to each col
$(cols).each(function(i) {
var that = this;
var coldata = $("<div class='ui-col-data' />");
var colvalue = $("<span class='ui-col-value' />");
colvalue.text($(that).attr("width"));
coldata.text(" >col" + i + ": ").append(colvalue);
$(".ui-resizer-cols").append(coldata);
//handle column resizing, bind each col to the slider...
$(coldata).click(function() {
$(".ui-col-data").unbind("resizehandler");
//remove all other binded events then trigger the new event only...
$(coldata).trigger("resizehandler", that);
});
});
}
$(".ui-col-data").live("resizehandler", function(event, data) {
var $parent = $(this).closest(".ui-resizer-cols");
//remove classes from all other cols so only the clicked col is resized
$parent.find(".ui-col-data").removeClass("ui-col-border");
$parent.find(".ui-col-value").removeClass("ui-resizer-val");
$(this).addClass("ui-col-border"); //now the active col
var valfield = $(this).find("span.ui-col-value");
valfield.addClass("ui-resizer-val");
//update whatever...
$("#ui-resizer").slider('value', valfield.text() );
$("#ui-resizer").bind("slide", function(e, ui) {
valfield.text(ui.value);
$(data).attr("width", ui.value); //update col
});
});
TABLE:
<table>
<colgroup>
<col width="25%">
<col width="25%">
<col width="25%">
<col width="25%">
</colgroup>
<tbody>
<tr>...</tr>// 4 td's here as normal
<tr>...</tr>
</tbody>
</table>
Slider:
<div class="ui-resizer-content">
<div id="ui-resizer" class="ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all">
<div class="ui-slider-range ui-slider-range-max ui-widget-header" style="width: 100%;"></div><a href="#" class="ui-slider-handle ui-state-default ui-corner-all" style="left: 0%;"></a></div>
<div class="ui-resizer-cols"></div>
<p> <a href="#" id="lnkResizer" style="float: right;">close</a>
</p>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档中这一点不太清楚(至少对我来说),但修复是:
显然我使用的代码(下面)对于使用单个处理程序的所有滑块来说是全局的,而上面的修复是特定于滑块的实例的? (如果我理解正确的话)
This was not so clear (at least to me) from the documentation but the fix is:
Apparantly the code I used (below) is global for all sliders using a single handler, and the fix above is specific to an instance of a slider? (if I have understood correctly)