注意:我将此添加到在此链接中找到的 JSFIDDLE
我编写了一些基本的 jQuery 代码来操作 3 个表之一中的行,处理几个按钮单击和复选框。我现在感觉很像一个n00b。我正在看这个并意识到有很多东西可以变成更通用的函数,但我并不精通创建 JS 函数。欢迎任何指点。
由于我有类似功能的块,我更愿意讨论如何简化每种块类型。
- 按钮交换
- 选择/取消选择清单
- 移动检查清单
- 显示/隐藏面板
这是代码:
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#displayActivePanel').hide();
$('#displayInactivePanel').hide();
// Button event handlers to move items from/to differing tables...
$('#TableAdminPortfolio tbody .buttonSwap').live('click', function () {
var tr = $(this).closest('tr');
var stat = tr.children('.padLeft.status').text().trim();
var tableActiveProjects = $('#TableActiveProjects tbody');
var tableInactiveProjects = $('#TableInactiveProjects tbody');
tr.fadeOut(1000);
if (stat == 'Active') {
tableActiveProjects.prepend(tr);
} else {
tableInactiveProjects.prepend(tr);
}
tr.fadeIn(1000);
});
$('#TableActiveProjects tbody .buttonSwap').live('click', function () {
var tr = $(this).closest('tr');
var tableAdminPortfolio = $('#TableAdminPortfolio tbody');
tr.fadeOut(1000);
tableAdminPortfolio.prepend(tr);
tr.fadeIn(1000);
});
$('#TableInactiveProjects tbody .buttonSwap').live('click', function () {
var tr = $(this).closest('tr');
var tableAdminPortfolio = $('#TableAdminPortfolio tbody');
tr.fadeOut(1000);
tableAdminPortfolio.prepend(tr);
tr.fadeIn(1000);
});
$('#masterCheckBoxAdminPortfolio').live('change', function () {
var isChecked = $('#masterCheckBoxAdminPortfolio:checked').val() != undefined;
var list = $('#TableAdminPortfolio .CheckBoxProjects');
list.each(function () {
this.checked = isChecked;
});
});
$('#TableActiveProjectsCheckbox').live('change', function () {
var isChecked = $('#TableActiveProjectsCheckbox:checked').val() != undefined;
var list = $('#TableActiveProjects .CheckBoxProjects');
list.each(function () {
this.checked = isChecked;
});
});
$('#TableInactiveProjectsCheckbox').live('change', function () {
var isChecked = $('#TableInactiveProjectsCheckbox:checked').val() != undefined;
var list = $('#TableInactiveProjects .CheckBoxProjects');
list.each(function () {
this.checked = isChecked;
});
});
$('#TableAdminPortfolioRemoveAllChecked').live('click', function () {
var list = $('#TableAdminPortfolio .CheckBoxProjects');
list.each(function () {
var isChecked = $(this).closest('td').children('.CheckBoxProjects:checked').val() != undefined;
if (isChecked) {
var tr = $(this).closest('tr');
var stat = tr.children('.padLeft.status').text().trim();
var tableActiveProjects = $('#TableActiveProjects tbody');
var tableInactiveProjects = $('#TableInactiveProjects tbody');
tr.fadeOut(1000);
if (stat == 'Active') {
tableActiveProjects.prepend(tr);
} else {
tableInactiveProjects.prepend(tr);
}
tr.fadeIn(1000);
}
});
});
$('#TableActiveProjectsAddAllChecked').live('click', function () {
var tableAdminPortfolio = $('#TableAdminPortfolio tbody');
var list = $('#TableActiveProjects .CheckBoxProjects');
list.each(function () {
var tr = $(this).closest('tr');
var isChecked = $(this).closest('td').children('.CheckBoxProjects:checked').val() != undefined;
if (isChecked) {
tr.fadeOut(1000);
tableAdminPortfolio.prepend(tr);
tr.fadeIn(1000);
}
});
});
$('#TableInactiveProjectsAddAllChecked').live('click', function () {
var tableAdminPortfolio = $('#TableAdminPortfolio tbody');
var list = $('#TableInactiveProjects .CheckBoxProjects');
list.each(function () {
var tr = $(this).closest('tr');
var isChecked = $(this).closest('td').children('.CheckBoxProjects:checked').val() != undefined;
if (isChecked) {
tr.fadeOut(1000);
tableAdminPortfolio.prepend(tr);
tr.fadeIn(1000);
}
});
});
$('#showActivePanel').click(function () {
$('#displayActivePanel').show();
});
$('#hideActivePanel').click(function () {
$('#displayActivePanel').hide();
});
$('#showInactivePanel').click(function () {
$('#displayInactivePanel').show();
});
$('#hideInactivePanel').click(function () {
$('#displayInactivePanel').hide();
});
});
}
</script>