Semantic-UI icon indicating copy to clipboard operation
Semantic-UI copied to clipboard

[Search] Using Different API Responses

Open illuminos opened this issue 11 years ago • 5 comments

I have a question. The documentation is not clear about searching a server json response without the title attribute?

Your suggested server response was { {"title":"apple"}, {"title":"banana"} }.

My existing code gives this {"Apple","Banana"}..without title or any key.

How do I make your search module work in this scenario?

Big fan of your framework. Thank you for your time.

illuminos avatar Dec 10 '14 07:12 illuminos

Modify $.fn.search.templates.category or $.fn.search.templates.simple to construct the HTML from your response.

Here's the default template that generates the html from a response, which you can modify for your instance. https://github.com/Semantic-Org/Semantic-UI/blob/master/src/definitions/modules/search.js#L836

jlukic avatar Dec 10 '14 16:12 jlukic

Hi, Modifying it doesn't seem to work. I have even tried appending a new template like this.

$.fn.search.settings.templates.testing = function(response){ 
      console.log(response)
}

and then, tried to use the template by specifying it like this

$('.ui.search').search({
    apiSettings: {
       url: 'my custom url'
    },
    type: 'testing'
});

That doesn't seem to work. What am I doing wrong? I was able to catch the response by using onResults ..but I was unable to append the data to onResultsAdd.

Please help. Thanks !

illuminos avatar Dec 15 '14 11:12 illuminos

Hi, Never mind. I've got it working !!

This is what I did.

$('.ui.search')
  .search({
    apiSettings: {
        url: 'myURLforJSON'
    },
    cache: false,
    searchFullText: false,
    onResults: function(response){
     var html = '';
     if(response!== undefined) {
        $.each(response, function(index, result) {
            html  += '<a class="result"><div class="content">';
            html += '<div class="title">' + result + '</div>';
            html  += '' + '</div></a>';
            return index < 6;
        });
        $('.ui.search div.results').html(html);
     }
      return false;
    },
    onResultsAdd: function(html){

        if($('div.results a').length==0){
            return "default";
        }
        else return false;
    }
});

I know it looks ugly as hell..but is there an efficient way of doing it? Like a better way of accessing how many items returned from the onResults response from other functions like onResultsAdd And, I also can't seem to find a way of adding this to the existing template.

Thank you for this amazing framework ! Learning loads !

illuminos avatar Dec 15 '14 13:12 illuminos

how to write an external JSON file? is just like this

[
    {
        "title" : "Lorem ipsum dolor sit amet."
    },
    {
        "title" : "Lorem ipsum dolor sit amet."
    },
    {
        "title" : "Lorem ipsum dolor sit amet."
    }
]

SunDi3yansyah avatar Apr 09 '15 04:04 SunDi3yansyah

if anyone comes around the same issue the problem is that response[fields.results] fails the following validations made on the search module.

isProperObject = ($.isPlainObject(response[fields.results]) && !$.isEmptyObject(response[fields.results])),
isProperArray  = ($.isArray(response[fields.results]) && response[fields.results].length > 0),

In order to fix it fields.results must be set correctly on the search configuration and must be a non empty array.

martoo6 avatar Nov 01 '17 03:11 martoo6