0

I have some select fields that set for example the vendor-id and status for my datatable. When these field change, I want to include these parameters in a link (the link calls a server function that exports the data to csv). My experience with jquery/coffeescript is very limited and I'm having trouble getting this to work. What I have so far is:

$ ->
  $('#vendor-id').on 'change', ->
    $('#shipments-table').DataTable().ajax.reload()
    $('a.download-link').attr 'href', (index, href) ->
      param = 'vendor-id='+$('#vendor-id').val();
      if href.charAt(href.length - 1) == '?'
        href + param
      else if href.indexOf('?') > 0
        if href.indexOf(param) == 0
          href + '&' + param
      else
        href + '?' + param
    return

I have two problems with my code. One is that for some reason, when I change the value, the param is added to the link twice (example: http://link.com?vendor-id=1&vendor-id=1 )

The second problem is that when I select a different value for this select box, the previous value is not removed from the link but added on to it.

How can I fix this?

boydenhartog
  • 752
  • 2
  • 9
  • 22

1 Answers1

1

You can find some answers here: Change URL parameters. For example: https://stackoverflow.com/a/10997390/4335261 - it worked for me. Just copy updateURLParameter function and use it this way:

$ ->
   $('#vendor-id').on 'change', ->
   $('#shipments-table').DataTable().ajax.reload()
   $('a.download-link').attr 'href', (index, href) ->
     updateURLParameter(href, 'vandor-id', $('#vendor-id').val())
Community
  • 1
  • 1
Szymon
  • 242
  • 1
  • 9
  • This isn't working for me. (I fixed the 'vandor-id' typo). The link doesn't change. Does the parameter need to be present in order for it to be changed? It is not present by default but added on after the value is set – boydenhartog Jul 28 '16 at 14:07
  • Woops. After I read the link I included the function and it's working perfectly. Cheers mate!!!! – boydenhartog Jul 28 '16 at 14:09