在使用ajax请求获取数据时,如何在表中添加if条件?

zz2j4svz  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(227)

我有一个表,当点击搜索按钮时,数据将显示在其中。所以我已经为它编写了一个ajax,它工作得非常好,但是我现在面临的问题是根据条件显示操作按钮。
例如,我有两个按钮,一个是 Add card 另一个是 Receive payment . 条件是,如果用户已经付款,则 add card 按钮将以其他方式显示 receive payment 按钮将显示。如何在ajax成功响应中添加if-else条件。

<table class="table table-hover" id='userList'>
    <thead>
        <tr class="bg-primary text-white">
            <th scope="col"> # </th>
            <th scope="col"> Name </th>
            <th scope="col"> Email </th>
            <th scope="col"> Mobile </th>
            <th scope="col"> Action </th>  
        </tr>
    </thead>
    <tbody></tbody>
</table>
``` `Ajax to fetch data and populate the data into table body` ```
$("#submit").click(function(){
    let search = $("#search").val();
    var url = "<?php echo Yii::app()->getBaseUrl(true) ?>/search";
    $.ajax({
        url : url,
        dataType: "json",
        method: 'post',
        data: {search:search},
        success: function( response ) {
            if(response.length>0){
                let count=1;
                response.forEach((item) => {
                        var string = "<tr>" +
                        "<td>" + count + "</td>" +
                        "<td>" + item.name + "</td>" +
                        "<td>" + item.email + "</td>" +
                        "<td>" + item.mobile + "</td>" +
                        "<td>" + 
                            "<a href=<?php echo Yii::app()->getBaseUrl(true) ?>/user?id="+item.id+" title='Add card' class='btn btn-warning btn-sm btn-square rounded-pill'><span class='btn-icon icofont-simple-smile'></span></a>"+
                            "<a href=javascript:void(0); 'onclick=confirm('<?php echo Yii::app()->getBaseUrl(true) ?>/delete?id="+item.id+",Are you sure to delete user?)' title='Delete user' class='btn btn-warning btn-sm btn-square rounded-pill'><span class='btn-icon icofont-medical-sign-alt'></span></a>" +
                            "<a href=<?php echo Yii::app()->getBaseUrl(true) ?>/receivepayment?id="+item.id+"&type=payment class='btn btn-primary btn-sm btn-square rounded-pill' title='Receive payment'><span class='btn-icon icofont-rupee-false'></span></a>"
                            + "</td>" +
                        "</tr>";
                        $("#userList tbody").append(string);
                    count++
                });
            }else {
                $('#userList tbody').empty();
            }
        }
    });
});

使用普通表只需简单地执行if-else即可。

<?php if($row['is_paid'] == 'Y'){ ?>
    <a href="<?php echo Yii::app()->getBaseUrl(true) ?>/user?id=<?php echo $row['id']; ?>" title="Add card" class="btn btn-warning btn-sm btn-square rounded-pill"><span class="btn-icon icofont-simple-smile"></span></a>
<?php }  else{ ?>
    <a href="<?php echo Yii::app()->getBaseUrl(true) ?>/receivepayment?id=<?php echo $row['id']; ?>&type=payment" class="btn btn-primary btn-sm btn-square rounded-pill" title="Receive payment"><span class="btn-icon icofont-rupee-false"></span></a>
<?php } ?>

此外,单击时的删除按钮不起作用,它不会弹出确认窗口。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题