Last active
November 21, 2017 17:32
-
-
Save guaiamum/8ccf6cc869604a087fa2bf46636bc81e to your computer and use it in GitHub Desktop.
Kind of universal script to store entity in local array, edit via ajax and display in DataTables.
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
var table; | |
var perguntas = []; | |
var codigoPerg = 0; | |
var acoes = '<i class="fa fa-arrow-up fa-lg praCima"></i> <i class="fa fa-arrow-down fa-lg praBaixo"></i> <i class="fa fa-pencil fa-lg editar" title="' + Resources.EDITAR + '"></i> <i class="fa fa-trash-o fa-lg remover" style="color: #ff0000" title="' + Resources.REMOVER +'"></i>'; | |
function updateTablePerguntas() { | |
table.clear(); | |
perguntas.sort(function (a, b) { return a.Ordem - b.Ordem; }); | |
table.rows.add(perguntas).draw(); | |
} | |
function alteraOrdem(a, b, alterarAjax) { | |
var aux = a.Ordem; | |
a.Ordem = b.Ordem; | |
b.Ordem = aux; | |
if (alterarAjax) | |
alteraAjax(a,b); | |
updateTablePerguntas(); | |
} | |
var AddPergunta = (adicionarAjax) => { | |
var campoTxt = document.getElementById('TxtPerguntaADD'); | |
if (!campoTxt.validity.valid) { | |
$(campoTxt).closest(".form-group").addClass("has-error"); | |
return; | |
} | |
codigoPerg++; | |
var p = { Ordem: perguntas.length + 1, Texto: campoTxt.value, Acoes: acoes, CodigoPergunta: codigoPerg } | |
perguntas.push(p); | |
if (adicionarAjax) { | |
adicionaAjax(p); | |
} else { | |
updateTablePerguntas(); | |
} | |
campoTxt.value = ""; | |
$(campoTxt).closest(".form-group").removeClass("has-error"); | |
} | |
var removerPergunta = (p, removerAjax) => { | |
var ordemAtual = p.Ordem; | |
var posicao = ordemAtual - 1; | |
//REMOVE | |
perguntas.splice(posicao, 1); | |
if (removerAjax === true) { | |
$.ajax({ | |
url: "../RemoverPergunta", | |
data: p, | |
type: 'POST' | |
}).complete(function (data) { | |
if (data.responseJSON.success === false) { | |
var errorStr = ''; | |
$.each(data.responseJSON.errors, | |
function (index, value) { | |
errorStr += '<p>' + value + '</p>'; | |
}); | |
swal({ | |
type: "warning", | |
title: Resources.ERRO, | |
html: errorStr | |
}); | |
} | |
}); | |
} | |
//REORDENA | |
for (var i = posicao; i < perguntas.length; i++) { | |
perguntas[i].Ordem = ordemAtual; | |
editaAjax(perguntas[i]); | |
ordemAtual++; | |
} | |
updateTablePerguntas(); | |
}; | |
var editarPergunta = (p, edicaoAjax) => { | |
var div = $('<div>', | |
{ | |
class: 'form-group container col-md-offset-2' | |
}); | |
var txt = $('<input>', | |
{ | |
id: 'novo-texto', | |
type: 'text', | |
class: 'form-control', | |
value: p.Texto, | |
required: "required" | |
}); | |
div.append(txt); | |
swal({ | |
type: 'warning', | |
title: Resources.DIGITE_PERGUNTA, | |
html: div, | |
showCancelButton: true, | |
confirmButtonColor: '#5cb85c', | |
confirmButtonText: Resources.SALVAR, | |
cancelButtonText: Resources.CANCELAR, | |
showLoaderOnConfirm: true, | |
preConfirm: function () { | |
return new Promise(function (resolve, reject) { | |
var campoTxt = document.getElementById('novo-texto'); | |
if (campoTxt.validity.valid) { | |
resolve(campoTxt.value); | |
} | |
reject(Resources.CAMPO_OBG); | |
}); | |
} | |
}).then(function (texto) { | |
p.Texto = texto; | |
if (edicaoAjax === true) { | |
editaAjax(p); | |
} | |
updateTablePerguntas(); | |
swal.close(); | |
}); | |
}; | |
function editaAjax(p) { | |
$.ajax({ | |
url: "../EditarPergunta", | |
data: p, | |
type: 'POST' | |
}).complete(function(data) { | |
if (data.responseJSON.success === false) { | |
var errorStr = ''; | |
$.each(data.responseJSON.errors, | |
function(index, value) { | |
errorStr += '<p>' + value + '</p>'; | |
}); | |
swal({ | |
type: "warning", | |
title: Resources.ERRO, | |
html: errorStr | |
}); | |
} | |
}); | |
} | |
function adicionaAjax(p) { | |
var codChecklist = $('#CodigoChecklist').val(); | |
$.ajax({ | |
url: "../AdicionarPergunta", | |
data: { pergunta: p, codigoChecklist: codChecklist}, | |
type: 'POST' | |
}).complete(function (data) { | |
if (data.responseJSON.success === false) { | |
var errorStr = ''; | |
$.each(data.responseJSON.errors, | |
function(index, value) { | |
errorStr += '<p>' + value + '</p>'; | |
}); | |
swal({ | |
type: "warning", | |
title: Resources.ERRO, | |
html: errorStr | |
}); | |
} else { | |
p.CodigoChecklist = codChecklist; | |
p.CodigoPergunta = data.responseJSON.data.CodigoPergunta; | |
updateTablePerguntas(); | |
} | |
}); | |
} | |
function alteraAjax(a,b) { | |
$.ajax({ | |
url: "../AlterarOrdem", | |
data: { codigoPerguntaA: a.CodigoPergunta, codigoPerguntaB: b.CodigoPergunta }, | |
type: 'POST' | |
}).complete(function (data) { | |
if (data.responseJSON.success === false) { | |
var errorStr = ''; | |
$.each(data.responseJSON.errors, | |
function (index, value) { | |
errorStr += '<p>' + value + '</p>'; | |
}); | |
swal({ | |
type: "warning", | |
title: Resources.ERRO, | |
html: errorStr | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment