javascript - 如何将选择下拉列表附加到 HTML 数据表?

标签 javascript php html jquery datatables

我创建了一个主要使用 HTML、CSS、PHP 和 MYSQL 的网站,并添加了一个带有 Angular 色的选择下拉列表供用户选择。我需要它出现在表中的每一行用户上。我已成功让 tabledit 在网站上工作,但我不确定如何将此下拉列表附加到 Angular 色列。

HTML 是这样设置的

<body>
    <div class="panel panel-default">
        <!--        <div class="panel-heading">Sample Data</div>-->
        <div class="panel-body">
            <div class="table-responsive">
                <table id="sample_data" class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>Approval</th>
                        <th>Roles</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

<!--SELECT DROPDOWN LIST-->
<select id="test">
    <?php
    for ($a = 1; $a <= $count ; $a++){
        ?>

        <option value="1"><?php echo($roles[($a-1)]);?></option>

        <?php
    }
    ?>
</select>
<!--//////////////-->
</body>

<script>
    $(document).ready(function(){

        var dataTable = $('#sample_data').DataTable({
            "processing" : true,
            "serverSide" : true,
            "order" : [],
            "ajax" : {
                url:"FetchUserTable.php",
                type:"POST"
            }
        });

        $('#sample_data').on('draw.dt', function(){
            $('#sample_data').Tabledit({
                url:'ActionUserTable.php',
                dataType:'json',
                columns:{
                    identifier : [0, 'user_id'],
                    editable:[
                        [1, 'first_name'],
                        [2, 'last_name'],
                        [3, 'email'],
                        [4, 'admin_approved', '{"1":"Approved","2":"Disapproved"}']
                        // [5, 'role_id']
                    ]
                },
                restoreButton:false,
                onSuccess:function(data, textStatus, jqXHR)
                {
                    if(data.action == 'delete')
                    {
                        $('#' + data.id).remove();
                        $('#sample_data').DataTable().ajax.reload();
                    }
                }
            });
        });

    });

最佳答案

您可以使用 render option on the column definitions渲染任何你想要的。在渲染函数中,您可以访问行和整个数据对象。

$(function() {
  let $tbl = $('#sample_data'), $select = $('#select-box')

  let data = [
    [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011/04/25",
      "$3,120"
    ],
    [
      "Garrett Winters",
      "Director",
      "Edinburgh",
      "8422",
      "2011/07/25",
      "$5,300"
    ]
  ]

  $tbl.DataTable({
    data: data,

    columns: [
      null, null, null, null, {

        render: function(data, type, row, meta) {
          return $select.html() + `Use this to select "${data}"`
        }
      },
      {
        render: function(data, type, row, meta) {
          return $select.html() + `Use this to select "${data}"`
        }
      },
    ]
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.22/datatables.min.css" />

<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.22/datatables.min.js"></script>


<body>
  <div class="panel panel-default">
    <!--        <div class="panel-heading">Sample Data</div>-->
    <div class="panel-body">
      <div class="table-responsive">
        <table id="sample_data" class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Email</th>
              <th>Approval</th>
              <th>Roles</th>
            </tr>
          </thead>
          <tbody>

          </tbody>
        </table>
      </div>
    </div>
  </div>

  <div style="display:none;" id="select-box">
    <select>
      <option>Role 1</option>
    </select>
  </div>
</body>

关于javascript - 如何将选择下拉列表附加到 HTML 数据表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64648284/

相关文章:

html - 用绝对内容填充相对容器

javascript - 从多个嵌套数组中提取值组合的问题

javascript - 动态创建的按钮上的单击事件

php - 在php中从数据库检索并比较时间

php - 为 MySQL 数据库中的行设置唯一 ID

html - 我无法将 <td> 中的复选框和单选按钮居中

jquery - 动画元素从一个列表移动到另一个

Javascript。从特定位置获取节点

javascript - HTML Javascript - 获取所有具有 ID 的元素

javascript - 无法加载 swf 文件运行时