【问题标题】:How to have a dropdown in each of Datatable row?如何在每个 Datatable 行中有一个下拉列表?
【发布时间】:2020-06-13 02:36:30
【问题描述】:

目前我成功地在数据表之外出现了一个下拉结果,但是如何将下拉列表移动到每个数据表行中?在我的情况下,在服务目录列中。我到处寻找,但没有达到我想要的。当前不应该出现外部数据表的下拉菜单。

HTML

<table id="bindNewServiceTable">
    <thead>
        <tr>
            <th class="text-center">Admin Status</th>
            <th class="text-center">Operate Status</th>
            <th class="text-center">Service Catalogue</th>
            <th class="text-center">Action</th>
        </tr>
    </thead>
</table>

<select id="dropdown">
    <option></option>
</select>

JS

$('#bindNewServiceTable').DataTable({
    ajax: {
        url: url_bind,
        crossDomain : true,
        type : "POST",
        cache : false,
        dataType : "json",
        contentType: "application/json",
        dataSrc : "service"
    },
    columns: [
        { data : "admin_status" },
        { data : "operate_status" },
        { data : "id", "className": "text-center",
            render: function(data){
                return createSelect(data);
            }
        },
        { data : "example" },
    ],
});

function createSelect(id){

    $.ajax ({
        url: url_list_cat,
        type : 'POST',
        dataType : 'json',
        data: id,
        cache: false,
        contentType: "application/json",
        processData: true,
        timeout: 10000,
        success: function(response){
            for (var i = 0; i < response.category.length; i++) {
                $("#dropdown").append($("<option>", {
                    response: response.category[i].name,
                    text: response.category[i].name
                }));
            }
        }
    });
}

【问题讨论】:

    标签: javascript html jquery datatables


    【解决方案1】:

    您需要在数据表字段中返回带有所需 HTML 的实际字符串。

    因此,与其尝试用id="dropdown" 填充div,不如这样做:

    
    function createSelect(id){
        $.ajax ({
            url: url_list_cat,
            type : 'POST',
            dataType : 'json',
            data: id,
            cache: false,
            contentType: "application/json",
            processData: true,
            timeout: 10000,
            success: function(response){
                var select = "<select id='dropdown'>";
                for (var i = 0; i < response.category.length; i++) {
                    var val = response.category[i].name
                    select += "<option value='" + val + "'>" + response.category[i].name + "</option>";
                }
                select += "</select>"
                return select;
            }
        });
    }
    

    【讨论】:

    • 我已经在函数中应用了这段代码,但下拉菜单仍然没有出现。
    【解决方案2】:

    你可能想试试这个并调整它以满足你的需要

    $('#bindNewServiceTable').DataTable({
        ajax: {
            url: url_bind,
            crossDomain : true,
            type : "POST",
            cache : false,
            dataType : "json",
            contentType: "application/json",
            dataSrc : "service"
        },
        columns: [
            { data : "admin_status" },
            { data : "operate_status" },
            { data : "id", "className": "text-center",
                render: function(data){
                    return createSelect(data);
                }
            },
            { data : "example" },
        ],
    });
    
    function createSelect(id){
    
       return  $.ajax ({
            url: url_list_cat,
            type : 'POST',
            dataType : 'json',
            data: id,
            cache: false,
            contentType: "application/json",
            processData: true,
            timeout: 10000,
            success: function(response){
            var $select = $("<select></select>", {
                            "id": id,
                           "value": value  //set you select value from response
                        });
                $.each(response, function(key,value){
                            var $option = $("<option></option>", {
                                "text": value.name,
                                "value": value.name
                            });
                            if(value === value.name){.  //compare the value from select with option selected
                                $option.attr("selected", "selected")
                            }
                            $select.append($option);
                        });
                        return $select.prop("outerHTML");
            }
        });
    }
    

    【讨论】:

    • 我尝试过但没有成功,注释//set you select value from response的值我已经替换为response.category.name
    • 您的表格中是否填充了下拉菜单?
    • 你的桌子现在是什么样子的?
    • 是的,我想在服务目录列的数据表中的每一行填充一个下拉菜单,每一行都有自己的 id 并指定相同的 API,当用户选择下拉菜单时,数据将读取来自 API。但当前的问题该列是空白的。
    • 与早期发布的图片相同
    猜你喜欢
    • 2022-10-06
    • 2021-11-14
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多