当用户点击thead中的单元格时,我会用输入元素和过滤器图标填充它。问题是它使列的宽度跳跃:
<table class="table">
<thead>
<tr>
<th>One</th>
<th>Two</th>
</tr>
<tr>
<th class='clickhere'>Click here
</th>
<th class='clickhere'>or here</th>
</tr>
</thead>
<tbody>
<tr>
<td>11</td>
<td>21</td>
</tr>
<tr>
<td>12</td>
<td>22</td>
</tr>
</tbody>
<table>
$(document).on('blur','.clickhere input',filterBlurred);
function filterBlurred() {
if ($(this).val() === '') {
$(this).closest('tr').find('th').html(' ');
}
}
$(document).on('click','.clickhere',filter);
function filter() {
$(this).html('<div class="input-group"><input class="form-control"><span class="btn input-group-addon glyphicon glyphicon-filter"> </span></div>');
$(this).find('input').focus();
}
答案 0 :(得分:0)
(function() {
var Variables = {};
Variables.theadID = 0;
// For every thead
$('thead').not('.no-filter').each(function(index, thead) {
var local = {};
if ($(thead).attr('id')) {
local.id = $(thead).attr('id');
} else {
// We'll assign a unique id
Variables.theadID += 1;
local.id = 'fw0Thead' + Variables.theadID;
$(thead).attr('id',local.id);
}
// Set the max-width for each column via css.
local.str = '';
$('th',this).each(function(index, element) { // for every th inside of this thead
if (!$(this).hasClass('no-filter')) {
local.str += '#' + local.id;
local.str += ' tr.fw0Filter th:nth-child(' + (index+1) + ') {';
local.str += ' max-width:' + $(this).width() + 'px;';
local.str += '}';
}
});
local.css = document.createElement("style");
local.css.type = "text/css";
local.css.innerHTML = local.str;
document.body.appendChild(local.css);
});
})();