无法在slickgrid中使用select下拉插件

时间:2015-12-23 08:14:43

标签: select drop-down-menu slickgrid

我要求在slickgrid中的标题中添加一个下拉列表。正常选择下拉列表工作正常。我需要使用下拉插件来提供正确的外观和感觉。每当我这样做时,下拉部分就会隐藏在标题中。

感谢任何帮助。

jsfiddle的链接在这里:

{{3}}

<div id="myGrid" style="width: 600px; height: 500px;"></div>


var grid;
var dataView;

var buttonFormat = function (row, cell, value, columnDef, dataContext) {
    return "";    
}

var columns = [
    {id: "title", name: "Title", field: "title"},
    {id: "term", name: "Term", field: "term"},
    {id: "%", name: "% Complete", field: "percentComplete"},
    {id: "detail", name: "Detail", field: "detail",formatter: buttonFormat}
];

var options = {
          rowHeight: 25,
          editable: false,
          enableAddRow: false,
          enableCellNavigation: true,
          forceFitColumns: false,
      headerrowheight:100,
          multiColumnSort: true,
          showHeaderRow: true,
      explicitInitialization: true
        };

$(function () {
    var data = [];
    for (var i = 0; i < 30; i++) {
        var days = Math.round(Math.random() * 10);
        data[i] = {
            id: i ,
            title: "Task " + i,
            term: days + " days",
            percentComplete: Math.round(Math.random() * 100),
            description: "We are working hard " + days + " days!",
            detail: null,
            toString:function(){
                var str = "";
                str += "Title:" + this.title + "<br/>";
                str += "Term:" + this.term + "<br/>";
                str += "Comp:" + this.percentComplete + "<br/>";
                str += "<b>" + this.description + "</b>";
                return str;
            }
        };
    }

    dataView = new Slick.Data.DataView();    
    dataView.beginUpdate();
    dataView.setItems(data);
    dataView.endUpdate();

    grid = new Slick.Grid("#myGrid", dataView, columns, options);

    grid.onClick.subscribe(function (e, args) {
        if ($(e.target).hasClass("btn")) {
            var item = dataView.getItem(args.row);
            openDialog(item);
        }
        e.stopImmediatePropagation();
    });

    grid.onHeaderRowCellRendered.subscribe(function(e, args) {
                $(args.node).empty();
                    if(args.column.id=="detail"){

                    $("<select style='width: 70px;'><option value=''>All</option><option value='Activation'>Activation</option><option value='Deactivation'>Deactivation</option><option value='Upload'>Upload</option></select>")
                   .data("columnId", args.column.id)
                   .val("")
                   .appendTo(args.node); 
             }


            });
      grid.init();

});

var openDialog = function(row){
    var dom = "<div>" + row.toString() + "</div>";
    $(dom).dialog();
};
$('select').each(function () {

    // Cache the number of options
    var $this = $(this),
        numberOfOptions = $(this).children('option').length;

    // Hides the select element
    $this.addClass('s-hidden');

    // Wrap the select element in a div
    $this.wrap('<div class="select"></div>');

    // Insert a styled div to sit over the top of the hidden select element
    $this.after('<div class="styledSelect"></div>');

    // Cache the styled div
    var $styledSelect = $this.next('div.styledSelect');

    // Show the first select option in the styled div
    $styledSelect.text($this.children('option').eq(0).text());

    // Insert an unordered list after the styled div and also cache the list
    var $list = $('<ul />', {
        'class': 'options'
    }).insertAfter($styledSelect);

    // Insert a list item into the unordered list for each select option
    for (var i = 0; i < numberOfOptions; i++) {
        $('<li />', {
            text: $this.children('option').eq(i).text(),
            rel: $this.children('option').eq(i).val()
        }).appendTo($list);
    }

    // Cache the list items
    var $listItems = $list.children('li');

    // Show the unordered list when the styled div is clicked (also hides it if the div is clicked again)
    $styledSelect.click(function (e) {
        e.stopPropagation();
        $('div.styledSelect.active').each(function () {
            $(this).removeClass('active').next('ul.options').hide();
        });
        $(this).toggleClass('active').next('ul.options').toggle();
    });

    // Hides the unordered list when a list item is clicked and updates the styled div to show the selected list item
    // Updates the select element to have the value of the equivalent option
    $listItems.click(function (e) {
        e.stopPropagation();
        $styledSelect.text($(this).text()).removeClass('active');
        $this.val($(this).attr('rel'));
        $list.hide();
        /* alert($this.val()); Uncomment this for demonstration! */
    });

    // Hides the unordered list when clicking outside of it
    $(document).click(function () {
        $styledSelect.removeClass('active');
        $list.hide();
    });

});


body{
    font-size:10px;    
}
.s-hidden {
    visibility:hidden;
    padding-right:10px;
}
.select {
    cursor:pointer;
    display:inline-block;
    position:relative;
    font:normal 11px/22px Arial, Sans-Serif;
    color:black;
    border:1px solid #ccc;
}
.styledSelect {
    position:absolute;
    top:0;
    right:0;
    bottom:0;
    left:0;
    background-color:white;
    padding:0 10px;
    font-weight:bold;
}
.styledSelect:after {
    content:"";
    width:0;
    height:0;
    border:5px solid transparent;
    border-color:black transparent transparent transparent;
    position:absolute;
    top:9px;
    right:6px;
}
.styledSelect:active, .styledSelect.active {
    background-color:#eee;
}
.options {
    display:none;
    position:absolute;
    top:100%;
    right:0;
    left:0;
    z-index:999;
    margin:0 0;
    padding:0 0;
    list-style:none;
    border:1px solid #ccc;
    background-color:white;
    -webkit-box-shadow:0 1px 2px rgba(0, 0, 0, 0.2);
    -moz-box-shadow:0 1px 2px rgba(0, 0, 0, 0.2);
    box-shadow:0 1px 2px rgba(0, 0, 0, 0.2);
}
.options li {
    padding:0 6px;
    margin:0 0;
    padding:0 10px;
}
.options li:hover {
    background-color:#39f;
    color:white;
}
.slick-headerrow-columns {
    height: 100px !important;
}

1 个答案:

答案 0 :(得分:1)

导致问题的故障是为标题行设置的SlickGrids默认样式及其列定义,即:

  

溢出:hidden;

将标题行和列CSS样式更改为:

.slick-headerrow  {
    overflow: unset !important;
}

.slick-headerrow-column, .slick-headerrow-columns {
    overflow: unset;
}

并将网格视口z-index设置为-1会导致您的下拉列表按预期显示。

我已用它更新了您最新的JSFiddle,您现在只需要处理行高和选择元素的宽度。