【问题标题】:How do I simultaneously convert multiple text cells in table into styled dropdowns?如何同时将表格中的多个文本单元格转换为样式下拉列表?
【发布时间】:2017-01-22 15:39:06
【问题描述】:

我有一个插件 (https://github.com/olivM/jQuery-Selectbox) 填充选择下拉菜单并设置下拉菜单的样式。

鉴于以下方法我无法进行分离,请通过我的代码中的偶数处理程序将元素修改附加到下拉列表中。

我只能在 firebug 检查器中手动执行此操作。

$('#elem').selectbox('detach');
$('#elem').selectbox('attach');

我需要做什么来分离然后将选择框附加到 html 下拉列表?

Method      Description
attach  .selectbox("attach")    Attach the select box to a jQuery selection. This will hide the element and create its custom replacement.
change  .selectbox("change")    Change selected attribute of the selectbox.
close   .selectbox("close")     Close opened selectbox.
detach  .selectbox("detach")    Remove the selectbox functionality completely. This will return the element back to its pre-init state.
disable .selectbox("disable")   Disable the selectbox.
enable  .selectbox("enable")    Enable the selectbox.
open    .selectbox("open")  Call up attached selectbox.
option  .selectbox("option", options)   Get or set any selectbox option. If no value is specified, will act as a getter.

【问题讨论】:

标签: jquery html


【解决方案1】:

我随后添加的对库的更新解决了该问题。

this._defaults 中我添加了 3 行(onBind、onInit、onDetach)

function Selectbox() {
        this._state = [];
        this._defaults = { // Global defaults for all the select box instances
            classHolder: "sbHolder",
            classHolderDisabled: "sbHolderDisabled",
            classSelector: "sbSelector",
            classOptions: "sbOptions",
            classGroup: "sbGroup",
            classSub: "sbSub",
            classDisabled: "sbDisabled",
            classToggleOpen: "sbToggleOpen",
            classToggle: "sbToggle",
            classFocus: "sbFocus",
            speed: 200,
            effect: "slide", // "slide" or "fade"
            onChange: null, //Define a callback function when the selectbox is changed
            onOpen: null, //Define a callback function when the selectbox is open
            onClose: null, //Define a callback function when the selectbox is closed

            onBind: null,
            onInit: null,
            onDetach: null
        };
}

在+中添加行

+      self._initSelectBox(target);
       sbSelector.appendTo(sbHolder);
       sbOptions.appendTo(sbHolder);
       sbHolder.insertAfter($target);

       $("html").on('mousedown', function(e){
                e.stopPropagation();
                $("select").selectbox('close');
       });
       $([".", inst.settings.classHolder, ", ,", inst.settings.classSelector].join("")).mousedown(function(e){
                e.stopPropagation;
       });
+      self._bindSelectbox(target);

在 _detachSelectbox 内添加了以下内容

var onDetach = this._get(inst, 'onDetach');
if (onDetach != null)
{
     onDetach.apply({inst.input ? inst.input[0]: null), [inst]);
}

正上方

$("#sbHolder_" + inst.uid).remove();
$.data(target, PROP_NAME, null);
$(target).show();

然后我添加了初始化和绑定方法

 _initSelectbox: funciton (target){
    var onInit, inst = this._getInst(target);
    if (inst) {
       onInit = this._get(inst, 'onInit');
       if (onInit != null)
       {
         onInit.apply({inst.input ? inst.input[0]: null), [inst]);
       }
    }
 }
  _bindSelectbox: funciton (target){
    var onBind, inst = this._getInst(target);
    if (inst) {
       onBind = this._get(inst, 'onBind');
       if (onBind != null)
       {
         onBind.apply({inst.input ? inst.input[0]: null), [inst]);
       }
    }
 }

调用代码(AngularJS)

function bindDrodown(selector)
{
    angular.forEach($(".selector").function(elem){
       //set span value to selected value of dropdown
       id = $(elem).attr("id");
       itemid = $("#"+id).data("itemid");
       setDrowDownValue(elem);
       setSpanValue(itemid);
    });

   $(selector).selectbox({
       onInit: function (inst){
       },
       onBind: function (inst){
       },
       onChange: function (id, inst){
           //change code here     
       },
       onDetach: function(inst){
       }
   });
}

【讨论】:

    【解决方案2】:

    我的问题不在于代码未执行,而在于获取数据的异步性质。

    添加超时解决了这个问题。

      setTimeout(function(){
                  $('#elem').selectbox("detach");
                  $('#elem').selectbox("attach");
                  }, 10);
    

    HTML 脚本

     $(function () {
              $('#elem').selectbox("attach");
            });
    

    获取下拉代码 - 位于角度控制器中

    $http.get( 'some json resource').success(function(data){
                $scope.States = data;
                 $scope.abbreviation = data[0].abbreviation;
    
                setTimeout(function(){ 
                  $('#elem').val($scope.abbreviation);
                  $('#elem').selectbox("detach");
                  $('#elem').selectbox("attach");
                }, 10);
    
          });
    

    【讨论】:

    • 为什么不为您的下拉列表设置回调,并在下拉列表填充后执行此操作?我认为最好远离超时时间
    • 感谢您的建议。就我而言,您是否建议我修改库以具有新的回调?如果可能的话,你可以提供一个例子吗?
    • 我不太确定你的程序结构,所以很难说 - 如果你能提供一些上下文并显示你试图在你的程序中执行 attachdetach会有帮助的
    • 我用更多代码修改了我的答案。如果有帮助,请告诉我。
    • 啊,好的!所以我建议不要使用$.get() 包装器,而应该只使用$.ajax(),然后你有你的success 函数以及一个complete 函数来解决这个问题。看看这里 - stackoverflow.com/a/23283596/1887101
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2021-03-22
    • 2020-10-20
    • 1970-01-01
    • 2015-04-18
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多