Javascript 带有下拉列的数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36031483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:31:31  来源:igfitidea点击:

DataTable with dropdown Column

javascriptjqueryhtmltwitter-bootstrapdatatables

提问by raghav

I need to make dropdown as column in DataTable jQuery it is lookinng like as below right now Datatable now

我需要将下拉列表作为 DataTable jQuery 中的列,它现在看起来像下面这样 现在数据表

And I want it like the below image enter image description here

我想要它像下图 在此处输入图片说明

and the code which I use is

我使用的代码是

<table id="example" class="hover row-border dataTable" role="grid" width="100%">
    <thead class="dataTableHeader">
        <tr>
            <th>Days</th>
            <th>Start Time</th>
            <th>End Time</th>
        </tr>
    </thead>
</table>

$(document).ready(function () {
    $('#example').DataTable({
        "aaData": OrganizationData.OrganizationPreference.ScheduleDaysCol,
        "columns": [
            {"data": "DayName"},
            {"data": "StartDateHour"},
            {"data": "EndDateHour"}

        ],
        "paging": false,
        "ordering": false,
        "info": false,
        "filter": false
    });
});

回答by annoyingmouse

Another way would be to use the rendermethod:

另一种方法是使用该render方法:

        "render": function(d,t,r){
            var $select = $("<select></select>", {
                "id": r[0]+"start",
                "value": d
            });
            $.each(times, function(k,v){
                var $option = $("<option></option>", {
                    "text": v,
                    "value": v
                });
                if(d === v){
                    $option.attr("selected", "selected")
                }
                $select.append($option);
            });
            return $select.prop("outerHTML");
        }

Working example.

工作示例

回答by DogPawHat

DataTables seem to have an editor for this type of thing as refrenced by Samyukta and others: https://editor.datatables.net/examples/inline-editing/simple

数据表似乎有一个由 Samyukta 和其他人引用的这种类型的编辑器:https://editor.datatables.net/examples/inline-editing/simple

I would say that is the easiest answer. It is however, a commercial extension with a free trial only.

我会说这是最简单的答案。然而,它只是一个免费试用的商业扩展。

If you wanted some jquery code to simply change the static times to dropdown boxes, you could give this a shot:

如果您想要一些 jquery 代码来简单地将静态时间更改为下拉框,您可以试一试:

//utility functions to get half-hour increment lists
function getTimeList(){
    var iterations = 48;
    var result = [];
    for(int i = 0; i < iterations; i++){
        var hour = Math.floor(i / 2);
        var minute = (i % 2) > 0 ? '30' : '00';
        result.push(hour + ':' + minute);
    }
    return result;
}

function getOptionTimeList(){
    var raw = getTimeList();
    var iterations = raw.length;
    var result = '';
    for(int i = 0; i < iterations; i++){
        result = result + '<option>' + raw[i] + '</option>';
    }
    return result;
}


//I'm using the not selector to avoid changing the days into dropdown by accident
$('#example tbody tr td:not(#example tbody tr:first-child)').each(
    function(index, element){
        var value = element.innerHTML;
        var optionList = getOptionTimeList();
        var replacement = '<td><select>' + optionList + '</select></td>';


        $(element).replaceWith(replacement)
    }
);

This should get you the drop down boxes where you need them. I'll revise this if you have problems with it.

这应该会为您提供需要它们的下拉框。如果你有问题,我会修改它。

回答by Tanmay

You can use this way then for the dropdown setting

您可以使用这种方式进行下拉设置

 "aaData": OrganizationData.OrganizationPreference.ScheduleDaysCol,
                        "columnDefs": [{ "targets": 0,"data": "DayName" },
                            {
                            "targets": 1,
                            "data": "StartDateTime",
                           "render": function (data, type, full, meta) {
                                    var $select = $("<select></select>", {
                                    });
                                    $.each(times, function (k, v) {

                                        var $option = $("<option></option>", {
                                            "text": v,
                                            "value": v
                                        });
                                        if (data === v) {
                                            $option.attr("selected", "selected")
                                        }
                                        $select.append($option);
                                    });
                                    return $select.prop("outerHTML");
                            }

回答by fudu

You can try to use this, this is what i'm using now.

您可以尝试使用它,这就是我现在正在使用的。

https://github.com/ejbeaty/CellEdit

https://github.com/ejbeaty/CellEdit

Look at this example:

看这个例子:

"inputTypes": [
            {
                "column":0, 
                "type":"text", 
                "options":null 
            }, 
            {
                "column":1, 
                "type": "list",
                "options":[
                    { "value": "1", "display": "Beaty" },
                    { "value": "2", "display": "Doe" },
                    { "value": "3", "display": "Dirt" }
                ]
            }

Hope it help someone.

希望它可以帮助某人。