使用algoliasearch将href更改为半硬编码变量

时间:2019-01-11 06:35:49

标签: javascript algolia

因此,基本上我正在使用AlgoliaSearch,并希望能够让用户点击出现的搜索结果。由于objectID与数据库中的ID匹配,因此我认为我可以做一些可以对a href =进行半硬编码的操作,并在html标记的末尾添加或添加ID。我尝试获取Javascript的getdocumentbyID,但这不起作用。

谢谢您的帮助,

<script>
    var index = client.initIndex('MultipleChoiceQuestion');
    //initialize autocomplete on search input (ID selector must match)
    autocomplete('#aa-search-input', { hint: false }, {
      source: autocomplete.sources.hits(index, { hitsPerPage: 25 }),
      //value to be displayed in input control after user's suggestion selection
      displayKey: 'name',
      //hash of templates used when rendering dataset
      templates: {
        //'suggestion' templating function used to render a single suggestion
        suggestion: function(suggestion) {
          var idx = suggestion.objectID;
          var array = suggestion.tag_list.join('\n');
          return '<div class="aa-highlight column is-2">' +
            array + '</div><div class="aa-mcq column is-4"><a href="" id="search-mcq-id">' +
            suggestion._highlightResult.question.value + '</a></div><div class="column is-4">' +
            suggestion._highlightResult.answer_explanation.value + idx + '</div>';
                          document.getElementById('search-mcq-id').href = idx;

        }
      }
    });

    </script>

tl; dr-我想要一个半硬编码链接的链接,该链接类似于<A href="example.com/something/OBJECTID">,但是我尝试的方法不起作用。

1 个答案:

答案 0 :(得分:1)

如果要对其进行硬编码,是否可以在要编写的JS模板中进行设置?似乎您通过返回JS后尝试设置href来使事情变得过于复杂(请注意:return编辑

后您将无法执行任何操作

也许尝试一下:

var idx = suggestion.objectID;
var array = suggestion.tag_list.join('\n');
return '<div class="aa-highlight column is-2">' +
  array + '</div><div class="aa-mcq column is-4"><a href="/' + idx + '">' +
  suggestion._highlightResult.question.value + '</a></div><div class="column is-4">' +
  suggestion._highlightResult.answer_explanation.value + idx + '</div>';