jQuery UI Multicolumn Autocomplete
Trying to make the autocomplete plugin work with multiple columns.
by salt
HTML
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
<div style="margin:10px 10px;padding:8px;width:400px;" class="ui-widget ui-widget-content ui-corner-all">
<div style="margin:0 0 14px 0; font-size:.7em;">An example of a 3 column autocomplete control.</div>
<div style="margin:0 0 4px 4px;">Find a city</div>
<!-- This is the input control that gets turned into the jquery ui widget -->
<input id="search" type="text" style="padding:2px;font-size:.8em;width:300px;" />
<div style="font-size:0.7em;color:#999;">Makes use of the web service at <a href="http://geonames.org">geonames.org</a>
</div>
<div style="margin:20px 0 0 0;"> <span id="results" style="color:#68a;"></span>
</div>
</div>
CSS
.ui-autocomplete-loading {
background: white url('http://jqueryui.com/demos/autocomplete/images/ui-anim_basic_16x16.gif') right center no-repeat;
}
JavaScript
/*
* jQuery UI Multicolumn Autocomplete Widget Plugin 2.1
* Copyright (c) 2012-2014 Mark Harmon
*
* Depends:
* - jQuery UI Autocomplete widget
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
$.widget('custom.mcautocomplete', $.ui.autocomplete, {
_create: function () {
this._super();
this.widget().menu("option", "items", "> :not(.ui-widget-header)");
},
_renderMenu: function (ul, items) {
var self = this,
thead;
if (this.options.showHeader) {
table = $('<div class="ui-widget-header" style="width:100%"></div>');
$.each(this.options.columns, function (index, item) {
table.append('<span style="padding:0 4px;float:left;width:' + item.width + ';">' + item.name + '</span>');
});
table.append('<div style="clear: both;"></div>');
ul.append(table);
}
$.each(items, function (index, item) {
self._renderItem(ul, item);
});
},
_renderItem: function (ul, item) {
var t = '',
result = '';
$.each(this.options.columns, function (index, column) {
t += '<span style="padding:0 4px;float:left;width:' + column.width + ';">' + item[column.valueField ? column.valueField : index] + '</span>'
});
result = $('<li></li>')
.data('ui-autocomplete-item', item)
.append('<a class="mcacAnchor">' + t + '<div style="clear: both;"></div></a>')
.appendTo(ul);
return result;
}
});
// Sets up the multicolumn autocomplete widget.
$("#search").mcautocomplete({
// These next two options are what this plugin adds to the autocomplete widget.
showHeader: true,
columns: [{
name: 'City',
width: '150px',
valueField: 'name'
}, {
name: 'State',
width: '120px',
...