Smart-Table icon indicating copy to clipboard operation
Smart-Table copied to clipboard

CurrentPage and QueryString Integration

Open JanStevens opened this issue 10 years ago • 2 comments
trafficstars

Hello,

I use smart-table with a rails backend, in the Rails world ransack is a very populair extension for doing searches in the database. For this we typically need to construct a search string (q) that indicates what fields to sort on.

Since I almost always use the custom Pipe function I end up doing two things on a table state:

  • Calculating my search string q from the tableState.search
  • Calculating the currentPage from the tableState.pagination so I can request a new page

After 4 tables I came up with the following for extending the stPipeDirective (note I use coffeescript)

# We decorate the stPipeDirective from smart table so we can
# add the current page and the q search string, this is only done
# when we define our own pipe function, we now can easily search and paginate
#
@app.config ['$provide', ($provide) ->
  $provide.decorator 'stPipeDirective', ($delegate) ->
    directive = $delegate[0];
    toQueryStringFilter = (tableState) ->
      sort = {}
      q = {}

      if tableState.sort.predicate
        order = if tableState.sort.reverse then 'desc' else 'asc'
        sort = {s: "#{tableState.sort.predicate} #{order}"}

      search = tableState.search.predicateObject || {}
      angular.extend(search, sort)
      decodeURIComponent($.param(q: search))

    angular.extend(directive.link, {
      pre: (scope, element, attrs, ctrl) ->
        if angular.isFunction(scope.stPipe)
          ctrl.preventPipeOnWatch()
          ctrl.pipe = ->
            tableState = ctrl.tableState()
            tableState.search.q = toQueryStringFilter(tableState)
            tableState.pagination.current = Math.floor(tableState.pagination.start / tableState.pagination.number) + 1 || 1
            scope.stPipe(tableState, ctrl)
    })
    $delegate
]

I think the ransack integration is a bit to custom for smart table but the current page should be provided not? Anyway now people can use it with Rails and Ransack. Can we start a wiki with helpful custom extensions people come up with?

Thanks for the awesome plugin!

JanStevens avatar Mar 04 '15 21:03 JanStevens

the wiki seems a good idea ! Thanks for sharing

lorenzofox3 avatar Mar 05 '15 02:03 lorenzofox3

Note you can set up a plunker, and submit a pull request on the documentation website with your example

lorenzofox3 avatar Mar 10 '15 05:03 lorenzofox3