Używam tabeli Jquery Datatables z bPaginate = false, a sScrollY to pewna stała wysokość. Ostatecznie chcę, aby tabela zmieniła rozmiar w zdarzeniu window.resize.Datatables: Zmień wysokość tabeli nie działa
Aby uzyskać to do pracy I zbudowali mniejszy testcase: W poniższych fragmentów kodu chcę tabelę, aby zmienić rozmiar po kliknięciu przycisku
HTML:
<!DOCTYPE html>
<html>
<head>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
<script type="text/javascript" language="javascript" src="http://www.datatables.net/release-datatables/media/js/jquery.dataTables.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<input id="button" type="button" value="Click me!" />
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
<tr>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Engine version</th>
<th>CSS grade</th>
</tr>
</thead>
<tbody>
<tr class="odd gradeX">
<td>Trident</td>
<td>Internet
Explorer 4.0</td>
<td>Win 95+</td>
<td class="center"> 4</td>
<td class="center">X</td>
</tr>
<tr class="even gradeC">
<td>Trident</td>
<td>Internet
Explorer 5.0</td>
<td>Win 95+</td>
<td class="center">5</td>
<td class="center">C</td>
</tr>
<tr class="odd gradeA">
<td>Trident</td>
<td>Internet
Explorer 5.5</td>
<td>Win 95+</td>
<td class="center">5.5</td>
<td class="center">A</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Engine version</th>
<th>CSS grade</th>
</tr>
</tfoot>
</table>
</body>
</html>
Javascript:
$('#button').click(function() {
console.log('Handler for .click() called.');
table = $('#example').dataTable();
settings = table.fnSettings();
console.log('old:' + settings.oScroll.sY);
settings.oScroll.sY = '150px';
console.log('new:' + settings.oScroll.sY);
table.fnDraw(false);
});
$('#example').dataTable({
"sScrollY": "350px",
"bPaginate": false,
"bJQueryUI": true
});
wyjście
Konsola jest zgodnie z oczekiwaniami:
Handler for .click() called.
old:350px
new:150px
, ale tabela nie aktualizuje się! Każdy pomysł, co robię źle?
Żywy przykład można znaleźć tutaj: http://jsbin.com/anegiw/12/edit
spróbuj ustawić "bDestroy": true –
To spowoduje ponowne zainicjowanie tabeli, więc może działać dla tego fragmentu kodu. Ale czy byłoby właściwe dla programu obsługi zdarzeń window.resize, który jest wywoływany wiele razy podczas zmiany rozmiaru okna broser? – dwergkees
Właściwie, właśnie wypróbowałem to w zdarzeniu window.resize: działa, ale jest strasznie powolne, nawet na nowoczesnej maszynie. – dwergkees