Giter VIP home page Giter VIP logo

sorting's Introduction

Sorting

Sorting adds reordering abilities to GORM models and sorts collections.

GoDoc

Register GORM Callbacks

Sorting utilises GORM callbacks to log data, so you will need to register callbacks first:

import (
  "github.com/jinzhu/gorm"
  "github.com/qor/sorting"
)

func main() {
  db, err := gorm.Open("sqlite3", "demo_db")
  sorting.RegisterCallbacks(db)
}

Sort Modes

Sorting two modes which can be applied as anonymous fields in a model.

  • Ascending mode:smallest first (sorting.Sorting)
  • Descending mode: smallest last (sorting.SortingDESC)

They can be used as follows:

// Ascending mode
type Category struct {
  gorm.Model
  sorting.Sorting // this will register a `position` column to model Category, used to save record's order
}

db.Find(&categories)
// SELECT * FROM categories ORDER BY position;

// Descending mode
type Product struct {
  gorm.Model
  sorting.SortingDESC // this will register a `position` column to model Product, used to save record's order
}

db.Find(&products)
// SELECT * FROM products ORDER BY position DESC;

Reordering

// Move Up
sorting.MoveUp(&db, &product, 1)
// If a record is in positon 5, it will be brought to 4

// Move Down
sorting.MoveDown(&db, &product, 1)
// If a record is in positon 5, it will be brought to 6

// Move To
sorting.MoveTo(&db, &product, 1)
// If a record is in positon 5, it will be brought to 1

Sorting Collections

Sorts a slice of data:

sorter := sorting.SortableCollection{
  PrimaryKeys: []string{"5", "3", "1", "2"}
}

products := []Product{
  {Model: gorm.Model{ID: 1}, Code: "1"},
  {Model: gorm.Model{ID: 2}, Code: "2"},
  {Model: gorm.Model{ID: 3}, Code: "3"},
  {Model: gorm.Model{ID: 3}, Code: "4"},
  {Model: gorm.Model{ID: 3}, Code: "5"},
}

sorter.Sort(products)

products // => []Product{
         //      {Model: gorm.Model{ID: 3}, Code: "5"},
         //      {Model: gorm.Model{ID: 3}, Code: "3"},
         //      {Model: gorm.Model{ID: 1}, Code: "1"},
         //      {Model: gorm.Model{ID: 2}, Code: "2"},
         //      {Model: gorm.Model{ID: 3}, Code: "4"},
         //    }

Sorting GORM-backend Models

After enabling sorting modes for GORM models, QOR Admin will automatically enable the sorting feature for the resource.

Sorting Demo with QOR

Sorting Collections

If you want to make a sortable select_many, collection_edit field, You could add a sorting.SortableCollection field with name: Field's name + 'Sorter'; which is used to save above field's data order. That Field will also be identified as sortable in QOR Admin.

// For model relations
type Product struct {
  gorm.Model
  l10n.Locale
  Collections           []Collection
  CollectionsSorter     sorting.SortableCollection
  ColorVariations       []ColorVariation `l10n:"sync"`
  ColorVariationsSorter sorting.SortableCollection
}

// For virtual arguments
type selectedProductsArgument struct {
  Products       []string
  ProductsSorter sorting.SortableCollection
}

selectedProductsResource := Admin.NewResource(&selectedProductsArgument{})
selectedProductsResource.Meta(&admin.Meta{Name: "Products", Type: "select_many", Collection: func(value interface{}, context *qor.Context) [][]string {
  var collectionValues [][]string
  var products []*models.Product
  db.DB.Find(&products)
  for _, product := range products {
    collectionValues = append(collectionValues, []string{fmt.Sprint(product.ID), product.Name})
  }
  return collectionValues
}})

Widgets.RegisterWidget(&widget.Widget{
  Name:      "Products",
  Templates: []string{"products"},
  Setting:   selectedProductsResource,
}

License

Released under the MIT License.

sorting's People

Contributors

jinzhu avatar jasonweng avatar binku87 avatar azumads avatar raven-chen avatar betrok avatar bom-d-van avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.