Merge pull request #320 from rcutmore/web-group-editing

Validate target input on web front end
pull/322/head
Jordan Wright 2016-07-10 17:30:33 -05:00 committed by GitHub
commit ca43a57767
1 changed files with 52 additions and 28 deletions

View File

@ -94,14 +94,11 @@ function edit(idx) {
}, },
done: function(e, data) { done: function(e, data) {
$.each(data.result, function(i, record) { $.each(data.result, function(i, record) {
targets.DataTable() addTarget(
.row.add([ record.first_name,
escapeHtml(record.first_name), record.last_name,
escapeHtml(record.last_name), record.email,
escapeHtml(record.email), record.position);
escapeHtml(record.position),
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
]).draw()
}); });
} }
}) })
@ -117,6 +114,35 @@ function deleteGroup(idx) {
} }
} }
function addTarget(firstNameInput, lastNameInput, emailInput, positionInput) {
// Create new data row.
var email = escapeHtml(emailInput).toLowerCase();
var newRow = [
escapeHtml(firstNameInput),
escapeHtml(lastNameInput),
email,
escapeHtml(positionInput),
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
];
// Check table to see if email already exists.
var targetsTable = targets.DataTable();
var existingRowIndex = targetsTable
.column(2, {order: "index"}) // Email column has index of 2
.data()
.indexOf(email);
// Update or add new row as necessary.
if (existingRowIndex >= 0) {
targetsTable
.row(existingRowIndex, {order: "index"})
.data(newRow);
} else {
targetsTable.row.add(newRow);
}
targetsTable.draw();
}
function load() { function load() {
$("#groupTable").hide() $("#groupTable").hide()
$("#emptyMessage").hide() $("#emptyMessage").hide()
@ -168,30 +194,28 @@ function load() {
$(document).ready(function() { $(document).ready(function() {
load() load()
// Setup the event listeners // Setup the event listeners
// Handle manual additions // Handle manual additions
$("#targetForm").submit(function() { $("#targetForm").submit(function() {
targets.DataTable() addTarget(
.row.add([ $("#firstName").val(),
escapeHtml($("#firstName").val()), $("#lastName").val(),
escapeHtml($("#lastName").val()), $("#email").val(),
escapeHtml($("#email").val()), $("#position").val());
escapeHtml($("#position").val()),
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>' // Reset user input.
]) $("#targetForm>div>input").val('');
.draw() $("#firstName").focus();
$("#targetForm>div>input").val('') return false;
$("#firstName").focus() });
return false // Handle Deletion
})
// Handle Deletion
$("#targetsTable").on("click", "span>i.fa-trash-o", function() { $("#targetsTable").on("click", "span>i.fa-trash-o", function() {
targets.DataTable() targets.DataTable()
.row($(this).parents('tr')) .row($(this).parents('tr'))
.remove() .remove()
.draw(); .draw();
}) });
$("#modal").on("hide.bs.modal", function() { $("#modal").on("hide.bs.modal", function() {
dismiss() dismiss();
}) });
}) });