在函数内部排序脚本无法正常工作

时间:2013-10-09 19:12:24

标签: javascript jquery sorting

我正在尝试将脚本变成一个函数,因为一遍又一遍地重复相同的代码非常烦人。

该剧本选择一个即将举行新手风琴的容器。容器内的每个手风琴都有一个唯一的数据排序ID。 (它需要从1到10排序) 然后它会检测是否有任何比它更小或更大的手风琴,并在它们之间加入。

该脚本无需在函数内部即可正常工作。

没有功能的脚本正常运行

var $container = $('.panel-group[data-group-holder="' + dataPlace + '"]'),
    $d = $container.find('div.panel-default'),
    $n = $('<div class="panel panel-default dragged-true" data-sort="' + dataSort + '"><div class="panel-heading"><h4 class="panel-title"><a class="accordion-toggle sub" data-sort="' + dataSort + '" data-toggle="collapse" href="#main-' + dataSort + '"><div class="remove set"><i class="icon-remove"></i></div>' + dataHtml + ' <b>(<span class="sub-count">0</span>)</b><i class="icon-angle-down"></i></a></h4></div><div id="main-' + dataSort + '" class="panel-collapse in"><div class="panel-body" data-accept="' + dataSort + '"></div></div></div>');
var $a = $d.filter(function () {
    return $(this).data('sort') < dataSort;
}).last();

$(that).parents('div:eq(0)').remove();
/*alert($('.draggable-groups .main-groups[data-sort="' + dataSort + '"]').parents().html());*/

if ($a.length) $a.after($n);
else $container.prepend($n);

此功能无法正常工作(不排序)

    function placeSort(thisContainer, containerFind, placeHtml, returnCompareOneFind, returnCompareOne, returnCompareTwo, removeClone) {
        var $container = $(thisContainer),
            $d = $container.find(containerFind),
            $n = $(placeHtml);

        if(returnCompareOneFind == "false") {
            var $a = $d.filter(function () {
                return $(this).data(returnCompareOne) < returnCompareTwo;
            }).last();
        } else {
            var $a = $d.filter(function () {
                return $(this).find(returnCompareOneFind).data(returnCompareOne) < returnCompareTwo;
            }).last();
        }

        removeClone;

        if ($a.length) $a.after($n);
        else $container.prepend($n);
    }

function addGroupAccordion(that, dataSort, dataPlace, dataHtml) {
    var thisContainer = '.panel-group[data-group-holder="' + dataPlace + '"]';
    var containerFind = 'div.panel-default';
    var placeHtml = '<div class="panel panel-default dragged-true" data-sort="' + dataSort + '"><div class="panel-heading"><h4 class="panel-title"><a class="accordion-toggle sub" data-sort="' + dataSort + '" data-toggle="collapse" href="#main-' + dataSort + '"><div class="remove set"><i class="icon-remove"></i></div>' + dataHtml + ' <b>(<span class="sub-count">0</span>)</b><i class="icon-angle-down"></i></a></h4></div><div id="main-' + dataSort + '" class="panel-collapse in"><div class="panel-body" data-accept="' + dataSort + '"></div></div></div>';
    var returnCompareOneFind = "false";
    var returnCompareOne = 'sort';
    var returnCompareTwo = dataSort;
    var removeClone = $(that).parents('div:eq(0)').remove();

    placeSort(thisContainer, containerFind, placeHtml, returnCompareOne, returnCompareTwo, removeClone);
}

$(document).on('click','.draggable-groups .main-groups',function(){
        var dataSort = $(this).attr("data-sort");
        var dataPlace = $(this).parents('div:eq(1)').attr("data-group");
        var dataHtml = $(this).html();
        var that = $(this);
        addGroupAccordion(that, dataSort, dataPlace, dataHtml);
        var groupHolder = dataPlace;
        countChildren(groupHolder);
    });

HTML

<div class="panel panel-default">
  <div class="panel-heading main">
    <h4 class="panel-title">
      <a class="accordion-toggle collapsed main side-js" data-parent="#main-panel" data-panel="00" data-toggle="collapse" href="#panel-00">
        <span>0</span>Tellija kulud 
        <b>(<span class="cat-count">0</span>)</b>
        <i class="icon-angle-down"></i>
      </a>
    </h4>
  </div>
  <div id="panel-00" class="panel-collapse collapse">
    <div class="panel-body">

      <div class="panel-group" data-group-holder="00">

      </div>

    </div>
  </div>
</div>

<h3>Main groups</h3>
  <div class="row draggable-groups" data-group="00">
    <div class="col-md-6">
      <div class="main-groups" data-sort="01">01</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="02">02</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="03">03</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="04">04</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="05">05</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="06">06</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="07">07</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="08">08</div>
    </div>
  </div>

创建一个jsFiddle会花费太多时间重新创建它。我很抱歉缺少它。

1 个答案:

答案 0 :(得分:2)

您的问题可能归结为尝试传递this

的问题

尝试一下:

function addGroupAccordion(self, dataSort, dataPlace, dataHtml) {
    ...
    var removeClone = $(self).parents('div:eq(0)').remove();
    ...
}

$(document).on('click','.draggable-groups .main-groups',function(){
    ...
    var self = this; // 'self' is more standardized
    addGroupAccordion(self, dataSort, dataPlace, dataHtml);
    ...
});

这里发生的是当您在var that = $(this);函数中设置$().on时,您正在存储包含this的jQuery对象,然后当您尝试在addGroupAccordion中使用它时使用$(that)的{​​{1}}函数,您要求jQuery执行:$($(this))