We have a listbox defined in a slickgrid column. We have the problem that to select an option from the listbox, we first have to select the cell, and then the option can be selected from the list box. Thus the user requires two clicks instead of one. What do we have to do so that the user can with one click open the list box to select the required value.
We use the following cell formatter and editor to display the list box:
FORMATTER:
function SelectCellFormatter(row, cell, value, columnDef, dataContext) {
opt_values = columnDef.options.split(',');
option_str = "";
for( i in opt_values ){
v = opt_values[i];
if(v == value)
{
option_str += "<OPTION value='"+v+"' selected>"+v+"</OPTION>";
}
else
{
option_str += "<OPTION value='"+v+"'>"+v+"</OPTION>";
}
}
return "<SELECT tabIndex='0' class='editor-select'>"+ option_str +"</SELECT>"
}
EDITOR:
SelectCellEditor : function(args) {
var $select;
var defaultValue;
var scope = this;
this.init = function() {
if(args.column.options){
opt_values = args.column.options.split(',')
} else {
opt_values ="yes,no".split(',');
}
option_str = ""
for( i in opt_values ){
v = opt_values[i];
option_str += "<OPTION value='"+v+"'>"+v+"</OPTION>";
}
$select = $("<SELECT tabIndex='0' class='editor-select'>"+ option_str +"</SELECT>");
$select.appendTo(args.container);
$select.focus();
};
this.destroy = function() {
$select.remove();
};
this.focus = function() {
$select.focus();
};
this.loadValue = function(item) {
defaultValue = item[args.column.field];
$select.val(defaultValue);
};
this.serializeValue = function() {
if(args.column.options){
return $select.val();
} else {
return ($select.val() == "yes");
}
};
this.applyValue = function(item,state) {
item[args.column.field] = state;
};
this.isValueChanged = function() {
return ($select.val() != defaultValue);
};
this.validate = function() {
return {
valid: true,
msg: null
};
};
this.init();
}