SlickGrid-Cell delete & insert

shift cells on add/delete cell

by Anil D

HTML

<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/lib/jquery.event.drag-2.2.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/slick.core.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/slick.grid.js"></script>
<link rel="stylesheet" href="https://rawgithub.com/Celebio/SlickGrid/master/slick.grid.css">
<link rel="stylesheet" href="https://rawgithub.com/Celebio/SlickGrid/master/slick-default-theme.css">
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/slick.formatters.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/slick.editors.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/plugins/slick.cellselectionmodel.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/plugins/slick.cellrangedecorator.js"></script>
<script src="https://rawgithub.com/Celebio/SlickGrid/master/plugins/slick.cellrangeselector.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<div id="myGrid" style="width:auto;height:200px;"></div>
<button type="button" id="addCell">Add</button>
<button type="button" id="delCell">Delete</button>
<div id="result"></div>

CSS

.editor-text{
    width : 100px;    
    text-overflow: ellipsis;
}
.dropdown{
    width: 75px;
    height: 22px;
    padding: 0px;
    z-index: 20;
    width: 100px;
    padding: 2px;
    font-size:11px;
    line-height: 1;
    height: 26px;
    margin-top: -10px;
    margin-right: 319px;
    margin-left: -6px;
    margin-bottom: -9px;

}
.ui-autocomplete {
		max-height: 100px;
		overflow-y: auto;
		/* prevent horizontal scrollbar */
		overflow-x: hidden;
        font-size : 14px;
        width : 110px;
        height : 50px;        
}

input.editor-text{
    background: transparent !important;
    border: transparent !important;
}

.customSelected {
    z-index: 10;
    background: #DFE8F6 !important;
}
.ng-invalid {
	border: 1px solid red;
}

body {
	font-family: Arial, Helvetica, sans-serif;
}

body,td,th {
	font-size: 14px;
	margin: 0;
}

table {
	border-collapse: separate;
	border-spacing: 2px;
	display: table;
	margin-bottom: 0;
	margin-top: 0;
	-moz-box-sizing: border-box;
	text-indent: 0;
}

.error {
	color: red;
}

.ui-helper-hidden {
	display: none;
}

JavaScript

function requiredFieldValidator(value) {
    if (value == null || value == undefined || !value.length) {
      return {valid: false, msg: "This is a required field"};
    } else {
      return {valid: true, msg: null};
    }
  }

  var grid;
  var data = [];
  var columns = [
    {id: "title", name: "Title", field: "title", width: 80, cssClass: "cell-title", editor: Slick.Editors.Text, validator: requiredFieldValidator},
    {id: "desc", name: "Description", field: "g0-description", width: 100, editor: Slick.Editors.LongText},
    {id: "duration", name: "Duration", field: "g0-duration", editor: Slick.Editors.Text},
    {id: "desc1", name: "Description", field: "g1-description", width: 100, editor: Slick.Editors.LongText},
    {id: "duration1", name: "Duration", field: "g1-duration", editor: Slick.Editors.Text},
    {id: "desc2", name: "Description", field: "g2-description", width: 100, editor: Slick.Editors.LongText},
    {id: "duration3", name: "Duration", field: "g2-duration", editor: Slick.Editors.Text},
    {id: "finish", name: "Finish", field: "finish", minWidth: 60, editor: Slick.Editors.Date},
  ];
  var options = {
    editable: true,
    enableAddRow: true,
    enableCellNavigation: true,
    asyncEditorLoading: false,
    autoEdit: false
  };

  $(function () {
    for (var i = 0; i < 5; i++) {
      var d = (data[i] = {});

      d["title"] = "Task " + i;
      d["g0-description"] = "G1 Desc";
      d["g0-duration"] = "10 days";
      d["g1-description"] = "G2 Desc";
      d["g1-duration"] = "20 days";
      d["g2-description"] = "G3 Desc";
      d["g2-duration"] = "30 days";
      d["finish"] = "01/05/2009";
    }

    grid = new Slick.Grid("#myGrid", data, columns, options);

    grid.setSelectionModel(new Slick.CellSelectionModel());

    grid.onAddNewRow.subscribe(function (e, args) {
      var item = args.item;
      grid.invalidateRow(data.length);
      data.push(item);
      grid.updateRowCount();
      grid.render();
    });
    
   ...