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

data-count-selected-text - singular and plural #705

Closed
alexdpunkt opened this issue Aug 22, 2014 · 9 comments
Closed

data-count-selected-text - singular and plural #705

alexdpunkt opened this issue Aug 22, 2014 · 9 comments

Comments

@alexdpunkt
Copy link

would be nice if one had the possibility to specify singular and plural texts.

for example:

data-count-selected-text="{0} items selected"
data-count-selected-text-singular="{0} item selected"

@t0xicCode
Copy link
Collaborator

I agree. The best would be for something like gettext to exist for javascript and be common enough that using it wouldn't pull a new dependency. In the meantime I might add logic that permits countSelectedText (and any other *Text for that matter) to be a function that accepts a single integer argument and returns a string.

@alexdpunkt
Copy link
Author

yes that would be great.

I could imagine something like this:

$('select').selectpicker({
   countSelectedText: function(num, element) { 
      var title;
      if(num == 1) {
          title = element.data('count-selected-text').replace('{0}', num);
      } else { 
          title = element.data('count-selected-text-plural').replace('{0}', num);
      }
      return title;
   }
});

So basically the element should be exposed to the function - that way any random attribute can be accessed and processed.

@t0xicCode
Copy link
Collaborator

I was more thinking of having something like follow:

$('select').selectpicker({
   countSelectedText: function(num) { 
      if (num == 1) {
          return "{0} item selected";
      } else { 
          return "{0} items selected";
      }
   }
});

@t0xicCode
Copy link
Collaborator

Each select could define its own function. I'm trying not to introduce an extra configuration variable if I don't have to.

@alexdpunkt
Copy link
Author

Well, I would need different texts for every select element.

This would be the perfect and most versatile solution:

countSelectedText: function(numSelected, numTotal, element) { 
      var text;
      if(num == 1) {
          text = element.data('foo');
      } else { 
          text = element.data('bar');
      }     
      return text.replace('{0}', numSelected).replace('{1}', numTotal);
}
<select data-foo="{0} item selected" data-bar="{0} items selected">...

Everything stays as it is, except when a function is provided it is called with those 3 parameters. No extra configuration variable :-)

@t0xicCode
Copy link
Collaborator

What I wants is for the function to return a "format" string, not the formatted string (I want to do the replacement in the plugin code).

You'd still be able to access the data since this would be mapped to the selectpicker object, and all data attributes are imported into the options property.

@alexdpunkt
Copy link
Author

Ah, I see :)

So it would look something like

countSelectedText: function(num) { 
      var text;
      if(num == 1) {
          text = this.options.foo;
      } else { 
          text = this.options.bar;
      }     
      return text;
}

@t0xicCode
Copy link
Collaborator

Yes, that's what I have in mind.

@alexdpunkt
Copy link
Author

I would love to see this anytime soon as I really need it for my current project. In the meantime I will hack my copy of bootstrap-select.js around line 364.

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

No branches or pull requests

2 participants