Show message result not found

1

Dear I would like to show a message of results not found if the search criterion is not found in the JSON file, I leave the code that I have where I can add that line of code is a message of no results if no search is found I hope I understand you and thank you

function Cargardata(count) {
  var searchField = $('#search').val();
  var myExp = new RegExp(searchField, 'i');

  $.getJSON('/books-schema.json', function(data) {
    var output = '<ul id="resultado">';

    var dataCounter = 0;

    $.each(data.data, function(i, item) {
        if ((dataCounter < count) && ((item.title.search(myExp) != -1) || (item.teaser.search(myExp) != -1))) {
          output += '<li>';
          output += '<span class="image"><img src=' + item.image + '/></span>';
          output += '<span class="title">' + '<h3>' + item.title + '</h3>' + '</span>';
          output += '<span class="description">' + '<p>' + item.teaser + '</p>' + '</span>';
          output += '</li>';
          dataCounter +=1;
        }
    });
    output += '</ul>';
    $('content').html(output);
  });

}
    
asked by Sixto Mujica 18.08.2017 в 22:41
source

3 answers

1

Because you simply first ask if data.data has elements, if you have then generate the content, otherwise, you generate a single saying that there is no data:

function Cargardata(count) {
  var searchField = $('#search').val();
  var myExp = new RegExp(searchField, 'i');

  $.getJSON('/books-schema.json', function(data) {
    var output = '<ul id="resultado">';

    var dataCounter = 0;    
    if(data.data.length > 0)
    {
        $.each(data.data, function(i, item) {
            if ((dataCounter < count) && ((item.title.search(myExp) != -1) || (item.teaser.search(myExp) != -1))) {
              output += '<li>';
              output += '<span class="image"><img src=' + item.image + '/></span>';
              output += '<span class="title">' + '<h3>' + item.title + '</h3>' + '</span>';
              output += '<span class="description">' + '<p>' + item.teaser + '</p>' + '</span>';
              output += '</li>';
              dataCounter +=1;
            }
        });
    }
    else{
      output += '<li><strong>No se encontro resultados</strong></li>';
    }
    output += '</ul>';
    $('content').html(output);
  });

}
    
answered by 18.08.2017 в 22:48
1

good estimates did not work the code, I do not know why the previous code was changed to me works well that I have and with the change that was made I stop working on this code to me I'm doing well

function Cargardata(count) {
    var searchField = $('#search').val();
    var myExp = new RegExp(searchField, 'i');

    $.getJSON('/books-schema.json', function(data) {
        var output = '<ul id="resultado">';
        var dataToShow = data.data.slice(0, count);
        $.each(dataToShow, function(i, item) {
            if ((item.title.search(myExp) != -1) || (item.teaser.search(myExp) != -1)) {
                output += '<li>';
                output += '<span class="image"><img src=' + item.image + '/></span>';
                output += '<span class="title">' + '<h3>' + item.title + '</h3>' + '</span>';
                output += '<span class="description">' + '<p>' + item.teaser + '</p>' + '</span>';
                output += '</li>';
            }
        });
        output += '</ul>';
        $('content').html(output);
    });

}
    
answered by 18.08.2017 в 23:20
1

If what you want is to print something, when you return 0 you could use:

$.getJSON('/books-schema.json', function(data) {
    if ( data.length == 0 ) {
        console.log("No hay datos!")
    }
});
    
answered by 21.08.2017 в 18:01