Bulk delete rows in SlickGrid. Fixes #1696

This commit is contained in:
Murtuza Zabuawala 2016-09-16 16:46:58 +01:00 committed by Dave Page
parent c5f04d03c6
commit c044af9585
3 changed files with 47 additions and 17 deletions

View File

@ -465,14 +465,27 @@ class TableCommand(GridCommand):
# For deleted rows
elif of_type == 'deleted':
is_first = True
rows_to_delete = []
keys = None
no_of_keys = None
for each_row in changed_data[of_type]:
data = changed_data[of_type][each_row]
sql = render_template("/".join([self.sql_path, 'delete.sql']),
data=data,
object_name=self.object_name,
nsp_name=self.nsp_name)
list_of_sql.append(sql)
list_of_rowid.append(data)
rows_to_delete.append(changed_data[of_type][each_row])
# Fetch the keys for SQL generation
if is_first:
# We need to covert dict_keys to normal list in Python3
# In Python2, it's already a list
keys = list(changed_data[of_type][each_row].keys())
no_of_keys = len(keys)
is_first = False
sql = render_template("/".join([self.sql_path, 'delete.sql']),
data=rows_to_delete,
primary_key_labels=keys,
no_of_keys=no_of_keys,
object_name=self.object_name,
nsp_name=self.nsp_name)
list_of_sql.append(sql)
for i, sql in enumerate(list_of_sql):
if sql:

View File

@ -2149,12 +2149,20 @@ define(
// Remove deleted rows from client as well
if(is_deleted) {
var rows = grid.getSelectedRows();
// Reverse the deletion from array
// so that when we remove it does not affect index
rows = rows.sort().reverse();
rows.forEach(function(idx) {
data.splice(idx, 1);
});
/* In JavaScript sorting by default is lexical,
* To make sorting numerical we need to pass function
* After that we will Reverse the order of sorted array
* so that when we remove it does not affect array index
*/
if(data.length == rows.length) {
// This means all the rows are selected, clear all data
data = [];
} else {
rows = rows.sort(function(a,b){return a - b}).reverse();
rows.forEach(function(idx) {
data.splice(idx, 1);
});
}
grid.setData(data, true);
grid.setSelectedRows([]);
}

View File

@ -1,4 +1,13 @@
{# Delete the row with primary keys #}
DELETE FROM {{ conn|qtIdent(nsp_name, object_name) }} WHERE
{% for pk_key in data %}
{% if not loop.first %} AND {% endif %}{{ conn|qtIdent(pk_key) }} = {{ data[pk_key]|qtLiteral }}{% endfor %};
DELETE FROM {{ conn|qtIdent(nsp_name, object_name) }}
{### If there is only one primary key ###}
{% if no_of_keys == 1 %}
WHERE {{ conn|qtIdent(primary_key_labels[0]) }} IN
{### If there are multiple primary keys ###}
{% elif no_of_keys > 1 %}
WHERE ({% for each_label in primary_key_labels %}{{ conn|qtIdent(each_label) }}{% if not loop.last %}, {% endif %}{% endfor %}) IN
{% endif %}
{### Rows to delete ###}
({% for obj in data %}{% if no_of_keys == 1 %}{{ obj[primary_key_labels[0]]|qtLiteral }}{% elif no_of_keys > 1 %}
{### Here we need to make tuple for each row ###}
({% for each_label in primary_key_labels %}{{ obj[each_label]|qtLiteral }}{% if not loop.last %}, {% endif %}{% endfor %}){% endif %}{% if not loop.last %}, {% endif %}
{% endfor %});