社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

jqueryui自动完成模型数据的有效使用方法

Jane Cohen • 5 年前 • 1348 次点击  

我是javascript和jquery的新手。我正在尝试创建一个以模型数据为源的自动完成jqueryui。我不知道怎样做才是最好的办法。 我试图初始化文档中的数据。准备如下:

           var listAddress = [];

           foreach (var item in Model.allBuildings)
          {              


          //adding into address array all addresses for label  and its id.


          @: listAddress.push({ label: "@Html.Raw(item.Address)", id: "@item.ApartmentBlockID" });*@

          }

自动完成功能可以工作,但我一直从开发人员工具获取消息

         Violation] 'setTimeout' handler took 113ms

我的问题是,如何更好地使用模型数据作为自动完成的源?我最大的困惑是我没有在任何地方设置setTimeout函数!错误指向jqueryui脚本中的setTimeout函数??

更新: 这是我的观点

      // first autocomplete

        <div class="col-md-10">
        @Html.HiddenFor(model => model.renovationDetail.ApartmentBlockID, new { @id = "hidden_apartblockID" })
        @Html.EditorFor(model => model.BuildingID, new { htmlAttributes = new { @class = "form-control", @id = "show_buildingID" } })
        @Html.ValidationMessageFor(model => model.renovationDetail.ApartmentBlockID, "", new { @class = "text-danger" })
    </div>
</div>


          //second autocomplete
         <div class="form-group">
    @Html.LabelFor(model => model.allBuildings.First().Address, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control", @id = "show_address" } })
        @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
    </div>
</div


   This is my javascript for address autocomplete(I do the same for the other one):

                                ////function to load building addresses when page loads.
          function ChangeAddressForSelect() {

              //creating autocomplete for address
              $('#show_address')
                  .blur(
                    function () {
                        var keyEvent = $.Event("keydown");
                        keyEvent.keyCode = $.ui.keyCode.ENTER;
                        $(this).trigger(keyEvent);

                  //    })
                  .autocomplete({
                      //source: '/Renovations/GetAddressForEdit',
                      source: function (request, response) {
                          response($.ui.autocomplete.filter(listAddress, 
                                                      request.term));
                      },
                      minLength: 0,
                      scroll: true,
                      select: function (event, ui) {
                          //set tagids to save
                          //$("#hidden_apartblockID").val(ui.item.id);
                          //// Tags for display
                          //this.value = ui.item.value;

                          return false;
                      },
                      focus: function () { $(this).autocomplete("search"); return false; },


                  .blur(function () {

                      //$(this).autocomplete('enable');


                  });

使用模型数据作为每个autcomplete的源的最有效方法是什么?我应该切换到ajax还是ajax源数据会减缓页面加载?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40567
 
1348 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Twisty
Reply   •   1 楼
Twisty    6 年前

这是一个我希望能有所帮助的例子。

$(function() {
  var listAddress = [{
    label: "Address 1",
    value: 1,
    id: 1
  }, {
    label: "Address 2",
    value: 2,
    id: 2
  }, {
    label: "Address 3",
    value: 3,
    id: 3
  }];

  function saveAddress(e) {
    var a = parseInt($("#address_id").val());
    if (isNaN(a)) {
      return;
    }
    // Post back to save selected Address ID
    return true;
  }

  $('#show_address').autocomplete({
    source: listAddress,
    minLength: 0,
    scroll: true,
    select: function(event, ui) {
      $("#address_id").val(ui.item.id);
      $(this).val(ui.item.label);
      // Trigger any other events or post backs
      return false;
    }
  }).focus(function() {
    $(this).autocomplete("search", "");
    return false;
  }).blur(saveAddress);
});
.hidden {
  opacity: .25;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div class="form-group">
  <!--
  @Html.LabelFor(model => model.allBuildings.First().Address, htmlAttributes: new { @class = "control-label col-md-2" })
  -->
  <label class="control-label col-md-2">Address</label>
  <div class="col-md-10">
    <input type="text" id="show_address" class="form-control" />
    <input type="text" id="address_id" class="hidden form-control" />
    <!--
        @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control", @id = "show_address" } })
        -->
  </div>
</div>

首先,最好解决您的源结构。你可以使用 id ,但是autocomplete正在查找特定对象的数组或数组。

具有标签和值属性的对象数组: [ { label: "Choice1", value: "value1" }, ... ]

如果需要,可以添加其他属性。如果你的数据很简单,我会坚持 label value .

下一步,为了 focus blur ,使用它们的方式将不在自动完成功能范围内,而是绑定到具有输入字段本身的事件。对于自动完成中的焦点,这与列表项的焦点相关。

焦点移动到项目时触发(未选择)。默认操作是将文本字段的值替换为焦点项的值,但前提是事件是由键盘交互触发的。

也不清楚你想做什么。您似乎想将选定的id发回数据库中的某个位置进行更新。我会这么做的 $.post() $.ajax() . 与.net相比,我在php上做的工作更多,所以我不能说这么多,而且您的示例代码并没有真正显示出您想要做什么。

希望能帮上忙/