ListViewのヘッダーを固定する方法 | asp.net
- 2017/04/02
- 22:55
今回はasp.netのListViewで行と列のヘッダーを固定する方法をご紹介いたします。
ヘッダーとデータ部分にそれぞれDivを設置してそれぞれにoverflowを設定しても列幅の同期がとれずに、構築することが困難です。
そこでヘッダー固定に有名なFixedMidashi.jsを使用します。数行のソースで設定が簡単なところと、最適化される前のjsが含まれていますので、何か不具合があっても対応が可能なところが良いですね。
FixedMidashiをHtmlに適用する基本的な方法はVectorの公式ページに紹介されていますので、ご確認ください。
http://hp.vector.co.jp/authors/VA056612/fixed_midashi/manual/index.html

前提条件
・Windows 7 Professional 以降 / Windows Server 2008 R2 以降
・Visual Studio 2008 Professional 以降
・VB.NET
・.Net Framework 3.5 以降
1.JavaScriptを設定する
<script src="Scripts/fixed_midashi.js"></script>
2.BodyにFixdMidashiの初期化メソッドを設定する
<body onload="FixedMidashi.create();">
3.ListViewをDivで囲む
<div class="table-area">
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemPlaceHolder1">
※注意点として、ListView内には不要なDivを設定しないことが重要です。
4.tableタグに固定したい行列番号を指定する
<LayoutTemplate>
<table _fixedhead="rows:1; cols:2; div-auto-size: none;">
列ヘッダー(タイトル部分)が2行ある場合は rows:2
行ヘッダー(データの左側)の固定が要らない場合は cols:0 とします。
5.CSSを設定する
ListViewを囲んでいるDivのセレクタにoverflow:autoを設定することと、各列の幅を設定することがポイントです。.table-area {
overflow: auto;
height: 220px;
width : 600px;
}
/*以下、列毎の設定*/
table tr .id-field {
min-width : 50px;
width : 50px;
}
table tr .name-field{
min-width : 150px;
width : 150px;
}
table tr .age-field {
min-width : 50px;
width : 50px;
}
table tr .result-field {
min-width : 200px;
width : 200px;
}
6.サンプルコード
aspx<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ListViewTest.aspx.vb" Inherits="ListViewTextChanged.ListViewTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>ListViewSample</title>
<link rel="stylesheet" type="text/css" href="~/Scripts/Style.css" media="all" />
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/fixed_midashi.js"></script>
</head>
<body onload="FixedMidashi.create();">
<form id="form1" runat="server">
<div class="table-area">
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemPlaceHolder1">
<EmptyDataTemplate>
<table>
<tr>
<th runat="server" class="check-field">
</th>
<th runat="server" class="id-field">
<label>ID</label>
</th>
<th runat="server" class="name-field">
<label>名前</label>
</th>
<th runat="server" class="age-field">
<label>年齢</label>
</th>
<th runat="server" class="result-field">
<label>結果</label>
</th>
</tr>
</table>
<p class="error-text">
データが存在しません。
</p>
</EmptyDataTemplate>
<LayoutTemplate>
<table _fixedhead="rows:1; cols:2; div-auto-size: none;">
<thead>
<tr>
<th runat="server" class="check-field">
<asp:Checkbox id="chkAll" runat="server" onclick="CheckAll(this)"/>
</th>
<th runat="server" class="id-field">
<label>ID</label>
</th>
<th runat="server" class="name-field">
<label>名前</label>
</th>
<th runat="server" class="age-field">
<label>年齢</label>
</th>
<th runat="server" class="result-field">
<label>結果</label>
</th>
</tr>
</thead>
<tbody>
<tr runat="server" id="itemPlaceHolder1"></tr>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td runat="server" class="check-field">
<asp:Checkbox ID="chkSelect" runat="server" Text="" Checked='<%# Eval("IsChecked")%>' class="checkedAll"></asp:Checkbox>
</td>
<td runat="server" class="id-field">
<asp:Label ID="lblID" runat="server" Text='<%# (Eval("ID")) %>'></asp:Label>
</td>
<td runat="server" class="name-field">
<asp:Label ID="lblName" runat="server" Text='<%# (Eval("NAME")) %>'></asp:Label>
</td>
<td runat="server" class="age-field">
<asp:TextBox ID="txtAge" runat="server" AutoPostBack="True" OnTextChanged="txtAge_TextChanged"></asp:TextBox>
</td>
<td runat="server" class="result-field">
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
<script type="text/javascript">
function CheckAll(chk) {
var rowCnt = $(".checkedAll").length;
for (var i = 0; i < rowCnt; i++) {
var childChkBox = $(".checkedAll")[i].childNodes[0]
childChkBox.checked = chk.checked;
FixedMidashi.syncValue(childChkBox);
}
}
</script>
</form>
</body>
</html>
css
body {
}
.error-text {
color: #FF0000;
font: bold;
}
.table-area {
overflow: auto;
height: 220px;
width : 600px;
}
/*テーブル全体の設定*/
table, tr, th, td {
border-collapse: collapse;
}
/*テーブルヘッダーの設定*/
table tr th {
background-color : #b0c4de ;
text-align: center;
}
/*テーブルの罫線の設定*/
table tr th, table tr td {
border: 1px #778899 solid;
margin: 0px;
padding: 10px;
}
/*以下、列毎の設定*/
table tr .id-field {
min-width : 50px;
width : 50px;
}
table tr td.id-field {
text-align: right;
}
table tr .name-field{
min-width : 150px;
width : 150px;
}
table tr .age-field {
min-width : 50px;
width : 50px;
}
table tr td.age-field {
text-align: right;
}
table tr .result-field {
min-width : 200px;
width : 200px;
}
最後までお読みいただきありがとうございます。
いかがでしたでしょうか。他にも asp.net に関連する記事を投稿しておりますのでよろしければご参考くださいませ。