質問

行と列の両方を並べ替えることができる、HTML ドラッグ アンド ドロップで並べ替え可能なテーブルが欲しいと思ったことはありませんか?それが私にとって死ぬほどのことだとわかっています。ソート可能なリストはたくさんありますが、ソート可能なテーブルを見つけるのは不可能のようです。

script.aculo.us が提供するツールを使用すると、かなり近づけることができることはわかっていますが、それらのツールではいくつかのクロスブラウザーの問題に遭遇しました。

役に立ちましたか?

解決

使ったことがある dhtmlxグリッド 過去に。とりわけ、ドラッグ アンド ドロップの行/列、クライアント側の並べ替え (文字列、整数、日付、カスタム)、およびマルチブラウザのサポートをサポートしています。

コメントへの返信:いいえ、これより良いものは見つかりませんでした。ただそのプロジェクトから離れただけです。:-)

他のヒント

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 に POST されます。この関数 (私の場合は PHP) は、データベース内の商品の順序を更新します。

お勧めします ソート可能jQuery. 。リスト項目やテーブルなどほとんどすべてのものに使用できます。

jQuery はクロスブラウザーに非常にフレンドリーなので、常にお勧めします。

David Heggie の答えが私にとって最も役に立ちました。もう少し簡潔にすることもできます。

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
});

同じマークアップで私にとっては機能します。

ほとんどのフレームワーク (Yui、MooTools、jQuery、Prototype/Scriptaculous など) には、並べ替え可能なリスト機能があります。それぞれについて少し調べて、最もニーズに合ったものを選択してください。

Java を気にしないのであれば、GWT 用の非常に便利なライブラリがあります。 GWT-DND オンラインデモをチェックして、それがどれほど強力であるかを確認してください。

どうでしょうか ソート可能?それはあなたの要件にうまく適合するようです。

使い方はとても簡単です。ソート可能な Javascript ファイルをロードし、ソート可能にしたいテーブルごとに、<table> タグに class="sortable" を適用します。

ほとんどの種類のデータの並べ替え方法はすぐに理解できますが、理解できないものがある場合は、カスタム ソート キーを追加して並べ替え方法を指示できます。ドキュメントではすべてが非常にわかりやすく説明されています。

David Heggie のソリューションで .serialize() が null を返しているのを見つけた場合は、TR の ID 値を単に「1」ではなく「id_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」としてシリアル化されます。

「_」の代わりに「=」、「-」、または「_」を使用できます。「id」以外の単語。

私はJQuery Sortableを使用してこれを実行していますが、あなたが私と同じようにVue.jsを使用している場合に備えて、ここにカスタムVueディレクティブを作成してSortable機能をカプセル化するソリューションがあります。Vueのドラッグ可能は知っていますが、テーブル列を次のように並べ替えません。問題ごとに ここ これを実際に見るには、 これをチェックして

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;
}

パグ テンプレート 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]}}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top