DataTable with dropdown Column

后端 未结 4 1168
不思量自难忘°
不思量自难忘° 2020-12-06 07:00

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

And I want it like the below image

and the code which I use is

相关标签:
4条回答
  • 2020-12-06 07:34

    Another way would be to use the render method:

            "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.

    0 讨论(0)
  • 2020-12-06 07:35

    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

    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:

    //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.

    0 讨论(0)
  • 2020-12-06 07:35

    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");
                                }
    
    0 讨论(0)
  • 2020-12-06 08:00

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

    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.

    0 讨论(0)
提交回复
热议问题