计算动态创建的表中的行数

时间:2015-02-20 10:50:20

标签: jquery

我在点击按钮时向表中添加行。

我想要做的是当用户点击一个按钮时,检查动态生成的行数。如果该表中的行数为" 0" - 空白,显示模态弹出窗口。

否则不要显示模态。

我当前的jQuery只为num_rows返回0。

$('.odds-btn').click(function()
    {
        var rowCount = $('#betting_table tr').length;

        var bet_type = $(this).attr('data-bet-type');
        var bet_line = $(this).attr('data-bet-line');

        if(bet_line == undefined)
        {
            bet_line = '0';
        }

        var game_id = $(this).attr('data-game-id');
        var bet_id = $(this).attr('data-offer-type-id');
        var team = $(this).attr('data-team-name');
        var odds = $(this).attr('data-odds-fractional');

        var url = "/index/build_betslip/" + bet_type + "/" + game_id + "/" + bet_id + "/" + team + "/" + odds + "/" + bet_line;

        $.ajax({
            type:   "GET",
            url: url,
            success:function()
            {
                alert(rowCount);

                if(rowCount == 0)
                {
                    // Add To The Bet Slip....
                    $('.slip-modal').modal('show');
                }

            }
        })

    })

我正在使用的PHP函数是:

public function build_betslip()
    {
        // Get Bet Type (1, 2, X, bttsyes, bttsno, under, over)
        $bet_type = $this->uri->segment(3);

        // Get Game ID
        $game_id = $this->uri->segment(4);

        // Get Bet ID (e.g 1X2 = WLD)
        $bet_id = $this->uri->segment(5);

        // Get Team Name
        $teamname = $this->uri->segment(6);

        // Get Odds
        $odds1 = $this->uri->segment(7);
        $odds2 = $this->uri->segment(8);

        // Get Bet Line (1.5 / 2.5 - For Under / Over Market)
        $bet_line = $this->uri->segment(9);

        $odds = $odds1;
        $odds .= "/";
        $odds .= $odds2;

        // Build An array titled Bet
        $bet = array(
            '0' => array(
                'bet_type'  =>  $bet_type,
                'game_id'   =>  $game_id,
                'bet_id'    =>  $bet_id,
                'team'      =>  urldecode($teamname),
                'odds'      =>  $odds,
                'line'      =>  $bet_line
            )
        );

        $betslip = $this->session->userdata('betslip');

        // Create The Betslip For The First Time...
        if(empty($betslip))
        {
            $this->session->set_userdata('betslip', $bet);
        }
        else
        {
            // Add To The Betslip Array...
            $betslip[] = array(
                'bet_type'  =>  $bet_type,
                'game_id'   =>  $game_id,
                'bet_id'    =>  $bet_id,
                'team'      =>  urldecode($teamname),
                'odds'      =>  $odds,
                'line'      =>  $bet_line
            );

            $this->session->set_userdata('betslip', $betslip);
        }

    }

    /*
     * This Function Gets
     * The Current Users' Bet Slip
     * And builds a table based on it and returns as
     * Suitable data
     */

    public function get_betslip()
    {
        $betslip = $this->session->userdata('betslip');

        if(empty($betslip))
        {
            echo "<p>Bet slip is currently empty</p>";
        }
        else
        {
            // Return The Table...
            echo "<table class='table' id='betting_table'>";
                echo "<thead>";
                    echo "<th>&nbsp;</th>";
                    echo "<th>Your Selection</th>";
                    echo "<th>Odds</th>";
                echo "</thead>";
                echo "<tbody>";
                    foreach($betslip as $key => $value)
                    {
                        echo '<tr class="table-row-' . $key .'">';
                        echo "<td><a class='bet-cross' data-row='$key' href='#'>x</a></td>";
                        echo "<td>";
                        echo $value['team'];
                        echo "</td>";
                        echo "<td>";
                        echo $value['odds'];
                        echo "</td>";
                        echo "</tr>";
                    }
                echo "</tbody>";
            echo "</table>";

            echo "<a class='clear-slip'  href='".base_url('betslip/clear')."'>Clear Slip</a>";

            echo "<a class='site-btn compare' href='".base_url('betslip')."'></a>";
        }


    }

来自AJAX代码的示例响应:

<table class='table' id='betting_table'><thead><th>&nbsp;</th><th>Your Selection</th><th>Odds</th></thead><tbody><tr class="table-row-0"><td><a class='bet-cross' data-row='0' href='#'>x</a></td><td>Wigan Athletic VS Charlton Athletic &#40;Draw&#41;</td><td>5/2</td></tr><tr class="table-row-1"><td><a class='bet-cross' data-row='1' href='#'>x</a></td><td>Atalanta</td><td>21/1</td></tr></tbody></table><a class='clear-slip'  href='http://local.oddssweeper/betslip/clear'>Clear Slip</a><a class='site-btn compare' href='http://local.oddssweeper/betslip'></a>

默认情况下,该表为空。

由于

2 个答案:

答案 0 :(得分:0)

您必须首先验证表格中是否存在行<tr>

例如:

var rowCount = $('#betting_table').find('tr').length;

上面的代码发现表中是否存在行。

答案 1 :(得分:0)

我可以看到你从ajax返回表格,所以为了使计数器工作,你需要在ajax成功回调中移动var rowCount = $('#betting_table tr').length;并在response上运行选择,然后{{1}将找到该表并给你计数

jQuery