代码之家  ›  专栏  ›  技术社区  ›  JHollanti

HTML拖放可排序表

  •  11
  • JHollanti  · 技术社区  · 16 年前

    是否希望有一个HTML拖放可排序表,您可以在其中对行和列进行排序?我知道这是我要死的东西。有很多可排序的列表,但找一个可排序的表似乎是不可能的。

    我知道您可以非常接近script.aculo.us提供的工具,但我遇到了一些跨浏览器的问题。

    9 回复  |  直到 16 年前
        1
  •  4
  •   msanders    16 年前

    我已经用过 dhtmlxGrid 在过去。它还支持拖放行/列、客户端排序(字符串、整数、日期、自定义)和多浏览器支持。

    评论回复: 不,没有发现更好的-刚从那个项目开始。-)

        2
  •  19
  •   SilverM-A Ravi Makwana    12 年前

    我使用了jquery ui的可排序插件,效果很好。与此类似的标记:

    <table id="myTable">
    <thead>
    <tr><th>ID</th><th>Name</th><th>Details</th></tr>
    </thead>
    <tbody class="sort">
    <tr id="1"><td>1</td><td>Name1</td><td>Details1</td></tr>
    <tr id="2"><td>2</td><td>Name1</td><td>Details2</td></tr>
    <tr id="3"><td>3</td><td>Name1</td><td>Details3</td></tr>
    <tr id="4"><td>4</td><td>Name1</td><td>Details4</td></tr>
    </tbody>
    </table>
    

    然后在javascript中

    $('.sort').sortable({
        cursor: 'move',
        axis:   'y',
        update: function(e, ui) {
            href = '/myReorderFunctionURL/';
            $(this).sortable("refresh");
            sorted = $(this).sortable("serialize", 'id');
            $.ajax({
                type:   'POST',
                url:    href,
                data:   sorted,
                success: function(msg) {
                    //do something with the sorted data
                }
            });
        }
    });
    

    这会将项目ID的序列化版本发布到给定的URL。然后这个函数(在我的例子中是php)更新数据库中项目的顺序。

        3
  •  5
  •   Neall    16 年前

    我推荐 Sortables 在里面 jQuery . 您可以在列表项或几乎任何东西上使用它,包括表。

    jquery非常适合跨浏览器,我一直推荐它。

        4
  •  3
  •   elise    16 年前

    大卫·赫吉的回答对我来说是最有用的。它可以稍微简洁一点:

    var sort = function(event, ui) {
      var url = "/myReorderFunctionURL/" + $(this).sortable('serialize');
      $.post(url, null,null,"script");  // sortable("refresh") is automatic
    }
    
    $(".sort").sortable({
      cursor: 'move',
      axis: 'y',
      stop: sort
    });
    

    适用于我,具有相同的标记。

        5
  •  1
  •   Anutron    16 年前

    大多数框架(yui、mootools、jquery、prototype/scriptaculous等)都具有可排序列表功能。对每一个都做一点调查,挑选一个最适合你需要的。

        6
  •  0
  •   graham.reeds    16 年前

    如果你不介意Java,那么GWT有一个非常方便的库。 GWT-DND 查看在线演示,看看它有多强大。

        7
  •  0
  •   Don Jones    16 年前

    怎么样 sorttable ?这似乎很符合你的要求。

    这很容易使用-加载SortTable JavaScript文件,然后,对于要使其可排序的每个表,将class=“Sortable”应用于<table>标记。

    它将立即了解如何对大多数类型的数据进行排序,但如果有一些数据没有排序,您可以添加一个自定义排序键来告诉它如何排序。文档解释得很好。

        8
  •  0
  •   CDR    12 年前

    如果在DavidHeggie的解决方案中找到.serialize()返回空值,则将trs的id值设置为“id_1”,而不是简单的“1”

    例子:

    <tr id="id_1"><td>1</td><td>Name1</td><td>Details1</td></tr>
    <tr id="id_2"><td>2</td><td>Name1</td><td>Details2</td></tr>
    <tr id="id_3"><td>3</td><td>Name1</td><td>Details3</td></tr>
    <tr id="id_4"><td>4</td><td>Name1</td><td>Details4</td></tr>
    

    以上将序列化为“id[]=1&id[]=2&id[]=3”

    您可以使用“=”、“-”或“u”而不是“u”。 除了“身份证”还有其他任何词。

        9
  •  0
  •   PirateApp    6 年前

    我正在使用jquery sortable来实现这一点,但如果您像我一样使用vue.js,这里有一个创建自定义vue指令来封装可排序功能的解决方案,我知道vue是可拖动的,但它不会根据问题对表列进行排序。 HERE 要看到这一点, CHECK THIS

    JS码

    Vue.directive("draggable", {
      //adapted from https://codepen.io/kminek/pen/pEdmoo
      inserted: function(el, binding, a) {
        Sortable.create(el, {
          draggable: ".draggable",
          onEnd: function(e) {
            /* vnode.context is the context vue instance: "This is not documented as it's not encouraged to manipulate the vm from directives in Vue 2.0 - instead, directives should be used for low-level DOM manipulation, and higher-level stuff should be solved with components instead. But you can do this if some usecase needs this. */
            // fixme: can this be reworked to use a component?
            // https://github.com/vuejs/vue/issues/4065
            // https://forum.vuejs.org/t/how-can-i-access-the-vm-from-a-custom-directive-in-2-0/2548/3
            // https://github.com/vuejs/vue/issues/2873 "directive interface change"
            // `binding.expression` should be the name of your array from vm.data
            // set the expression like v-draggable="items"
    
            var clonedItems = a.context[binding.expression].filter(function(item) {
              return item;
            });
            clonedItems.splice(e.newIndex, 0, clonedItems.splice(e.oldIndex, 1)[0]);
            a.context[binding.expression] = [];
            Vue.nextTick(function() {
              a.context[binding.expression] = clonedItems;
            });
    
          }
        });
      }
    });
    
    const cols = [
      {name: "One", id: "one", canMove: false},
      {name: "Two", id: "two", canMove: true},
      {name: "Three", id: "three", canMove: true},
      {name: "Four", id: "four", canMove: true},
    ]
    
    const rows = [
      {one: "Hi there", two: "I am so excited to test", three: "this column that actually drags and replaces", four: "another column in its place only if both can move"},
      {one: "Hi", two: "I", three: "am", four: "two"},
      {one: "Hi", two: "I", three: "am", four: "three"},
      {one: "Hi", two: "I", three: "am", four: "four"},
      {one: "Hi", two: "I", three: "am", four: "five"},
      {one: "Hi", two: "I", three: "am", four: "six"},
      {one: "Hi", two: "I", three: "am", four: "seven"}
    ]
    
    Vue.component("datatable", {
      template: "#datatable",
      data() {
        return {
          cols: cols,
          rows: rows
        }
      }
    })
    
    new Vue({
      el: "#app"
    })
    

    CSS

    .draggable {
      cursor: move;
    }
    
    table.table tbody td {
      white-space: nowrap;
    }
    

    PUG模板HTML

    #app
      datatable
    
    script(type="text/x-template" id="datatable")
      table.table
        thead(v-draggable="cols")
          template(v-for="c in cols")
            th(:class="{draggable: c.canMove}")
              b-dropdown#ddown1.m-md-2(:text='c.name')
                b-dropdown-item First Action
                b-dropdown-item Second Action
                b-dropdown-item Third Action
                b-dropdown-divider
                b-dropdown-item Something else here...
                b-dropdown-item(disabled='') Disabled action
    
        tbody
          template(v-for="row in rows")
            tr
              template(v-for="(col, index) in cols")
                td {{row[col.id]}}