2014-10-17 117 views
0

我想向SpreadJS中的列添加自动完成功能,这样当用户键入单元格时,下拉列表将显示与从服务器检索到的匹配项目。该SpreadJS文档指出:使用SpreadJS自动完成

SpreadJS支持组合框,复选框,按钮,文本,超链接, Wijmo编辑细胞(AutoCompleteTextBox)和自定义类型的细胞。

http://helpcentral.componentone.com/NetHelp/SpreadHClientUG/celltypes.html

AutoCompleteTextBox听起来像它可能做的伎俩;但是,我找不到任何文件或样本来证明如何实现。我可以创建一个自定义单元格类型,但是如果我可以利用已经存在的功能,它会好得多!

有谁知道我该如何实现这个?

感谢, 斯科蒂

回答

1

我也有同样的问题,但我调整了TextCellType使用jQueryUI自动完成。

<div> 
    <input type='hidden' id="dropDownCellInfo" /> 
    <div id="ss" style="height:500px;border:solid gray 1px;"/> 
    </div> 

您可以参考jQuery UI documentation以了解有关使用自动填充小部件的更多信息。下面的代码创建一个TextCellType并覆盖其创建编辑器方法以创建一个文本元素并使用jQuery自动完成对其进行初始化。

我不得不使用隐藏文本元素来捕获行和列,在从列表中选择一个选项后,必须更新该值。可能有更好的办法,但这有效。

var cellType = new $.wijmo.wijspread.TextCellType(); 
    cellType.createEditorElement = function(context) 
    { 
    var obj = jQuery('#dropDownCellInfo'); 
    obj.data('sheet' , context.sheet); 
    obj.data('row', context.row); 
    obj.data('col', context.col); 

    console.log(context); 
    var $textarea = $('<input type="text" id="txtSearch" style="visible:false" />'); 
    var editor = document.createElement("div"); 
    $(editor).append($textarea); 
    $textarea.autocomplete({ 
     minLength: 2, 
     autoFocus : true, 
     source: 'http://localhost/spreadjs/index.php', 
     focus: function(event, ui) { 
     $("#txtSearch").val(ui.item.inst_name); 
     return false; 
     }, 
     select: function(event, ui) { 
     $("#txtSearch").val(ui.item.inst_name);    
     var obj = jQuery('#dropDownCellInfo'); 
     var spd = jQuery("#ss").wijspread("spread").getActiveSheet().setActiveCell(obj.data('row'),obj.data('col')); 

     // We have to explicitly remove the list element from the DOM because some 
     // how the method creates a new list for each autocomplete request and does not deletes the list after onSelect event. 
     jQuery('ul.ui-autocomplete').remove(); 
     return false; 
     } 
    }) 
    .autocomplete("instance")._renderItem = function(ul, item) { 
     return $("<li>") 
     .append("<a>" + item.inst_name + "</a>") 
     .appendTo(ul); 
    }; 
    return editor 
    }; 
    sheet.getColumn(3).cellType(cellType);