简体   繁体   English

如何在数据表的每一行中添加下拉列表

[英]How to add dropdown list in each row of datatable

I have a datatable with 3 columns : Username, email and role. 我有一个包含3列的数据表:用户名,电子邮件和角色。 In the header of 3rd column (role), there is a button "Edit". 在第三列(角色)的标题中,有一个“编辑”按钮。 On clicking "Edit", I want dropdown menu should come on each row. 点击“编辑”后,我希望下拉菜单出现在每一行上。 "Edit" button should be hidden and "Save" button should come up. “编辑”按钮应该被隐藏,“保存”按钮应该出现。 Using the following code, I am able to add dropdown menu, but I am not able to select anything from dropdown menu. 使用以下代码,我能够添加下拉菜单,但无法从下拉菜单中选择任何内容。

<script type="text/javascript">
$(document).ready(
    function() {
    var oTable = $('#big_table').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": '<?php echo base_url(); ?>index.php/User/datatable',
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "iDisplayStart ":20,
    "oLanguage": {
    "sProcessing": "<img src='<?php echo base_url(); ?>assets/images/ajax-loader_dark.gif'>"
    },  

    "aoColumnDefs": [
         { "bSortable": false, "aTargets": [ 2 ] }
        ],      

    "fnInitComplete": function() {
       $("#big_table tr").find("th:nth-child(3)").append('<button id = "editTool">Edit</button><button id = "save"> Save </button>');      
            $("#save").hide();
     },
     "fnDrawCallback": function() {
                },
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            },
            'fnServerData': function(sSource, aoData, fnCallback)
        {
          $.ajax
          ({
            'dataType': 'json',
            'type'    : 'POST',
            'url'     : sSource,
            'data'    : aoData,
            'success' : fnCallback
          });
        }
} );
$("#big_table").on('click',$("#editTool"),function(){
        console.log("editTool click");
            console.log("Edit");
            $("#big_table tbody tr").each(function(index){
            var selected = "Admin1";
            var name = "role"+index;
            var menu = '<?php 
            $options = array(
                'Student'       => 'Student',
                'Contributer'   => 'Contributer',
                'Moderator'     => 'Moderator',
                'Admin1'        => 'Admin 1', 
                'Admin2'        => 'Admin 2',
             );
            $selected = "'+selected+'";
            $val =  form_dropdown("'+name+'", $options,"'+selected+'");
            echo preg_replace( "/\r|\n/","",$val)?>';
            $(this).find("td:nth-child(3)").html(menu);
            $("#editTool").hide();
            $("#save").show();
         });
     });

} );
</script>

I got the problem. 我有问题。 The problem lies with the following code: 问题在于以下代码:

    $("#big_table").on('click',$("#editTool"),function(){
      //codes
    });

This is triggered each time we click on #big_table. 每次我们单击#big_table都会触发此操作。 For the first time, when I click on "edit" button, this method is called and the dropdown box is getting added. 第一次,当我单击“编辑”按钮时,将调用此方法,并且将添加下拉框。 When I try to select something from dropdown, this method is invoked once again, because the click is still on #bigTable. 当我尝试从下拉列表中选择内容时,该方法将再次被调用,因为单击仍然在#bigTable上。 I solved this issue by replacing the above by following: 我通过替换以下内容解决了此问题:

   $("#big_table thead th:nth-child(3)").on('click',$("#editTool"),function(){
    //code
    });

Now this method is invoked whenever third header is clicked. 现在,只要单击第三个标题,就会调用此方法。 So now I can properly select from the dropdown menu. 因此,现在我可以从下拉菜单中正确选择了。 But still my goal is not achieved. 但是我的目标仍然没有实现。 I want this method to invoked, when "edit" button is clicked, not when the column header is clicked. 我希望在单击“编辑”按钮时而不是在单击列标题时调用此方法。 Please suggest some solution. 请提出一些解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM