2

グリッドビューのネストまたはサブグリッドビューの作成に関するトピックについては、いくつかの質問があります。私はこのアプローチを検討しましたが、私の目的には多すぎます。私が見つけた最も近い既存の質問はこれでした:グループ化されたGridview

残念ながら、これにはグループ化行を作成する方法についてのアドバイスがいくつかありますが、それらを折りたたみ可能にすることにはなりません。

私の要件は、ユーザーに行を区切ったグリッドビューを表示してほしいということです。

-グループ1
データ1| データ2| データ3
データ1| データ2| データ3
データ1| データ2| データ3-
グループ2
データ1| データ2| データ3
データ1| データ2| データ3-
グループ3
データ1| データ2| データ3
データ1| データ2| データ3
データ1| データ2| データ3
データ1| データ2| データ3

ユーザーが次のように表示したい場合は、次のようにすることができます。

+グループ1-
グループ2
データ1| データ2| データ3
データ1| データ2| データ3-
グループ3
データ1| データ2| データ3
データ1| データ2| データ3
データ1| データ2| データ3
データ1| データ2| データ3

またはこれ:

+グループ
1+グループ
2+グループ3

基本的に、すべてのグループ化行に含まれるのはグループのタイトルです。それらは実際には適切なGridview行でさえありません。実際の行は適切なグリッドビューであり、それ以上のドリルダウン機能は必要ありません。

私のソリューションをクライアントサイドで実行できるようにしたいと思います。javascriptまたはjQuery(jQuery-ui 1.8.8を含む)を使用できますが、使用しているAJAXツールキットの数を任意に拡張できないという制約があります。複数のグループ展開ポストバックを介してページの状態を常に管理する必要はありません。

これは達成できることですか?誰かが私にナッジを与えるかもしれないリソースの方向に私を向けることができますか?

編集:ああ、はい、そして私は言及するのを忘れました。ベースグリッドビューの行には、ボタン、テキストボックス、チェックボックス、ドロップダウンなどのコントロールが含まれる場合がありますが、これらに限定されない場合があります。

4

1 に答える 1

8

あなたは実際のコードを提供していないので、この他の質問に基づいて必要なことを達成する方法の例をまとめました.

もう 1 つの質問は、単純にサーバーのドライブ C にあるファイルを取得し、それらをグリッド内で作成時間の降順でグループ化します。リピーターのマークアップは次のとおりです。

<asp:HiddenField ID="dataGroups" runat="server" />
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_RowDataBound" >
    <ItemTemplate>          
        <!-- Bind to your specific properties i.e. Invoice #, file type, etc. -->
        <table id="tableItem"  runat="server">
            <tr>
                <td style="width: 200px;">
                    <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                </td>
                <td style="width: 200px;">
                    <asp:Label ID="lblDirName" runat="server" Text='<%#Eval("DirectoryName") %>'></asp:Label>
                </td>
                <td style="width: 200px;">
                    <asp:Label ID="lblCreationTime" runat="server" Text='<%#Eval("CreationTime") %>'></asp:Label>
                </td>
                <td style="50px">
                <asp:Button ID="btnAction" runat="server"  Text="Hit me" OnClick="btnAction_Click"/>
                </td>
            </tr>
        </table>
    </ItemTemplate>       
</asp:Repeater>

OnRowDataBoundイベントのコード ビハインドは次のとおりです。それがあなたが使用するものであるため、C#で書かれています:

private int month = -1;
private int year = -1;
protected void rpt_RowDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Binding to FileInfo objects. 
        //Since we are grouping by CreationTime we need to check if it's time to create a new "group"
        //Is current month and year different from the value previously stored on the month and year variables?
        if (month != (e.Item.DataItem as FileInfo).CreationTime.Month || year != (e.Item.DataItem as FileInfo).CreationTime.Year)
        {   
            month = (e.Item.DataItem as FileInfo).CreationTime.Month;
            year = (e.Item.DataItem as FileInfo).CreationTime.Year;

            //append the current group to the hidden variable "dataGroups" which will tell us quickly how many groups we have in total
            dataGroups.Value += (e.Item.DataItem as FileInfo).CreationTime.ToString("yyyMM") + ",";
        }
        //for every row; "stamp it" with this attribute since we'll use it on the client side with jQuery
        (e.Item.FindControl("tableItem") as HtmlTable).Attributes.Add("data-group", (e.Item.DataItem as FileInfo).CreationTime.ToString("yyyMM"));
    }
}

今度はクライアント側です。折りたたみ可能なパネルを作成するには、jQuery マジックを実行する必要があります。

<link href="css/flick/jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.8.22.custom.min.js" type="text/javascript"></script>

 <script type="text/javascript">
     $(document).ready(function () {
         //asp hidden element containing the list of groups separated by commas.
         //Check code behind of RowDataBound to see where is this populated
         var dataGroups = $('#<%=dataGroups.ClientID%>').val().split(',');
         for (var i = 0; i < dataGroups.length; i++) {
             //split() doesn't have an option to ignore empty strings so we'll just ignore it
             if (dataGroups[i] != '') {
                 //select all table elements with the data-group value matching the 
                 //current group we are iterating over and enclose them all 
                 //inside a div; effectively creating a "group"
                 $('table').filter(function (inputs) {
                     return $(this).data('group') == dataGroups[i];
                 }).wrapAll("<div class='accordion'>");
             }
         }
         var accordions = $('.accordion');
         //now, for every div enclosing the groups, create a Handle that will work as the element that
         //collapses or expands the group
         $(accordions).wrapInner("<div>").prepend('<h3><a href="#">Handle</a></h3>');

         //Now replace the word "Handle" above for the actual group number/name or what have you
         for (var i = 0; i < accordions.length; i++) {
             $(accordions[i]).find('h3 a').text("Group " + $(accordions[i]).find('table:first').data('group'));
         }

         //finally call jQuery.accordion to create the accordions on every group
         $('.accordion').accordion({ collapsible: true, autoHeight: false });
     });
 </script>

さて、これらのコード行はこれを生成します:

ここに画像の説明を入力

于 2012-07-25T19:39:30.077 に答える