dougai8673 2018-07-25 19:56 采纳率: 100%
浏览 30
已采纳

表格排序打开子行上的行单击

The code I have posted below shows a child row when you click on the value inside of a row, in this example with colindex of 2.

I was wondering what way I could make the same thing happen but when you click anywhere on the row. I am not sure on how to do this. The way I am creating my table is by using tablesorter and a php query. The display of the table works fine, I am just having issues finding documentation on displaying the child row when a row is clicked not the value.

My PHP:

<?php
    foreach($report_tabs as $report) {
        $tag=$report["tag"];
        $title=$report["title"];
        $hcols=$report["hdrcols"];
        $cols=$report["datacols"];
        $db_query=$report["dbquery"];

        echo "<div id='my_test_table_tab' class='tab-pane active'>";
            echo "<table id='test_table' class='table table-hover tablesorter'>";
                echo "<thead>";
                    echo "<tr>";
                            // removed the hard coded column headers and took the ones from our query
                            global $hcols;
                            foreach($hcols as $column_header) {
                                echo "<th>$column_header</th>";
                            }
                    echo "</tr>";
                echo "</thead>";
                echo "<tbody>";
                        //Use queried data to create each row of the table
                        $rowcount=0;
                        //Creating checker health table & filling with data
                        if ( isset($db_query)) {
                            while($row = mysqli_fetch_array($db_query)) {
                                $rowcount++;
                                // removed the hard coded column set and made it driven off of the array below
                                echo "<tr>";
                                $colindex = 0;
                                foreach( $cols as $column_name ) {
                                    $val = $row[$column_name];
                                    if ($colindex == 2) {
                                        echo "<td style='text-align: left;  width: 1pt;'><a href='#' class='toggle' onClick='drawChart(\"$val\");'>$val</a></td>";
                                        $tempval = $val;
                                    } else {
                                        echo "<td style='width:100pt; text-align='right'>$val</td>";
                                    }
                                    $colindex++;
                                }
                                echo "</tr>";
                                echo "<tr class='tablesorter-childRow'>";
                                    echo "<td colspan='3'>";
                                        echo "<div id='$tempval' style='height: 400px;'></div>";
                                        echo "<div id='fail$tempval' style='height: 400px;'></div>";
                                    echo "</td>";
                                echo "</tr>";
                            }
                        }
                echo "</tbody>";
            echo "</table>";
            echo "<h4>$rowcount rows retrieved</h4>";
        echo "</div>";
    }
?>

My TableSorter function to open based on value click:

<script>
// Table Sorter Options
$(document).ready(function(){
    // Turns all tables with the 'tablesorter' class into tablesorter tables with the given widgets
    $(".tablesorter").tablesorter({
        // stickyHeaders - Keeps headers always visible when scrolling down
        // filter - Adds filter boxes to each column
        cssChildRow : "tablesorter-childRow",
        widgets: ['stickyHeaders','filter'],
        widgetOptions: {
            stickyHeaders_offset : 50,
            filter_placeholder : {search : ''},
            filter_saveFilters: true,
            pager_output: '{startRow} - {endRow} / {filteredRows} ({totalRows})',
            pager_removeRows: false,
            filter_childRows  : true,
            filter_cssFilter  : 'tablesorter-filter',
            filter_startsWith : false,
            filter_ignoreCase : true
        },
    });
    // Clear buttons
    add_clear_buttons();

    var table = $( '#my_test_table_tab' );

    // hide child rows
    table.find( '.tablesorter-childRow td' ).addClass( 'hidden' );

    // Toggle child row content (td), not hiding the row since we are using rowspan
    table.delegate( '.toggle', 'click' ,function() {
    // use "nextUntil" to toggle multiple child rows
    // toggle table cells instead of the row
    $( this )
        .closest( 'tr' )
        .nextUntil( 'tr.tablesorter-hasChildRow' )
        .find( 'td' )
        .toggleClass( 'hidden' );
    return false;
    });

    // Toggle filter_childRows option
    $( 'button.toggle-combined' ).click( function() {
        var widget_options = table[0].config.widgetOptions,
        options = !widget_options.filter_childRows;
        widget_options.filter_childRows = options;
        $( '.state1' ).html( options.toString() );
        // update filter; include false parameter to force a new search
        table.trigger( 'search', false );
        return false;
    });
});
</script>
  • 写回答

1条回答 默认 最新

  • duanhuhong5255 2018-07-28 05:19
    关注

    All you'd need to do is change the delegate function (why are you using such an old version of jQuery?), to point to the row instead of the link.

    table.delegate( '.tablesorter-hasChildRow', 'click' ,function() {
    

    I set up this demo, which is using a newer version of jQuery - the delegate function has been deprecated, so use on instead:

    $table.on('click', '.tablesorter-hasChildRow', function() {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历