Jquery: preliminary autocomplete fields

I am using a tokenizing autocomplete plugin for jquery ( http://loopj.com/2009/04/25/jquery-plugin-tokenizing-autocomplete-text-entry ). I mainly use Ruby, but I'm really new to javascript.

My basic setup looks like this and works great for a new blank form:

$(document).ready(function () {
  $("#tag_ids_field").tokenInput("/tags", {
    queryParam: "search"
  });
});

The problem arises when I try to fill it out, for example, for an edit page. I am trying to do something like this (where the text field "#tag_ids_field" contains JSON when loading the page - this way it is just cleaner on the application side).

$(document).ready(function () {
  var tags = $("#tag_ids_field").html();
  $("#tag_ids_field").tokenInput("/tags", {
    queryParam: "search",
    prePopulate: tags
  });
});

, , , , "undefined". , JSON, Rails, .js:

$(document).ready(function () {
  $("#tag_ids_field").tokenInput("/tags", {
    queryParam: "search",
    prePopulate: "[{\"id\":\"44\",\"name\":\"omnis sit impedit et numquam voluptas enim\"},{\"id\":\"515\",\"name\":\"deserunt odit id doloremque reiciendis aliquid qui vel\"},{\"id\":\"943\",\"name\":\"exercitationem numquam possimus quasi iste nisi illum\"}]"
  });
});

, - , .

:

-, "undefined", , ?

-, , (, , ). , ? - ( , ):

$(document).ready(function () {
  $(".tag_ids_field").tokenInput("/tags", {
    queryParam: "search",
    prePopulate: (the contents of that particular ".tag_ids_field" input box)
  });
});

Flash84x: , . tag_ids_field db - , . ( , ) . ( ) , , , , JSON, .

, , Ruby , . javascript, , , .

+3
2

, :

$(document).ready(function () {
  $(".tag_ids_field").each(function(index) {
    var ids = eval($(this).html());
    $(this).html('');

    $(this).tokenInput("/tags", {
      queryParam: "search",
      prePopulate: ids
    })
  });
});
+3

, ruby, value . ajaxing , . , ...

, , .

+1

Source: https://habr.com/ru/post/1738607/


All Articles