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

Feature request: State/Province Abbreviations #2

Closed
iapparatus opened this issue Dec 2, 2014 · 27 comments
Closed

Feature request: State/Province Abbreviations #2

iapparatus opened this issue Dec 2, 2014 · 27 comments

Comments

@iapparatus
Copy link

Thank you for the great script. One feature that would be very useful is to be able to optionally use state/province 2-letter abbreviations as values in the region selector.

@benkeen
Copy link
Member

benkeen commented Feb 24, 2015

Agreed, that would be very nice. Good suggestion.

@nozpheratu
Copy link

👍

@benkeen
Copy link
Member

benkeen commented Apr 10, 2015

Problem about this one is that I don't think all country regions are able to be reduced to a 2-char abbreviations. But perhaps just an "abbreviate" option that limits them to whatever abbreviation is appropriate, according to the country.

@nozpheratu
Copy link

@benkeen Sounds like a sound idea to me, or perhaps 'alpha-2-code' or 'code' option would be a bit more explicit.

@sventech
Copy link
Contributor

sventech commented May 3, 2015

Do you have a plan for how to approach this? We could get a spreadsheet of region codes for each country perhaps... Or use the wiki.

@benkeen
Copy link
Member

benkeen commented May 4, 2015

How about this? https://github.com/benkeen/country-region-selector/blob/master/source/data-with-abbrev.js#L47

It's super simple: right now the | separator is used to delimit the list of regions in a country; I just added a secondary ~ delimiter to delimit the region and the region abbreviation. That'll keep the data set as small as possible and work for any country.

If people are willing to do the (tedious!) task of updating that file, I can update the code to support it...

[I can help out, too - but it may take some time for me to get to it].

@sventech
Copy link
Contributor

sventech commented May 4, 2015

Works great for me! I care most about US/AU/NZ/GB/BR/NO and a couple others, so I should be able to track down the data, then I'll fill in any others I can find easily. Hopefully @iapparatus will contribute some codes too and we can fill it out quickly. 👍

@benkeen
Copy link
Member

benkeen commented May 4, 2015

Nice, thanks @sventech!

@sventech
Copy link
Contributor

The regions we have for American Samoa are those from the tourist info, but the Wikipedia page lists these counties:
Ituau~01|Ma'oputasi~02|Sa'Ole~03|Sua~04|Vaifanua~05|Alataua~06|Fofo~07|Leasina~08|Tualatai~09|Tualauta~10|Faleasao~11|Fitiuta~12|Ofu~13|Olosega~14|Ta'ū~15|Rose Atoll~21|Swains Island~22

@sQu1rr
Copy link

sQu1rr commented Jun 19, 2015

you can hook up data from my fork guys, i changed some core functionality (defaulting to abbr data and such) and the regions/countries are a bit different, more ISOish i guess, so i didn't want to create a pull request. I will be using my modified version for my own needs - so feel free to pull my changes if you need to.
https://github.com/sQu1rr/country-region-selector/blob/master/source/data-with-abbrev.js

@wobblymusic
Copy link

This will be a great enhancement. Has code been written to support it? All I see is the data.

@sventech
Copy link
Contributor

@wobblymusic I think Ben is planning to implement it when the data is finished. He designed the data format to be handled by a trivial mod to his code, from what I understand. You're welcome to contribute -- you can just copy the data from sQu1rr / Aleksandr and make sure it is correctly formatted. There is a list of what countries remain to be done at the top of the file.

benkeen pushed a commit that referenced this issue Oct 15, 2015
update to latest upstream version
@benkeen
Copy link
Member

benkeen commented Oct 16, 2015

Thanks for everyone whose chimed in - and for the pull requests! :) Clearly there's interest in this feature, so I'll work on this shortly, even though all the data isn't fully complete.

  • I'll add a big proviso saying "currently only supports X,Y,Z countries" which we'll update as the data gets added.
  • if the shortcode region doesn't exist for a country but the user requests it, output a console.warn() saying as much and downgrade to the full region name.
  • I'll add a whitelist/blacklist option to let you specify which countries appear in the country dropdowns. So you could choose only countries that had region abbrevs (if you wished)

I think between these, it'll be okay to implement now without the full data set.

Once this is in, I'll also try to devote a little time to adding in some as well. Be nice to get this sucker all wrapped up!

@wobblymusic
Copy link

That would be great. The second bullet, in particular, is how I imagined the feature would work.

@benkeen
Copy link
Member

benkeen commented Oct 19, 2015

Hi all, I've just added this and released 0.2.0.

Couple of quick comments:

Let me know if anyone spots any problems! I'll leave this issue open a little while before closing it.

@sventech
Copy link
Contributor

Norway's listing has the name and abbreviation twice. Probably my fault. I've created a pull request.

Thanks for implementing the feature!

@wobblymusic
Copy link

This is working great, Thank you so much! One small change that should probably be made to the README file. If you are using the data-value="shortcode" option, then the data-default-value for region needs to be the abbreviation, not the full name. So the data-default-value definition should read something like this:

data-default-value - optional. The default selected value in the region dropdown (e.g. "British Columbia", or "BC" if using the data-value="shortcode" option)

I apologizing for not implementing this myself, but I'm not very git-literate (I'm working on it!).

@benkeen
Copy link
Member

benkeen commented Oct 20, 2015

Ah, good call! I'll make a note to update the doc tonight.

@wobblymusic
Copy link

Since this new version with the region abbreviations was posted, I'm having trouble adding a customization I had working in the previous version. I'm using this in conjuction with another function that requires the 2 letter country code as a parameter. When the country under crs is changed, I want to populate the region fields, but I also want to fire this other script. In the older version of crs, I put my function within $(this).on("change", function() {_populateRegionFields... and it worked perfectly. But when I try to do this in the new version it doesn't fire. Even an alert() isn't triggered, although it was in the older version. What am I missing?

Relatedly, I stupidly didn't save my customized old version and now I can't figure out how to grab the 2 letter country code for the selected country. Can someone help me with that as well? I don't want to use the country shortcode option when I trigger crs; I just want to grab the selected country shortcode from within the crs script when the selected country changes.

Thanks.

@wobblymusic
Copy link

To be more clear, if I change the code,

$(this).on("change", function() {
_populateRegionFields(countryElement, regionElement);
});

to

$(this).on("change", function() {
_populateRegionFields(countryElement, regionElement);
alert('Hello!');
});

The list of regions does correctly change when the country changes, but the alert doesn't fire. It did fire under the older version from last week.

@benkeen
Copy link
Member

benkeen commented Oct 23, 2015

Hey @wobblymusic! Hmm... I can't think of anything that would affect that change. But do you need to modify the script at all? Might be better to just add your own JS so it won't get overwritten with future upgrades.

e.g. just add some JS in your page like $(".your-selector-here").on("change", function() { ... });. That should work... I'm not canceling the event, so it should get called on the onChange event. There, you could do whatever you want.

To get the value with jQuery, you can do this:

$(".your-selector-here").on("change", function() { 
   console.log("my value is: ", this.value);
   console.log("or using jQuery... ", $(this).val());
});

Hope this helps a bit.

@wobblymusic
Copy link

Hi Ben. The problem is that I want to grab the two letter country abbreviation for my other function, but I want crs to give me the full country name in its selector value. How can I grab the two-letter abbreviation outside of crs.js when it's not $(this).val() ?

on edit: I realize I could create my own variable of country names and abbreviations, but that seems wrong somehow.

@benkeen
Copy link
Member

benkeen commented Oct 24, 2015

Ah, I see... Drat. Yes, you're kind of stuck with hacking it in that case. Maybe the least unobtrusive hack would be to edit it to output an extra data-shortcode attribute on the options. Then you could do your own JS to just extract that value...

<option value="full region name here" data-shortcode="shortcode here">full region name here</option>

If you're really stuck, email me what you have and I can see if I can edit it this weekend. ben.keen@gmail.com

@wobblymusic
Copy link

I'm not sure what happened -- probably a caching issue -- but my original hack is working now. I can access the country by using _countries[this.selectedIndex-1][1] within the $(this).on("change", function(). If you want to add the option of outputting an extra data-shortcode attribute, I won't say no, but I'm ok for now. Thanks for all the support!

@benkeen
Copy link
Member

benkeen commented Oct 26, 2015

Good to hear! Thanks for the follow-up. :)

@benkeen benkeen closed this as completed Oct 26, 2015
@benkeen
Copy link
Member

benkeen commented Apr 26, 2016

N.B. on this issue. I thought I'd implement a React version of the country-region-selector script, so will be moving the data source to a separate location in a different format. See: https://github.com/benkeen/country-region-data

That way I'll be able to pull that data source into whatever scripts I need and manipulate it into an appropriate format for packaging.

@sventech
Copy link
Contributor

That seems like a good idea. It also makes it easier for other projects, such as Zippopotam.us to use the data (although I suppose there might be license issues, since they use the OpenData license).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants