Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Row insertion #1042

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/example-insert-row.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SlickGrid example: Insert or delete a row</title>
<link rel="stylesheet" href="../slick.grid.css" type="text/css"/>
<link rel="stylesheet" href="../css/smoothness/jquery-ui-1.8.16.custom.css" type="text/css"/>
<link rel="stylesheet" href="examples.css" type="text/css"/>
</head>
<body>
<table width="100%">
<tr>
<td valign="top" width="50%">
<div id="myGrid" style="width:600px;height:500px;"></div>
</td>
<td valign="top">
<h2>Demonstrates:</h2>
<ul>
<li>adding a row</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/mleibman/SlickGrid/blob/gh-pages/examples/example-insert-row.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
<button id="button" onclick="addRow()">Adding a row at index 5</button>
</td>
</tr>
</table>

<script src="../lib/jquery-1.7.min.js"></script>
<script src="../lib/jquery.event.drag-2.2.js"></script>

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

<script>
var grid;
var dataView;
var columns = [
{id: "title", name: "Title", field: "title"},
{id: "duration", name: "Duration", field: "duration"},
{id: "%", name: "% Complete", field: "percentComplete"},
{id: "start", name: "Start", field: "start"},
{id: "finish", name: "Finish", field: "finish"},
{id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
];

var options = {
enableCellNavigation: true,
enableColumnReorder: false
};

$(function () {
var data = [];
for (var i = 0; i < 500; i++) {
data[i] = {
id: i,
title: "Task " + i,
duration: "5 days",
percentComplete: Math.round(Math.random() * 100),
start: "01/01/2009",
finish: "01/05/2009",
effortDriven: (i % 5 == 0)
};
}

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

dataView.onRowCountChanged.subscribe(function (e, args) {
grid.updateRowCount();
grid.render();
});

dataView.onRowsChanged.subscribe(function (e, args) {
grid.invalidateRows(args.rows);
grid.render();
});
})

function addRow() {
dataView.beginUpdate();
dataView.insertItem(5, { id: 'new5', title: "New task", duration: "Days", percentComplete: 0 });
dataView.endUpdate();
}

</script>
</body>
</html>
25 changes: 21 additions & 4 deletions slick.dataview.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,30 @@
refresh();
}

/**
* @param newRow we wish to add. Checks that newRow defines
* property idProperty, and that newRow[idProperty] is
* not already taken by something in the idxById map
*/
function ensureIdAndThatItDoesNotExist(newRow) {
var id = newRow[idProperty]
if (id === undefined) {
throw "Each data element must implement a unique 'id' property";
}
if (idxById.hasOwnProperty(id)) {
throw "Id " + newId + " is already taken";
}
}

function insertItem(insertBefore, item) {
ensureIdAndThatItDoesNotExist(item)
items.splice(insertBefore, 0, item);
updateIdxById(insertBefore);
refresh();
}

function addItem(item) {
ensureIdAndThatItDoesNotExist(item)
items.push(item);
updateIdxById(items.length - 1);
refresh();
Expand Down Expand Up @@ -512,7 +529,7 @@
group = groups[i];
group.groups = extractGroups(group.rows, group);
}
}
}

groups.sort(groupingInfos[level].comparer);

Expand Down Expand Up @@ -562,7 +579,7 @@
level = level || 0;
var gi = groupingInfos[level];
var groupCollapsed = gi.collapsed;
var toggledGroups = toggledGroupsByLevel[level];
var toggledGroups = toggledGroupsByLevel[level];
var idx = groups.length, g;
while (idx--) {
g = groups[idx];
Expand All @@ -584,7 +601,7 @@
g.collapsed = groupCollapsed ^ toggledGroups[g.groupingKey];
g.title = gi.formatter ? gi.formatter(g) : g.value;
}
}
}

function flattenGroupedRows(groups, level) {
level = level || 0;
Expand Down Expand Up @@ -901,7 +918,7 @@
inHandler = true;
var selectedRows = self.mapIdsToRows(selectedRowIds);
if (!preserveHidden) {
setSelectedRowIds(self.mapRowsToIds(selectedRows));
setSelectedRowIds(self.mapRowsToIds(selectedRows));
}
grid.setSelectedRows(selectedRows);
inHandler = false;
Expand Down
12 changes: 11 additions & 1 deletion tests/dataview/dataview.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ test("updating an item to pass the filter", function() {
same(args.pageSize, 0, "pageSize arg");
same(args.pageNum, 0, "pageNum arg");
same(args.totalRows, 4, "totalRows arg");
count++;
count++;
});
dv.updateItem(3,{id:3,val:3});
equal(count, 3, "events fired");
Expand Down Expand Up @@ -697,6 +697,16 @@ test("insert at the end", function() {
assertConsistency(dv);
});

test("insert with id already taken", function() {
var dv = new Slick.Data.DataView()
dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
try {
dv.insertItem(2, {id:2,val:1337});
ok(false, "exception thrown");
}
catch (ex) {}
})

module("deleteItem");

test("must have id", function() {
Expand Down