向上prev(),before();向下next(),after()通过<table>表格中的tr进行排序

//触发排序函数时 调用ajax
 $.ajax({
     url: '/add/···',//路径
      type: "POST",
      data: {
          "page": 1,
          "rows": 999999,
          "userId": id
      },
      success: function (data) {
          layer.closeAll("loading");
          if (data.code == 200) {
              let list = data.list;
              if (list.length < 2) {
                  layer.msg("选中层级无排序数据", {icon: 2});
                  return false;
              }
 //str就是弹框要显示的可以排序的表格             
              let str = "<table id='sort_table'style='width: 220px;margin: 10px;'>";
              for (let i = 0; i < list.length; i++) {
                  str += "<tr><td width='60%'>" + list[i].title +
                      "</td><td width='40%' style='text-align: center'>" +
                      "<button type=\"button\" id=\"" + list[i].id + "\" class=\"layui-btn layui-btn-primary layui-btn-xs upup\">上</button>" +
                      "<button type=\"button\" class=\"layui-btn layui-btn-primary layui-btn-xs downdown\">下</button>" +
                      "</td></tr>";
              }
              str += "</table>";
              let layer_open = layer.open({
                  type: 1,
                  title: false,
                  area: '250px',
                  offset: '100px',
                  move: false,
                  btn: ['保存', '取消'],
                  success: function (layero, index) {
                      $(".upup").click(function () {
                          let btn = $(this);
                          let tr = btn.parent("td").parent("tr");
                          let pretr = tr.prev("tr");
                          if (pretr.length == 1) {
                              pretr.before(tr);
                              tr.fadeOut().fadeIn();
                          } else {
                              layer.msg("不能再往上了", {icon: 2});
                          }
                      });
                      $(".downdown").click(function () {
                          let btn = $(this);
                          let tr = btn.parent("td").parent("tr");
                          let nexttr = tr.next("tr");
                          if (nexttr.length == 1) {
                              nexttr.after(tr);
                              tr.fadeOut().fadeIn();
                          } else {
                              layer.msg("不能再往下了", {icon: 2});
                          }
                      });
                  },
                  yes: function (index, layero) {
                      let idlist = "";
                      $(".upup").each(function () {
                          idlist=idlist+","+$(this).attr("id");
                      });
                      layer.load(1);
                      $.ajax({
                          url: 'add/'+idlist.substr(1),
                          type: "post",
                          dataType: "json",
                          data: {
                              token: getToken(),
                          },
                          success: function (data) {
                              if (data.code == 200) {
                                  layer.msg("操作成功!", {icon: 1});
                                  setTimeout(function () {
                                      layer.close(layer_open);
                                      layer.closeAll("loading");
                                  }, 1500);
                                  reBuildTree();//保存后重新渲染好友分组的数据
                              }else {
                                  layer.closeAll("loading");
                                  layer.alert(data.msg, {icon: 2});
                              }
                          },
                          error: function () {
                              layer.closeAll("loading");
                              layer.msg(data.msg, {icon: 2});
                          }
                      });
                  },
                  btn2: function (index, layero) {
                      //点击取消按钮
                      layer.close(layer_open);
                  },
                  content: str,
              });
          } else if (data.code == 401) {
              setTimeout(function () {
                  parent.doLogin();
              }, 1500);
          } else {
              layer.msg(data.msg, {icon: 2});
          }
      },
      error: function (data) {
          layer.closeAll("loading");
          layer.msg(data.msg, {icon: 2});
      }
  });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值