Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Will contain which type of item table contains.
- var tableType = null;
- var EditableTable = function () {
- return {
- //main function to initiate the module
- init: function ($editableSample) {
- // this object type.
- // this.type = type;
- function restoreRow(oTable, nRow) {
- var aData = oTable.fnGetData(nRow);
- var jqTds = $('>td', nRow);
- for (var i = 0, iLen = jqTds.length; i < iLen; i++) {
- oTable.fnUpdate(aData[i], nRow, i, false);
- }
- oTable.fnDraw();
- }
- var useInputTag = false;
- function editRow(oTable, nRow) {
- var aData = oTable.fnGetData(nRow);
- var $jqTds = $('>td', nRow);
- switch(tableType){
- case 'group':
- editGroup($jqTds, aData);
- break;
- case 'user':
- editUser($jqTds, aData);
- break;
- }
- }
- function saveRow(oTable, nRow) {
- // SAVE WHEN ADD NEW.
- if (useInputTag) {
- // get only <input> element which is groupe-name from current row.
- var $jqInputs = $('input', nRow);
- switch(tableType)
- {
- case 'user':
- break;
- case 'group':
- saveGroupInfosNew($jqInputs)
- break;
- }
- }
- else // SAVE WHEN EDITIING
- {
- switch(tableType)
- {
- case 'user':
- break;
- case 'group':
- saveGroupeInfosEdit(nRow)
- break;
- }
- }
- }
- // NOT USED
- function cancelEditRow(oTable, nRow) {
- var jqInputs = $('input', nRow);
- oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
- oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
- // $('td', nRow).eq().text();
- oTable.fnUpdate(jqInputs[2].value, nRow, 2, false);
- oTable.fnUpdate(jqInputs[3].value, nRow, 3, false);
- oTable.fnUpdate(jqInputs[4].value, nRow, 4, false);
- oTable.fnUpdate('<a class="edit" href="">Editer</a>', nRow, 5, false);
- oTable.fnDraw();
- }
- // $('#editable-sample') == $table
- var oTable = $editableSample.dataTable({
- "aLengthMenu": [
- [5, 15, 20, -1],
- [5, 15, 20, "All"] // change per page values here
- ],
- // set the initial value
- "iDisplayLength": 5,
- "sDom": "<'row'<'col-lg-6'l><'col-lg-6'f>r>t<'row'<'col-lg-6'i><'col-lg-6'p>>",
- "sPaginationType": "bootstrap",
- "oLanguage": {
- "sLengthMenu": "_MENU_ records per page",
- "oPaginate": {
- "sPrevious": "Prev",
- "sNext": "Next"
- }
- },
- "aoColumnDefs": [{
- 'bSortable': false,
- 'aTargets': [0]
- }
- ]
- });
- var $editableSampleWrapper = $('#editable-sample_wrapper');
- $editableSampleWrapper.find('.dataTables_filter input').addClass("form-control medium"); // modify table search input
- $editableSampleWrapper.find('.dataTables_length select').addClass("form-control xsmall"); // modify table per page dropdown
- var nEditing = null;
- // FIRE WHEN ADD NEW IS CLICKED not dynamic which mean .
- // event if new element is adding with this same (#editable-...) its binded with this.
- $('#editable-sample_new').click(function (e) {
- e.preventDefault();
- // TODO: make this specific per table type.
- var aiNew = oTable.fnAddData([guid(), '',
- // TODO: use calender/picker
- '<input type="date" name="date">',
- 'Hors ligne',
- '<input type="checkbox" disabled="disabled" checked>',
- // TODO: Rename this to sauvegarder/enregistre
- '<a class="edit" href="">Save</a>',
- '<a class="cancel" data-mode="new" href="">Cancel</a>'
- ]);
- // Prevent multiply adding.
- $(this).attr('disabled', 'disabled');
- // TODO: change text from edit to save.
- var nRow = oTable.fnGetNodes(aiNew[0]);
- useInputTag = true;
- editRow(oTable, nRow);
- nEditing = nRow;
- });
- // on binds dynamic clicking to new added element can use this method. Note: *click event is not dynamic!
- $editableSample.on('click', 'a.delete', function (e) {
- console.log("delete clicked");
- e.preventDefault();
- // TODO: Uncommet this!
- if (confirm("Voulez vous supprimer?") == false) {
- return;
- }
- //console.log($(obj).html());
- // $(this).toggle(2000)
- // $(this).toggle(1000);
- // console.log($(this).parents().parents().html());
- // console.log($(this).parents().parents().length);
- // //console.log($(this).html());
- // var jqInputs = $('tr', $(this));
- //console.log($(nRow).html().('input'));
- // console.log($(this).html());
- // console.log($(this).text());
- // console.log($(this).parents().html());
- // console.log(jqInputs.html());
- // console.log(jqInputs.length);
- var nRow = $(this).parents('tr')[0];
- var reference = $('td', nRow).first().text();
- $.post('vue/Groupe/quickInsert.php',
- {
- reference: reference,
- function: 'delete'
- });
- oTable.fnDeleteRow(nRow);
- });
- $editableSample.on('click', 'a.cancel', function (e) {
- e.preventDefault();
- if ($(this).attr("data-mode") == "new") {
- var nRow = $(this).parents('tr')[0];
- oTable.fnDeleteRow(nRow);
- nEditing = null;
- } else {
- restoreRow(oTable, nEditing);
- nEditing = null;
- }
- enableAdd();
- });
- $editableSample.on('click', 'a.edit', function (e) {
- e.preventDefault();
- // disable add new click when editing...
- var $buttonAddNew = $('#editable-sample_new');
- if(!$buttonAddNew.is(":disabled"))
- {
- $buttonAddNew.attr('disabled', 'disabled');
- }
- /* Get the row as a parent of the link that was clicked on */
- var nRow = $(this).parents('tr')[0];
- if (nEditing !== null && nEditing != nRow) {
- /* Currently editing - but not this row - restore the old before continuing to edit mode */
- restoreRow(oTable, nEditing);
- editRow(oTable, nRow);
- nEditing = nRow;
- } else if (nEditing == nRow && this.innerHTML == "Save") {
- /* Editing this row and want to save it */
- // TODO: Do validation before calling this method.
- saveRow(oTable, nEditing);
- // Enable adding.
- enableAdd();
- // TODO: test.
- //console.log($(this).parents['tr'].eq(0) == null);
- // remove disabled attribute
- // .('input [disabled="disabled"]').removeAttribute('disabled');
- //console.log($(nRow).children().removeAttr('disabled'));
- //console.log($(nRow).html());
- // console.log($(nRow).children().html());
- //$(nRow).('input').removeAttr('disabled');
- // $(nRow).children().removeAttr("disabled");
- //console.log($(nRow).html());
- // TODO: delete
- // try {
- // var inputTag = $(nRow).children()
- // console.log($(inputTag)[1].attributes);
- // if (inputTag != null) {
- // console.log($(inputTag).attr('disabled'));
- // }
- // } catch (e) {
- // console.log(e.toString());
- // }
- // $(nRow).remove(); works
- // $(nRow).("[disabled]").removeAttr("disabled");
- //console.log('input:' + $(nRow).children().html());
- nEditing = null;
- // alert("Updated! Do not forget to do some ajax to sync with backend :)");
- } else {
- /* No edit in progress - let's start one */
- useInputTag = false;
- editRow(oTable, nRow);
- nEditing = nRow;
- }
- });
- // ------------------- Helper functions
- function enableAdd() {
- $('#editable-sample_new').removeAttr('disabled');
- }
- function guid() {
- function s4() {
- return Math.floor((1 + Math.random()) * 0x10000)
- .toString(16)
- .substring(1);
- }
- return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
- s4() + '-' + s4() + s4() + s4();
- }
- function getDatePicker(){
- var strVar="";
- strVar += " <div class=\"form-group\">";
- strVar += " <label class=\"control-label col-md-3\">Start with years viewMode<\/label>";
- strVar += " <div class=\"col-md-4 col-xs-11\">";
- strVar += "";
- strVar += " <div data-date-viewmode=\"years\" data-date-format=\"dd-mm-yyyy\" data-date=\"12-02-2012\" class=\"input-append date dpYears\">";
- strVar += " <input type=\"text\" readonly=\"\" value=\"12-02-2012\" size=\"16\" class=\"form-control\">";
- strVar += " <span class=\"input-group-btn add-on\">";
- strVar += " <button class=\"btn btn-primary\" type=\"button\"><i class=\"fa fa-calendar\"><\/i><\/button>";
- strVar += " <\/span>";
- strVar += " <\/div>";
- strVar += " <\/div>";
- strVar += " <\/div>";
- return strVar;
- }
- // -------------------- Group helper functions
- function editGroup($jqTds, aData){
- // Adding new row
- if (useInputTag) {
- // if($(jqTds[0]).is(":visible"))
- // {
- // console.log('hide')
- // $(jqTds[0]).hide();
- // }
- // ref
- $jqTds[0].innerHTML = aData[0];
- // name
- $jqTds[1].innerHTML = '<input type="text" class="form-control small" value="' + aData[1] + '">';
- // date creation
- // jqTds[2].innerHTML = '<input type="text" class="form-control small" value="' + aData[2] + '">';
- // jqTds[2].innerHTML = '<input type="text" class="form-control small" value="' + aData[2] + '">';
- jqTds[2].innerHTML = '<input class="form-control input-medium default-date-picker" type="date" name="date">';
- // $jqTds[2].innerHTML = getDatePicker();
- // status
- $jqTds[3].innerHTML = aData[3];
- //jqTds[4].innerHTML = '<input type="text" class="form-control small" value="' + aData[4] + ' disabled="disabled">';
- // alive
- $jqTds[4].innerHTML = aData[4];
- $jqTds[5].innerHTML = '<a class="Save" href="">Save</a>';
- $jqTds[6].innerHTML = '<a class="cancel" data-mode="new" href="">Cancel</a>';
- }
- else { // Edit name only.
- // TODO: test.
- $jqTds[0].innerHTML = aData[0];
- $jqTds[1].innerHTML = '<input type="text" class="form-control small" value="' + aData[1] + '">';
- $jqTds[2].innerHTML = aData[2];
- $jqTds[3].innerHTML = aData[3];
- $jqTds[4].innerHTML = aData[4];
- $jqTds[5].innerHTML = '<a class="edit" href="">Save</a>';
- $jqTds[6].innerHTML = '<a class="cancel" href="">Cancel</a>';
- }
- }
- function saveGroupInfosNew($jqInputs) {
- // INSERT
- var reference = $jqInputs[0].value;
- var nom = $jqInputs[1].value;
- var date_creation = $jqInputs[2].value;
- var statut = 'Hors ligne';
- var alive = '<td><input type="checkbox" checked disabled="disabled" /></td>';
- oTable.fnUpdate(reference, nRow, 0, false);
- oTable.fnUpdate(nom, nRow, 1, false);
- oTable.fnUpdate(date_creation, nRow, 2, false);
- oTable.fnUpdate(statut, nRow, 3, false);
- oTable.fnUpdate(alive, nRow, 4, false);
- oTable.fnUpdate('<a class="edit" href="">Editer</a>', nRow, 5, false);
- oTable.fnUpdate('<a class="delete" href="">Supprimer</a>', nRow, 6, false);
- oTable.fnDraw();
- $.post("vue/Groupe/quickInsert.php",
- {
- reference: reference,
- nom: nom,
- date_creation: date_creation,
- statut: statut,
- alive: alive,
- function: 'insert'
- }, function (obj) {
- //alert(obj);
- });
- }
- function saveGroupeInfosEdit(nRow){
- // UPDATE
- // var aData = oTable.fnGetData(nRow);
- // get all input from row
- // var jqInputs = $('input', nRow); default
- // NOTE: Useful.
- /*
- // method 1
- var val_1 = $('td', nRow)[0];
- console.log(val_1.innerHTML);
- // method 2
- console.log($('td', nRow).first().text());
- //method 3
- console.log($('td', nRow).eq(0).text());
- */
- // get text of first td from row;
- var reference = $('td', nRow).first().text();
- var newGroupName = $('input', nRow)[0].value; // javascript way, with jquery would be: .eq(0).text();
- // console.log(reference);
- // console.log(newGroupName);
- // throw new Error();
- oTable.fnUpdate(newGroupName, nRow, 1, false);
- oTable.fnUpdate('<a class="edit" href="">Editer</a>', nRow, 5, false);
- oTable.fnUpdate('<a class="delete" href="">Supprimer</a>', nRow, 6, false);
- oTable.fnDraw();
- // DO UPDATE
- $.post('vue/Groupe/quickInsert.php',
- {
- reference: reference,
- groupe_name: newGroupName,
- function: 'update'
- });
- }
- // -------------------- USER helper functions
- function editUser(jqTdsm, aData) {
- }
- function saveUserInfos($jqInputs) {
- throw new Error('Not implemented!');
- }
- }
- };
- }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement