Editing experience

by damyanpetev

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.1/jquery.mockjax.min.js"></script>
<script src="http://igniteui.com/js/modernizr.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
<script src="http://cdn-na.infragistics.com/jquery/20132/latest/js/infragistics.core.js"></script>
<script src="http://cdn-na.infragistics.com/jquery/20132/latest/js/infragistics.lob.js"></script>
<script src="http://igniteui.com/data-files/adventureworks.min.js"></script>
<link href="http://cdn-na.infragistics.com/jquery/20132/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet"></link>
<link href="http://cdn-na.infragistics.com/jquery/20132/latest/css/structure/infragistics.css" rel="stylesheet"></link>

<table id="grid"></table>

<button id="save">SAVE</button>
<div id="log" style="overflow:auto; height:200px; background-color:#f2f2f2; padding:5px"></div>

JavaScript

$(function () {
            $("#grid").igGrid({
                autoGenerateColumns: false,
                width: "100%",
                primaryKey: "ProductID",
                updateUrl: '/update/product',
                columns: [
                    { headerText: "Product ID", key: "ProductID", dataType: "number", width: "15%" },
                    { headerText: "Product Name", key: "Name", dataType: "string", width: "40%" },
                    { headerText: "Product Number", key: "ProductNumber", dataType: "string", width: "30%" },
                    { headerText: "Make Flag", key: "MakeFlag", dataType: "bool", width: "15%" }
                ],
                dataSource: adventureWorks,
                features: [
                    {
                        name: 'Paging',
                        type: "local",
                        pageSize: 10
                    },
                    {
                        name: 'Updating',
                        editMode: "row",
                        dataDirty:function (event, ui) {
                           // this is required to allow paging to work without auto-comming data source
                            return false;
                        }
                    }
                ]
            });  
    // Mock update endpoint
$.mockjax({
    url: "/update/product",
    contentType: 'text/json',
    response: function (settings) {
        var transactions = JSON.parse(settings.data.ig_transactions);
        for (i = 0; i < transactions.length; i++) {
            $('#log').prepend("<p> Update operation: '" + transactions[i].type + "'  on row with ID: " + transactions[i].rowId + "</p> ");
        }
        this.responseText = JSON.stringify({Success: true});
        //mock doesn't set the right header, jquery fails to parse...
        this.headers['Content-Type'] = "text/json";
    }
});
});

$(document).on('click', "#save", function () {
    $("#grid").igGrid("saveChanges");
});