@@ -25,14 +25,11 @@ func GetPagingParams(c *gin.Context) (page, offset, pageSize int) {
2525}
2626
2727func (c * Ctx [T ]) combineStdSelectorRequest () {
28- var StdSelectorInitParams struct {
29- ID []int `json:"id"`
30- }
28+ StdSelectorInitID := c .ctx .QueryArray ("id[]" )
3129
32- _ = c .ctx .ShouldBindJSON (& StdSelectorInitParams )
33- if len (StdSelectorInitParams .ID ) > 0 {
30+ if len (StdSelectorInitID ) > 0 {
3431 c .GormScope (func (tx * gorm.DB ) * gorm.DB {
35- return tx .Where (c .itemKey + " IN ?" , StdSelectorInitParams . ID )
32+ return tx .Where (c .itemKey + " IN ?" , StdSelectorInitID )
3633 })
3734 }
3835}
@@ -62,6 +59,9 @@ func (c *Ctx[T]) result() (*gorm.DB, bool) {
6259 }
6360
6461 result = result .Model (& dbModel )
62+ if c .table != "" {
63+ result = result .Table (c .table , c .tableArgs ... )
64+ }
6565
6666 c .combineStdSelectorRequest ()
6767
@@ -72,16 +72,30 @@ func (c *Ctx[T]) result() (*gorm.DB, bool) {
7272 return result , true
7373}
7474
75- func (c * Ctx [T ]) ListAllData () ([] * T , bool ) {
75+ func (c * Ctx [T ]) ListAllData () (data any , ok bool ) {
7676 result , ok := c .result ()
7777 if ! ok {
7878 return nil , false
7979 }
8080
8181 result = result .Scopes (c .SortOrder ())
82- models := make ([]* T , 0 )
83- result .Find (& models )
84- return models , true
82+ if c .scan == nil {
83+ models := make ([]* T , 0 )
84+ result .Find (& models )
85+
86+ if c .transformer != nil {
87+ transformed := make ([]any , 0 )
88+ for k := range models {
89+ transformed = append (transformed , c .transformer (models [k ]))
90+ }
91+ data = transformed
92+ } else {
93+ data = models
94+ }
95+ } else {
96+ data = c .scan (result )
97+ }
98+ return data , true
8599}
86100
87101func (c * Ctx [T ]) PagingListData () (* model.DataList , bool ) {
@@ -90,11 +104,11 @@ func (c *Ctx[T]) PagingListData() (*model.DataList, bool) {
90104 return nil , false
91105 }
92106
93- result = result .Scopes (c .OrderAndPaginate ())
107+ scopesResult : = result . Session ( & gorm. Session {}) .Scopes (c .OrderAndPaginate ())
94108 data := & model.DataList {}
95109 if c .scan == nil {
96110 models := make ([]* T , 0 )
97- result .Find (& models )
111+ scopesResult .Find (& models )
98112
99113 if c .transformer != nil {
100114 transformed := make ([]any , 0 )
@@ -106,9 +120,12 @@ func (c *Ctx[T]) PagingListData() (*model.DataList, bool) {
106120 data .Data = models
107121 }
108122 } else {
109- data .Data = c .scan (result )
123+ data .Data = c .scan (scopesResult )
110124 }
111125
126+ var totalRecords int64
127+ result .Session (& gorm.Session {}).Count (& totalRecords )
128+
112129 page := cast .ToInt (c .ctx .Query ("page" ))
113130 if page == 0 {
114131 page = 1
@@ -119,9 +136,6 @@ func (c *Ctx[T]) PagingListData() (*model.DataList, bool) {
119136 pageSize = cast .ToInt (reqPageSize )
120137 }
121138
122- var totalRecords int64
123- result .Session (& gorm.Session {}).Count (& totalRecords )
124-
125139 data .Pagination = model.Pagination {
126140 Total : totalRecords ,
127141 PerPage : pageSize ,
@@ -137,3 +151,15 @@ func (c *Ctx[T]) PagingList() {
137151 c .ctx .JSON (http .StatusOK , data )
138152 }
139153}
154+
155+ // EmptyPagingList return empty list
156+ func (c * Ctx [T ]) EmptyPagingList () {
157+ pageSize := settings .ServerSettings .PageSize
158+ if reqPageSize := c .ctx .Query ("page_size" ); reqPageSize != "" {
159+ pageSize = cast .ToInt (reqPageSize )
160+ }
161+
162+ data := & model.DataList {Data : make ([]any , 0 )}
163+ data .Pagination .PerPage = pageSize
164+ c .ctx .JSON (http .StatusOK , data )
165+ }
0 commit comments