I am using SlickGrid to generate a data grid. The data is fetched by a jQuery AJAX call. With the advice from https://stackoverflow.com/a/11611477/2294855 I am able to handle the response data and set it to the DataView. But my problem arises when I make the Grid option "enableAddRow: true".
Here, when I try to add something in the new row, whenever I insert something in any cell and shift to other, the data is not there. For example:
My declaration for the grid:
var categories, categoryList, subjectList, data, grid;
var url = "{{ url('get_data', {'_prefix': app.request.get('_prefix')}) }}";
var dataView = new Slick.Data.DataView();
var columns = [
{id: "serial", name: "Ordnungsnummer", field: "serial", editor: Slick.Editors.Integer, validator: requiredFieldValidator, sortable: true},
{id: "category", name: "Kategorie", field: "category", editor: Slick.Editors.categoryDropdown, formatter: Slick.Formatters.category, validator: requiredFieldValidator, sortable: true},
{id: "subject", name: "Themenbereich", field: "subject", editor: Slick.Editors.subjectDropdown, formatter: Slick.Formatters.subject, validator: requiredFieldValidator, sortable: true},
{id: "week", name: "Schulwoche", field: "week", editor: Slick.Editors.Integer, validator: requiredFieldValidator, sortable: true},
{id: "note", name: "Anmerkung", field: "note", editor: Slick.Editors.LongText, validator: requiredFieldValidator}
];
var options = {
editable: true,
enableAddRow: true,
enableCellNavigation: true,
asyncEditorLoading: true,
forceFitColumns: true,
topPanelHeight: 25
};
grid = new Slick.Grid("#myGrid", dataView, columns, options);
// wire up model events to drive the grid
dataView.onRowCountChanged.subscribe(function (e, args) {
grid.updateRowCount();
grid.render();
});
dataView.onRowsChanged.subscribe(function (e, args) {
grid.invalidateRows(args.rows);
grid.render();
});
grid.onAddNewRow.subscribe(function (e, args) {
var item = args.item;
var column = args.column;
grid.invalidateRow(dataView.length);
dataView.push(item);
grid.updateRowCount();
grid.render();
});
function fetchData() {
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: {schoolclass: {{ schoolclass }}},
success: function( response ) {
createGrid(response);
}
});
}
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};
}
}
function createGrid(response) {
data = response.data;
categories = response.categories;
categoryList = generateCategoryList(categories);
subjectList = generateSubjectList(categories);
dataView.beginUpdate();
dataView.setItems(data);
console.log(data);
dataView.endUpdate();
}
function generateCategoryList(categories) {
var categoryList = {};
$.each(categories, function(index, item) {
categoryList[item.id] = item.name;
});
return categoryList;
}
function generateSubjectList(categories) {
var subjectList = {};
$.each(categories, function(index, category) {
$.each(category.subjects, function(index, subjects) {
subjectList[subjects.id] = subjects.name;
});
});
return subjectList;
}
function sendData(data) {
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: data,
success: function( response ) {
createGrid(response);
alert("data successfully saved");
},
error: function(textStatus, errorThrown) {
alert("sorry could not save the data");
console.log("text Status = "+ textStatus);
console.log("Error Thrown = "+ errorThrown);
}
});
}
$(function () {
fetchData();
$("#save").click(function() {
var newData = JSON.stringify(grid.getData());
sendData(newData);
});
})
Any help is highly appreciated.