【问题标题】:Codeigniter create dynamic table and dropdownlistCodeigniter 创建动态表和下拉列表
【发布时间】:2015-11-06 20:41:38
【问题描述】:

我想创建表并在用户单击按钮时动态添加行。对于同一个表列,我想获取数据 从数据库中并在下拉列表中显示它。我使用ajax函数来获取数据。 Ajax 功能也可以正常工作,但我无法添加 表格的新行。请帮我解决这个问题。

这是我的代码; 我觉得在 JavaScript 函数中添加 PHP 代码的方式有问题,但我不知道如何解决。请帮帮我。

html ....................................

<table class="table table-bordered table-hover table-striped">
                                    <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Group</th>                                
                                        <th>Quantity</th>                                
                                        <th>Status</th>
                                        <th>Cost</th>
                                        <th><input type="button" class="addrow btn btn-success" value="Add Row "></th>
                                    </tr>
                                    </thead>
                                    <tbody id="body">
                                    <tr>
                                        <th>1</th>
                                        <td>
                                            <?php $Treatments['#'] = 'Please Select'; ?>
                                            <?php echo form_dropdown('tcode', $Treatments, '#', 'class="form-control" id="group"'); ?>

                                        </td>

                                        <td><input type="text" class="form-control quantity" size="3" id="quantity" name="quantity[]" size="3" /></td>

                                        <td>
                                            <select class="form-control status" id="status[]" name="status[]">
                                                <option>Proposed</option>
                                                <option>Done</option>
                                                <option>Not Done</option>
                                                <option>Not Required</option>
                                            </select>
                                        </td>

 <td><input type="text" class="form-control cost" size="3" id="cost" name="cost[]" /></td>
</tr>

 </tbody>
</table>

javascript ..................................................

$(function(){
        $('.addrow').click(function(){
            addrow();
        });
 function addrow()
        {
            var row =($('#body tr').length-0)+1;
            var tr ='<tr>'+
                '<th>'+row+'</th>'+
                '<td>'+
                     '<?php $Treatments['#'] = 'Please Select'; ?>'+
                     '<?php echo form_dropdown('tcode', $Treatments, '#', 'class="form-control" id="group"'); ?>'+

                '</td>'+                
                '<td><input type="text" class="form-control quantity" id="quantity" name="quantity[]" size="3"></td>'+

                '<td><input type="text" class="form-control cost" size="3" id="cost" name="cost[]" /></td>'+
                '</tr>';
            $('#body').append(tr);
        }
    });     

【问题讨论】:

    标签: php jquery codeigniter dynamic html-table


    【解决方案1】:

    试试这个,

    $('#body tr:last').after(tr);
    

    小提琴链接:http://jsfiddle.net/bMyGY/1137/

    【讨论】:

      猜你喜欢
      • 2021-05-18
      • 1970-01-01
      • 2018-11-17
      • 2018-07-22
      • 1970-01-01
      • 2015-03-22
      • 2018-09-12
      • 2013-07-05
      • 2019-11-26
      相关资源
      最近更新 更多