3つの異なる「高さ」のケースを処理する方法は次のとおりです。
- 高さは動的に増加し、表示される行の数
maxHeight
に基づいて制限されます
- 高さは動的に増加し、制約はありません (ページはスクロールします)
- 高さは固定で静的です (たとえば、1 行のみの場合は空白が表示される場合があります)。
基本的にautoHeight
、ケース #2 とケース #1 に使用します (ただし、行数が目的の表示行数より少ない場合はケース #1 のみ)。ケース#1の場合rowHeight
、私は自分の発明を使用して、maxVisibleRows
開発者に「このテーブルはスクロールを開始する前に5行まで成長し、スクロール可能な領域の高さは5行の高さの合計に等しい」と言わせます。ケース #3 の場合、SlickGrid のデフォルトの動作である単純な親の高さの制約です。
サンプルコード:
var parentContainer = document.getElementById("slick_container");
var uniqueRecordIdField = "id";
var rows = [{id: 1, product: "Bells"}, {id: 2, product: "Whistles"}]; // data (array of rows)
var maxVisibleRows = 3; // user can see 3 rows without scrolling
var HeightOptions = { Static:0, Auto:1, Max:2 }; // "enum" for illustrative purposes
var HeightOption = HeightOptions.Auto; // dev would set this somehow
var options = { ... }; // slick grid options object
var parentHeight = "";
switch (HeightOption)
{
case HeightOptions.Static:
parentHeight = "400px;" // (hardcoded default/example; probably should allow override)
options.autoHeight = false; // do not use slickgrid auto height
break;
case HeightOptions.Max:
// use # of rows to determine whether table should be scrollable
if (maxVisibleRows > 0 && rows.length > maxVisibleRows)
{
var arbitraryHeightPadding = 14; // style hack for viewport
// constrain height to only show X rows
parentHeight = ((options.rowHeight * maxVisibleRows) + arbitraryHeightPadding) + "px";
options.autoHeight = false; // do not use slickgrid auto height
}
else
{
// use slickgrid autoHeight to allow height to shrink / grow
parentHeight = "";
options.autoHeight = true;
}
break;
case HeightOptions.Auto:
default:
parentHeight = ""; // or could use 'inherit' or 'auto' or whatever probably
options.autoHeight = true; // use slickgrid auto height to grow indefinitely
break;
}
// set height of slick grid parent (container)
parentContainer.style.height = parentHeight;
// do normal slick grid rendering stuff...
DataView.onRowCountChanged.subscribe(function (e, args)
{
Grid.updateRowCount();
Grid.render();
});
DataView.beginUpdate();
DataView.setItems(rows, uniqueRecordIdField);
DataView.endUpdate();