개 보호소를 만들고있어 이미지 URL을 모두 데이터베이스에 저장했습니다. 지금까지, 이것은 내가 무엇을 가지고 : 내가 원하는 것은 내가 강아지의 사진을 보여주고 싶은이 P를 보여주는 대신이다HTML + JS로 데이터베이스의 이미지를 표시하는 방법은 무엇입니까?
.
내 코드 :
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;
모두에게 감사하고 도와주세요!
아, 정말요? 나는 그것을 피하고 싶었습니다. 나는 그것을 시도 할 것이다, 고마워. arrayOfDogs 내 pathArray, 오른쪽 생각하십니까? 그리고 그것을 script.js에 var populateEspecieTable = function (data) 밑에 넣어야합니다. 맞습니까? 첫 번째 코드 단편은 내가 어디에 넣어야하는지 알려주시겠습니까? –
@EduardaBuss [파트 1] arrayOfDogs는 pathArray이고, IF setButton() 함수는 그리드/테이블을 템플릿으로 작성하는 함수입니다. js를 구조화하는 방법에 관해서는, AngularJS 앱에서 js에 대한 다른 용도는 (아마도) 서로 다른 파일로 구분됩니다. 예 : 'factory.js'-는 db/server에서 데이터를 요청한 파일의 이름이 될 수 있으며 ArrayofDog를 포함하는 객체로 포맷합니다. 'scripts.js'- 'controller.js'로 이름을 바꿀 수 있습니다 (실제로는 문제가되지 않습니다). db에 대한 호출이 해결 된 후입니다 (보통 약속 패턴으로 처리됩니다). – Tamb
@EduardaBuss [파트 2 ] 약속을 사용하는 것이 불편한 경우 if/else 문을 사용하여 데이터를 요청한 다음 (오류 처리를 보장) 서식을 지정한 다음 템플릿 기능에 전달할 수 있습니다. - 이름 충돌을 피하기 위해 함수/객체로 이것을 배치하는 것이 좋습니다. 그런 다음 코드를 모듈로 분리 할 수 있습니다 (데이터 가져 오기, 템플릿 작성 등). 본질적으로 이것이 AngularJS가하는 것입니다. 하지만 다시 한번, "모듈 패턴"을 확인하고 거기에서부터 더 편안하게 느낀다면. 그것이 JS에서 시작한 방법입니다. – Tamb