You are here

SlickGrid example 3a: Advanced Editing in Slickgrid 6

Demonstrates:

  • compound cell editors driving multiple fields from one cell
  • providing validation from the editor
  • hooking into validation events

File

js/slickgrid/examples/example3a-compound-editors.html
View source
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>SlickGrid example 3a: Advanced Editing</title>
		<link rel="stylesheet" href="../slick.grid.css" type="text/css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="../css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
		<link rel="stylesheet" href="examples.css" type="text/css" media="screen" charset="utf-8" />
		<style>
		.cell-title {
			font-weight: bold;
		}
	</style>
	</head>
	<body>


		<div style="width:600px;float:left;">
			<div id="myGrid" style="width:100%;height:500px;"></div>
		</div>

		<div class="options-panel" style="width:320px;margin-left:650px;">
            <h2>Demonstrates:</h2>
            <ul>
                <li>compound cell editors driving multiple fields from one cell</li>
                <li>providing validation from the editor</li>
                <li>hooking into validation events</li>
            </ul>
		</div>

		<script src="../lib/firebugx.js"></script>

		<script src="../lib/jquery-1.4.3.min.js"></script>
		<script src="../lib/jquery-ui-1.8.5.custom.min.js"></script>
		<script src="../lib/jquery.event.drag-2.0.min.js"></script>

		<script src="../slick.core.js"></script>
		<script src="../slick.editors.js"></script>
		<script src="../slick.grid.js"></script>

		<script>
		var grid;
		var data = [];

		var columns = [
			{id:"title", name:"Title", field:"title", width:120, cssClass:"cell-title", editor:TextCellEditor},
            {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 = $("<INPUT type=text style='width:40px' />")
                            .appendTo(args.container)
                            .bind("keydown", scope.handleKeyDown);

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

                $to = $("<INPUT type=text style='width:40px' />")
                            .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<500; 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);
            });
		})
		</script>
	</body>
</html>