Search code examples
slickgrid

SlickGrid dropdown box editor not working


I have am trying to implement something along the lines of Slickgrid, column with a drop down select list?

my code is; slick.editors.js ;

(function ($) {
  // register namespace
  $.extend(true, window, {
    "Slick": {
      "Editors": {
        "Text": TextEditor,
        "Integer": IntegerEditor,
        "Date": DateEditor,
        "YesNoSelect": YesNoSelectEditor,
        "Checkbox": CheckboxEditor,
        "PercentComplete": PercentCompleteEditor,
        "LongText": LongTextEditor,
        "SelectOption": SelectCellEditor
      }
    }
  });  

with the function defined futher down,

function SelectCellEditor(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();
}

Then in my CSHTML

var columns = [

  { id: "color", name: "Color", field: "color", options: "Red,Green,Blue,Black,White", editor: Slick.Editors.SelectOption },
  { id: "lock", name: "Lock", field: "lock", options: "Locked,Unlocked", editor: Slick.Editors.SelectOption },
  ];


    var options = {
        enableCellNavigation: true,
        enableColumnReorder: false
    };

    $(function () {
        var data = [];
        for (var i = 0; i < 20; i++) {
           data[i] = {
           color: "Red",
           lock: "Locked"
            };
   }

the grid shows and the colour is shown as if its a regular text in a cell, but no dropdown?.


Solution

  • The drop-down will appear only when you are editing that cell. Adding editable: true to your grid options should work I think.