Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting only modified or new Kendo UI Grid rows to send to the server

Although I have tried several times, I could not solve trying many method. Shortly this is the thing what I want to do : only get modified or new rows as an object or JSON string.

like image 716
esquare Avatar asked Nov 25 '14 07:11

esquare


1 Answers

You should use set for changing the content of a record. Then, getting the records modified is just iterating through datasource.data() array and checking which items have dirty set to true.

var data = grid.dataSource.data();
var dirty = $.grep(data, function(item) {
    return item.dirty
});
// Dirty array contains those elements modified
console.log("dirty", dirty);

The following snippet shows both that changing the data programmatically or via Grid built-in incell edition is compatible with this approach.

$(document).ready(function() {
  var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service",
      dataSource = new kendo.data.DataSource({
        transport: {
          read:  {
            url: crudServiceBaseUrl + "/Products",
            dataType: "jsonp"
          },
          update: {
            url: crudServiceBaseUrl + "/Products/Update",
            dataType: "jsonp"
          },
          destroy: {
            url: crudServiceBaseUrl + "/Products/Destroy",
            dataType: "jsonp"
          },
          create: {
            url: crudServiceBaseUrl + "/Products/Create",
            dataType: "jsonp"
          },
          parameterMap: function(options, operation) {
            if (operation !== "read" && options.models) {
              return {models: kendo.stringify(options.models)};
            }
          }
        },
        batch: true,
        pageSize: 20,
        schema: {
          model: {
            id: "ProductID",
            fields: {
              ProductID: { editable: false, nullable: true },
              ProductName: { validation: { required: true } },
              Discontinued: { type: "boolean" }
            }
          }
        }
      });

  var grid = $("#grid").kendoGrid({
    dataSource: dataSource,
    navigatable: true,
    pageable: true,
    height: 300,
    columns: [
      "ProductName",
      { field: "Discontinued", width: 120 }
    ],
    editable: "incell",
    selectable: true
  }).data("kendoGrid");

  $("#change").on("click", function() {
    var sel = grid.select();
    if (sel.length) {
      var item = grid.dataItem(sel);
      item.set("Discontinued", true);
    }
  });

  $("#show").on("click", function() {
    var data = grid.dataSource.data();
    var dirty = $.grep(data, function(item) {
        return item.dirty
    });
    $("#logger").html(JSON.stringify(dirty, null, 2));
  });
});
#logger {
  min-height: 60px;
  border: 1px solid black;
  overflow-x: scroll
}

#grid {
  width: 500px;
}
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.default.min.css" />
<script src="http://cdn.kendostatic.com/2014.3.1119/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1119/js/kendo.all.min.js"></script>

<div id="example">
  <div>
    This button changes the Discontinued field to "true" for the selected row: 
    <button id="change" class="k-button">Change selected</button>
  </div>
  <div>
    Click for displaying modified rows: 
    <button id="show" class="k-button">Show dirty</button>
    <pre id="logger"></pre>
  </div>
  <div id="grid"></div>
</div>
like image 148
OnaBai Avatar answered Sep 24 '22 22:09

OnaBai