Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Add sortable rows in ui/table component #5522

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement new sortable_controller.js with sortable.js and `rails/…
…request.js`

The `onEnd` method, sends a PATCH request to update item positions,
using `@rails/request.js` for secure AJAX calls with proper CSRF token 
handling.

https://github.com/SortableJS/Sortable
https://github.com/rails/requestjs-rails
  • Loading branch information
rainerdema committed Nov 29, 2023
commit 0b21c42346c5ce98f7a37ba93c2505735e924d5a
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Controller } from "@hotwired/stimulus"
import Sortable from "sortablejs"
import { patch } from '@rails/request.js'

export default class extends Controller {
static values = {
param: { type: String, default: 'postion' },
handle: { type: String, default: null },
animation: { type: Number, default: 150 },
}

connect() {
this.sortable = Sortable.create(this.element, {
onEnd: this.onEnd.bind(this),
animation: this.animationValue,
handle: this.handleValue,
})
}

async onEnd({ item, newIndex }) {
if (!item.dataset.sortableUrl) return

const data = new FormData()
data.append(this.paramValue, newIndex + 1)

return await patch(item.dataset.sortableUrl, { body: data, responseKind: "js" })
}

disconnect() {
this.sortable.destroy()
this.sortable = null
}
}
2 changes: 2 additions & 0 deletions admin/config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
pin "@hotwired/turbo-rails", to: "turbo.js"

pin "stimulus-use", to: "https://ga.jspm.io/npm:[email protected]/dist/index.js"
pin "sortablejs", to: "https://cdn.jsdelivr.net/npm/[email protected]/+esm"
pin "@rails/request.js", to: "https://cdn.jsdelivr.net/npm/@rails/[email protected]/+esm"

pin "solidus_admin/application", preload: true
pin "solidus_admin/utils"
Expand Down