SlickGrid example 2: 按想要的风格展示列

可编辑单元支持一列展示多个属性域,可以为编辑单元提供验证,并且自定义验证事件。
SlickGrid example 2: 按想要的风格展示列_第1张图片

代码:
SlickGrid example 3a:Advanced Editing
href="../css/smoothness/jquery-ui-1.8.16.custom.css"type="text/css" />

Demonstrates:

  • compound cell editorsdriving multiple fields from onecell
  • providing validation fromthe editor
  • hooking into validationevents


  • var grid;//定义表格
    //定义表格数据变量
    var data = [];
    //设置列显示样式
    var columns = [ {
    id : "title",
    name : "Title",
    field : "title",
    width : 120,
    cssClass : "cell-title",
    editor : Slick.Editors.Text
    }, {//可管理多个属性域
    id : "range",
    name : "Range",
    width : 120,
    formatter : NumericRangeFormatter, //自定义显示风格格式化方法
    editor : NumericRangeEditor //编辑方法
    } ];
    //设置表格参数
    var options = {
    editable : true,
    enableAddRow : false,
    enableCellNavigation : true,
    asyncEditorLoading : false
    };
    //一列中显示多个属性域
    function NumericRangeFormatter(row, cell, value, columnDef,dataContext) {
    return dataContext.from + " - " + dataContext.to;
    }
    //编辑处理
    function NumericRangeEditor(args) {
    var $from, $to;
    var scope = this;

    this.init = function() {
    $from = $("").appendTo(
    args.container).bind("keydown", scope.handleKeyDown);

    $(args.container).append("  to ");

    $to = $("").appendTo(
    args.container).bind("keydown", scope.handleKeyDown);

    scope.focus();
    };

    this.handleKeyDown = function(e) {
    if (e.keyCode == $.ui.keyCode.LEFT
    || e.keyCode == $.ui.keyCode.RIGHT
    || e.keyCode == $.ui.keyCode.TAB) {
    e.stopImmediatePropagation();
    }
    };

    this.destroy = function() {
    $(args.container).empty();
    };

    this.focus = function() {
    $from.focus();
    };

    this.serializeValue = function() {
    return {
    from : parseInt($from.val(), 10),
    to : parseInt($to.val(), 10)
    };
    };

    this.applyValue = function(item, state) {
    item.from = state.from;
    item.to = state.to;
    };

    this.loadValue = function(item) {
    $from.val(item.from);
    $to.val(item.to);
    };

    this.isValueChanged = function() {
    return args.item.from != parseInt($from.val(), 10)
    || args.item.to != parseInt($from.val(), 10);
    };
    //可定义验证编辑单元数据
    this.validate = function() {
    if (isNaN(parseInt($from.val(), 10))
    || isNaN(parseInt($to.val(), 10))) {
    return {
    valid : false,
    msg : "Please type in valid numbers."
    };
    }

    if (parseInt($from.val(), 10) >parseInt($to.val(), 10)) {
    return {
    valid : false,
    msg : "'from' cannot be greater than 'to'"
    };
    }

    return {
    valid : true,
    msg : null
    };
    };

    this.init();
    }

    $(function() {// 生成表格数据
    for ( var i = 0; i < 100; i++) {
    var d = (data[i] = {});

    d["title"] = "Task " + i;
    d["from"] = Math.round(Math.random() * 100);
    d["to"] = d["from"] + Math.round(Math.random() * 100);
    }
    //渲染表格
    grid = new Slick.Grid("#myGrid", data, columns,options);

    grid.onValidationError.subscribe(function(e, args) {
    alert(args.validationResults.msg);
    });
    })

    你可能感兴趣的:(SlickGrid)