javascript - 通过第一列制作可扩展的表格 View 单元格

标签 javascript jquery html ajax html-table

我有一个包含多行的表格,用户可以点击该行的任意宽度,它会展开以提供额外信息。这是一个working sample

html表格代码

<table id="report" border="1" style="width:100%" >
    <tr>
        <th> First </th>
        <th> Second </th>
        <th> Third </th>
    </tr>

    <tr>
         <td>1st title</td>
         <td>1</td>
         <td>1</td>
    </tr>   
    <tr>
        <td colspan="10">
            dummy text 1<br>
            dummy text 1<br>
            dummy text 1<br>
        </td>
    </tr>   

    <tr>
         <td>2nd title</td>
         <td>2</td>
         <td>2</td>
    </tr>   
    <tr>
        <td colspan="10">
            dummy text 2 <br>
            dummy text 2<br>
            dummy text 2<br>
        </td>
    </tr>           

</table>

脚本代码

$(document).ready(function(){
    $("#report tbody tr:odd").addClass("odd");
    $("#report tbody tr:not(.odd)").hide();
    $("#report tbody tr:first-child").show();

    $("#report tbody tr.odd").click(function(){
        $(this).next("tr").toggle();
        $(this).find(".arrow").toggleClass("up");
    });
});

我正在尝试稍微修改此表,我希望当用户单击第一列中的任何值时(在本例中用户单击第一个标题或第二个标题),然后只有该行的 View 应该展开。目前, View 会在行的任何位置展开。谁能告诉我怎么做

最佳答案

如果您只想成为可点击的第一列,请将事件附加到每行的第一个 td:

   $(document).ready(function(){
        $("#report tbody tr:odd").addClass("odd");
        $("#report tbody tr:not(.odd)").hide();
        $("#report tbody tr:first-child").show();

        $("#report tbody tr.odd td:first-child").click(function(){
            $(this).parent().next("tr").toggle();
            $(this).find(".arrow").toggleClass("up");
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<table id="report" border="1" style="width:100%" >
    <tr>
        <th> First </th>
        <th> Second </th>
        <th> Third </th>
    </tr>

    <tr>
         <td>1st title</td>
         <td>1</td>
         <td>1</td>
    </tr>   
    <tr>
        <td colspan="10">
            dummy text 1<br>
            dummy text 1<br>
            dummy text 1<br>
        </td>
    </tr>   

    <tr>
         <td>2nd title</td>
         <td>2</td>
         <td>2</td>
    </tr>   
    <tr>
        <td colspan="10">
            dummy text 2 <br>
            dummy text 2<br>
            dummy text 2<br>
        </td>
    </tr>           

</table>

关于javascript - 通过第一列制作可扩展的表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32714599/

相关文章:

html - 在具有 haslayout 属性的 div 中输入,即具有 "margin-left"

html - 修复滚动修复底部的div

javascript - 自定义四舍五入到最接近的 0.05

javascript - 奇怪的执行问题 - 标准输出未显示 - 回调不起作用

javascript - 在 shopify 搜索栏中删除冒号搜索运算符

javascript - 使用变量设置传单圆 LatLng

javascript - Ng-include 仅在第二次出现后出现

javascript - Mootools 到 jquery 对象问题

javascript - jQuery如何加载图像的一部分

html - 重定向到另一个页面元标记