Created
August 15, 2018 17:20
-
-
Save gpamfilis/6beaebf85a1f9a48eeb63ddef0810e75 to your computer and use it in GitHub Desktop.
Datatables javascript version 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
$(document).ready(function() { | |
table = $('#example').DataTable({ | |
"ajax":{ | |
"url": "/get_data", | |
"dataSrc" :"items"} | |
}); | |
var csrftoken = $('meta[name=csrf-token]').attr('content') | |
$.ajaxSetup({ | |
beforeSend: function(xhr, settings) { | |
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type)) { | |
xhr.setRequestHeader("X-CSRFToken", csrftoken) | |
//console.log(csrftoken); | |
} | |
} | |
}) | |
$('#example tbody').on( 'click', 'tr', function () { | |
//console.log('Clicking on row.'); | |
if ( $(this).hasClass('selected') ) { | |
//console.log('Unselecting'); | |
$(this).removeClass('selected'); | |
$('#add').prop('disabled', false); | |
$('#modify').prop('disabled', true); | |
$('#delete').prop('disabled', true); | |
var myURL = document.location.origin; | |
var new_url = myURL.substring(0, myURL.indexOf('?')); | |
window.history.pushState("object or string", "Title", myURL ); | |
} | |
else { | |
// this line unselects the previous one. Find a way to take care of them. | |
table.$('tr.selected').removeClass('selected'); | |
console.log('Selecting'); | |
$(this).addClass('selected'); | |
var myURL = document.location.origin; | |
var new_url = myURL.substring(0, myURL.indexOf('?')); | |
window.history.pushState("object or string", "Title", myURL ); | |
$('#add').prop('disabled', false); /// true | |
$('#modify').prop('disabled', false); | |
$('#delete').prop('disabled', false); | |
id = table.row('.selected').data()[0]; | |
var myURL = document.location; | |
var new_url = myURL + "?project_id="+id; | |
window.history.pushState("object or string", "Title", new_url ); | |
row_data = table.row('.selected').data(); | |
console.log(row_data); | |
document.getElementById("project_title").value = row_data[1]; | |
document.getElementById("year_1").value = row_data[2]; | |
document.getElementById("year_2").value = row_data[3]; | |
document.getElementById("year_3").value = row_data[4]; | |
document.getElementById("year_4").value = row_data[5]; | |
document.getElementById("year_5").value = row_data[6]; | |
document.getElementById("justification").value = row_data[7]; | |
document.getElementById("comments").value = row_data[8]; | |
} | |
}); | |
$('#delete').click( function () { | |
id = table.row('.selected').data()[0]; | |
console.log(id); | |
var choice = window.confirm("Are you sure you want to delete this item?"); | |
var myURL = document.location.origin; | |
var new_url = myURL.substring(0, myURL.indexOf('?')); | |
if (choice==true){ | |
table.row('.selected').remove().draw( false ); | |
$.ajax({ | |
type: 'DELETE', | |
url: new_url + "/delete_data/"+id, | |
data:{"deleting":id}, | |
success: function(data){ | |
table.row('.selected').remove().draw( false ); | |
//document.getElementById("add_item").disabled = false; | |
//document.getElementById("edit_item").disabled = true; | |
//document.getElementById("delete_row").disabled = true; | |
}, | |
error: function (xhr, statusText, err) { | |
alert("error"+xhr.status); | |
} | |
});} | |
else{} | |
}); | |
} ); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment