$('document').ready(function(){ // Botones $("#animals").click(function(){ $.get( "/animal", function( data ) { $( "#body" ).html("

Yours Dinos



"); $( "#animal-add").click(function(){ $( "#body" ).load( "/templates/animal-form.html" ); }) dataSet = data.data.map(function(item) { return [item._id, item.name, item.initial_level + " - " + item.level]; }); $('#animals-table').DataTable({ data: dataSet, responsive: true, columns: [ { title: "id", visible: false}, { title: "Name" }, { title: "Level" }, { title: "Options", className: "options-table", orderable: false,data: null, targets: -1, "mRender": function(data, type, full) { return '    '; }} ]}); }); }); $("#species").click(function(){ $.get( "/species", function( data ) { $( "#body" ).html("

All Species

"); dataSet = data.data.map(function(item) { return [item.name, item.description]; }); $('#species-table').DataTable({ data: dataSet, responsive: true, columns: [ { title: "Name" }, { title: "Description" }, { title: "Options", className: "options-table-species", orderable: false,data: null, targets: -1, "mRender": function(data, type, full) { return '' }} ] }); }); }); $("#about").click(function(){ $( "#body" ).load( "/templates/about.html" ); }); }); //Funtions function onSubmitAnimal(form){ var data = {}; $.map($(form).serializeArray(), function(n, i){ if(n['value']!= "") data[n['name']] = n['value']; }); if(data._id){ $.ajax({ type: "PUT", url: "/animal/"+data._id, data: JSON.stringify(data), success: function() { $("#animals").click() }, contentType : "application/json" }); }else{ $.ajax({ type: "POST", url: "/animal", data: JSON.stringify(data), success: function() { $("#animals").click() }, contentType : "application/json" }); } return false; }; function editAnimal(id){ $( "#body" ).load( "/templates/animal-form.html", function(){ $.get( "/animal/"+id, function( data ) { for(key in data.data[0]) { if(data.data[0].hasOwnProperty(key)){ if(key == "father" || key == "mother" || key == "specie" || key == "sex"){ let a = key; setTimeout(function(){ $('#a-'+a+' option[value='+data.data[0][a]+']').attr('selected','selected'); console.log(data.data[0][a]); console.log(a); }, 200) }else{ $('input[name='+key+']').val( data.data[0][key].$numberDecimal ? data.data[0][key].$numberDecimal : data.data[0][key]); } } } }); }); } function showAnimal(id){ $( "#body" ).load( "/templates/animal-show.html", function(){ $.get( "/animal/"+id, function( data ) { $.get( "/animal", function( animalsData ) { $.each(data.data[0], function(i, val) { $("#data").append( "
"+i+": "+(val.$numberDecimal ? val.$numberDecimal : val)+"
"); }); }); }); }); } function removeAnimal(id){ if(confirm("¿Surely you want to erase this dino?")){ $.ajax({ url: '/animal/'+id, type: 'DELETE', success: function() { $("#animals").click() } }); } }