Home > Web Front-end > JS Tutorial > body text

How jQuery implements move up and move down based on sorting function

高洛峰
Release: 2016-12-05 10:55:06
Original
2638 people have browsed it

The example in this article describes jQuery’s method of moving up and down based on the sorting function. Share it with everyone for your reference, the details are as follows:

effect

How jQuery implements move up and move down based on sorting function

idea,

swap sort with adjacent elements.

The premise is that each element has its own sort value, which is not zero.

<tr id="{sh:$vo.id}">
  <td>
    <span onclick="up(this);" class="glyphicon glyphicon-arrow-up text-danger up" style="cursor: pointer;" title="上移" aria-hidden="true"></span>
       
    <span onclick="down(this);" class="glyphicon glyphicon-arrow-down text-danger down" style="cursor: pointer;" title="下移" aria-hidden="true"></span>
  </td>
  <td>
    <span title="{sh:$vo.user_id}">{sh:$vo.store_name}</span>
  </td>
  <td class="center"><a href="{sh:$vo.logo}" target="_blank"><img  src="{sh:$vo.logo}"    style="max-width:90%"How jQuery implements move up and move down based on sorting function" ></td>
  <td class="center">{sh:$vo.category_name}</td>
  <td class="center edit">
    <a val="{sh:$vo.store_id}" onclick="view(this);" class="view btn btn-success" href="javascript:void(0);" title="查看">
      <i class="halflings-icon white zoom-in"></i>
    </a>
  </td>
</tr>
Copy after login

Click to trigger the up method and down method.

Get the current id.

Get adjacent elements through jQuery.

// 上移
function up(obj){
  var $tr = $(obj).parents("tr");
  if ($tr.index() != 0) {
    var current_id  = $tr.attr(&#39;id&#39;);
    var exchange_id  = $tr.prev("tr").attr(&#39;id&#39;);
    $.ajax({
      url: &#39;{sh::U("Mall/ajax","todo=exchange_sort")}&#39;,
      type: &#39;POST&#39;,
      data: &#39;current_id=&#39;+current_id+&#39;&exchange_id=&#39;+exchange_id,
      success:function(json) {
        if (json == 1) {
          $tr.fadeOut().fadeIn();
          $tr.prev().before($tr);
          layer.msg(&#39;上移成功&#39;, {icon: 1});
        } else {
          layer.msg(&#39;上移失败&#39;, {icon: 2});
        }
      }
    });
  }
}
// 下移
function down(obj) {
  var len = $(".down").length;
  var $tr = $(obj).parents("tr");
    if ($tr.index() != len - 1) {
      var current_id  = $tr.attr(&#39;id&#39;);
      var exchange_id  = $tr.next("tr").attr(&#39;id&#39;);
      $.ajax({
        url: &#39;{sh::U("Mall/ajax","todo=exchange_sort")}&#39;,
        type: &#39;POST&#39;,
        data: &#39;current_id=&#39;+current_id+&#39;&exchange_id=&#39;+exchange_id,
        success:function(json) {
          if (json == 1) {
            $tr.fadeOut().fadeIn();
            $tr.next().after($tr);
            layer.msg(&#39;下移成功&#39;, {icon: 1});
          } else {
            layer.msg(&#39;下移失败&#39;, {icon: 2});
          }
        }
      });
  }
}
Copy after login

Several jQuery methods are used here, prev(), next(), before(), after(). And effects, fadeOut(), fadeIn(). And some simple logical judgments and skills.

php background processing,

case &#39;exchange_sort&#39;:
$mallShopModel = M(&#39;Mall_shop&#39;);
$current_id  = $this->_post(&#39;current_id&#39;,&#39;trim&#39;);
$exchange_id  = $this->_post(&#39;exchange_id&#39;,&#39;trim&#39;);
$current_sort = $mallShopModel->where(array(&#39;id&#39;=>$exchange_id))->getField(&#39;sort&#39;);
$exchange_sort = $mallShopModel->where(array(&#39;id&#39;=>$current_id))->getField(&#39;sort&#39;);
$cdata[&#39;id&#39;]  = $current_id;
$cdata[&#39;sort&#39;] = $current_sort;
$cres     = $mallShopModel->save($cdata);
$edata[&#39;id&#39;]  = $exchange_id;
$edata[&#39;sort&#39;]  = $exchange_sort;
$eres     = $mallShopModel->save($edata);
if ($cres !== FALSE && $eres !== FALSE){
  exit(&#39;1&#39;);
} else {
  exit(&#39;2&#39;);
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template