【问题标题】:Semantic ui search not work语义 ui 搜索不起作用
【发布时间】:2017-03-14 22:12:55
【问题描述】:

我使用语义 ui,当我在网站上进行搜索时,结果为空,但当我查看控制台时,我看到了 json 结果

这是我的 js 代码

$('.ui.search').search({
    apiSettings: {
        url: "https://api.github.com/search/repositories?q={query}"
    },
   fields: {
        results: 'items',
        title: 'name',
        url: 'html_url',
        description: 'description'
    }
  });

还有我的html代码

<div class="ui right aligned category search item">
  <div class="ui transparent icon input">
    <input class="prompt" placeholder="Rechercher" type="text">
    <i class="search link icon"></i>
  </div>
  <div class="results"></div>
</div>

screenshot results in my html page

我已经尝试了语义 ui 页面的示例

    $('.ui.search')
  .search({
    type          : 'category',
    minCharacters : 3,
    apiSettings   : {
      onResponse: function(githubResponse) {
        var
          response = {
            results : {}
          }
        ;
        // translate GitHub API response to work with search
        $.each(githubResponse.items, function(index, item) {
          var
            language   = item.language || 'Unknown',
            maxResults = 8
          ;
          if(index >= maxResults) {
            return false;
          }
          // create new language category
          if(response.results[language] === undefined) {
            response.results[language] = {
              name    : language,
              results : []
            };
          }
          // add result to category
          response.results[language].results.push({
            title       : item.name,
            description : item.description,
            url         : item.html_url
          });
        });
        return response;
      },
      url: '//api.github.com/search/repositories?q={query}'
    }
  })

这是行不通的

【问题讨论】:

    标签: javascript php json search semantic-ui


    【解决方案1】:

    和你有同样的问题

    调试器并得到这个: debug screenshot

    似乎会从响应中获取“结果”字段,因此如果您的响应没有“结果”字段,则需要在 onResponse 回调中设置“结果”:

    apiSettings : {
      onResponse (response) {
        return {
          results: response.myresults
        }
      }
    }
    

    如果你没有设置模板,它将使用标准模板,标准模板使用'title'字段显示,你需要做一些这样的转换:

    response.myresults.forEach((item) => {
      item.title = item.name;
    })
    

    希望对你有帮助

    【讨论】:

    • 我不明白。 OP 在其响应对象中有一个“结果”标签 - 有什么区别? :o 在使用像 OP 这样的类别时,我在语义中也遇到了同样的错误(没有类别也可以正常工作)。有趣的是,结果确实出现了,但 maxResults 不适用。
    猜你喜欢
    • 2016-10-01
    • 2012-09-22
    • 2016-05-25
    • 2016-11-12
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多