Search code examples
javascriptnode.jshtmldreamfactory

How to show an image from a database with HTML+JS?


I am making a dog shelter app, so I saved all the images URL in the database. So far, this is what I have:

Displaying dogs info

What I want is instead of showing this P I want to show the dog's photo.

My code:

index.html

        <div id="template_especie_show">
            <nav class="navbar navbar-fixed-top df-nav cen col-md-12" role="navigation" style="">
                <ul class="nav navbar-nav" style="width: 100%">
                    <li class="pull-left"><button type="button" id="especie_menu_left" class="btn btn-default btn-menu">Voltar</button></li>
                    <li class="pull-left"><button type="button" id="especie_menu_logout" class="btn btn-default btn-menu-logout">Sair</button></li>
                    <li style="float: none; display: inline-block; position: relative; text-align: center"><a href="#index"><img src="img/icone_ap.png" height="47"></a></li>
                </ul>
            </nav>
            <div class="row vert-offset-top-30"></div>
            <div class="col-md-2"></div>
            <div class="col-md-8">

                <div style="text-align: center"><h2><span id="animal_show_name"></span></h2></div>
                <div class="input-group">
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                    </div>
                    <input type="text" id="table_especie_search" class="form-control" placeholder="Procurar animais">
                </div>
                <table class="table table-hover" id="table_especie">
                    <thead>
                    <tr>
                        <th></th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
            <div class="col-md-2"></div>
        </div>

script.js

var tableEspecie= $('#table_especie').DataTable({
    "paging":   false,
    "info":     false,
    "order": [
        [2, "asc" ],
        [3, "asc"],
        [1, "asc"]
    ],
    "columnDefs": [
        { "visible": false, "targets": 0 },
        { "visible": false, "targets": 2 },
        { "visible": false, "targets": 3 }
    ],

    "drawCallback": function () {
        var api = this.api();
        var rows = api.rows( {page:'current'} ).nodes();
        var last=null;

        api.column(2, {page:'current'} ).data().each( function ( especie, i ) {
            if ( last !== especie) {
                $(rows).eq( i ).before(
                    '<tr class="especie info"><td colspan="4">'+especie+'</td></tr>'
                );

                last = especie;
            }
        } );

        $("#table_especie thead").remove();
        $("#table_especie tfoot").remove();
    }
});

var populateEspecieShowName = function(data) {
    $('#animal_especie_name').text(data[0].name);
};


var populateEspecieTable = function(data) {
    var animais = [];

    $.each(data, function(id_animal, animal){
        animais.push([
            animal.id_animal,
            animal.nome_animal + ': ' + '<br>' + animal.notas_animal,
            animal.nome_animal.charAt(0).toUpperCase()
        ]);
    });

    $('#table_especie').dataTable().fnClearTable();
    $('#table_especie').dataTable().fnAddData(animais);
    $('#table_especie').dataTable().fnDraw();
};

$('#table_especie tbody').on( 'click', 'tr', function () {
    var animalId = $('#table_especie').DataTable().row(this).data();

    if (animalId !== undefined)
        $.route('animal/' + animalId[0]);
});

$('#table_especie_search').keyup(function(){
    $('#table_especie').DataTable().search($(this).val(), false, true).draw() ;
});

route.js

 case 'especie_show':
                setButton('left', template, 'especies', 'redirect', null);
                setButton('right', template, 'especie/' + pathArray[1] + '/edit', 'redirect', null);


                var params = 'filter=id_especie%3D' + pathArray[1] + '&fields=nome_especie';
                $.api.getRecords('especie', params, getToken('token'), populateEspecieShowName);

                params = 'filter=especie_id_especie%3D' + pathArray[1] + '&fields=nome_animal, notas_animal';
                $.api.getRecords('animal', params, getToken('token'), populateEspecieTable);

                break;

Thanks everyone for the attention and help!


Solution

  • I don't know how your data is structured, but you would need a url for each dog in the shelter.

    [{"name":"fido", 
    "img":"some url"}, 
    {"name":"woofer", 
    "img":"some 2nd url"}, 
    {"name":"champ", 
    "img":"some 3rd url"
    }]
    

    You would pull that info into your js. Then you would loop over each item and set the image tag source attribute accordingly.

            for(var i = 0; i < arrayOfDogs.length; i++){
             (your img node).src = arrayOfDogs[i].url;         
              }
    

    It looks like you're using jQuery. So you can create a template and when you loop over the data, append a new row/cell/header/whatever to the table or div