Skip to content

Commit

Permalink
Selected triggers event when checked overflow, resolved OpenNHP#733
Browse files Browse the repository at this point in the history
  • Loading branch information
Minwe committed Nov 24, 2015
1 parent 47ecb90 commit 819bab4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Amaze UI Change Log
---

### 2015.11 W3

**JS:**

- `Improved` #733 Selected 增加超过最多可选值时提示接口。

### 2015.11 W2

**构建:**
Expand Down Expand Up @@ -39,7 +45,7 @@

**JS:**

- 处理 Browserify 输出 UMD 文件时没有添加 AMD 和 CommonJS 依赖的问题。
- `Fixed` #783 处理 Browserify 输出 UMD 文件时没有添加 AMD 和 CommonJS 依赖的问题。

### 2015.07 W4

Expand Down
22 changes: 20 additions & 2 deletions docs/javascript/selected.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,33 @@ doc: docs/javascript/selected.md

### 选项验证提示

**`v2.5` 新增**:设置 `maxchecked` 属性以后,超出设定值时会触发 `checkedOverflow.selected.amui` 事件,可以监听该事件提示用户。

`````html
<select multiple data-am-selected minchecked="2" maxchecked="3">
<select multiple data-am-selected minchecked="2" maxchecked="3" id="demo-maxchecked">
<option value="a">Apple</option>
<option value="b">Banana</option>
<option value="o">Orange</option>
<option value="m">Mango</option>
</select>
<script>
$('#demo-maxchecked').on('checkedOverflow.selected.amui', function() {
alert('最多选择' + this.getAttribute('maxchecked') + '');
});
</script>
`````
```html
<select multiple data-am-selected minchecked="2" maxchecked="3">
<select multiple data-am-selected minchecked="2" maxchecked="3" id="demo-maxchecked">
<option value="a">Apple</option>
<option value="b">Banana</option>
<option value="o">Orange</option>
<option value="m">Mango</option>
</select>
<script>
$('#demo-maxchecked').on('checkedOverflow.selected.amui', function() {
alert('最多选择' + this.getAttribute('maxchecked') + '');
});
</script>
```

### JS 操作 select
Expand Down Expand Up @@ -406,6 +418,12 @@ $(function() {
- `maxHeight: null`: 列表最大高度
- `dropUp: 0`: 是否为上拉,默认为 `0` (`false`)

#### 事件

| 事件名称 | 描述 |
| ------ | ---- |
| `checkedOverflow.selected.amui` | 超出设定的最多可选值时触发(`v2.5` 新增) |

## 常见问题

### 与 jQuery Form 冲突?
Expand Down
31 changes: 23 additions & 8 deletions js/ui.selected.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Selected.DEFAULTS = {
btnStyle: 'default',
dropUp: 0,
maxHeight: null,
maxChecked: null,
placeholder: '点击选择...',
selectedClass: 'am-checked',
disabledClass: 'am-disabled',
Expand Down Expand Up @@ -135,7 +136,9 @@ Selected.prototype.init = function() {
// set hint text
var hint = [];
var min = $element.attr('minchecked');
var max = $element.attr('maxchecked');
var max = $element.attr('maxchecked') || options.maxChecked;

this.maxChecked = max || Infinity;

if ($element[0].required) {
hint.push('必选');
Expand Down Expand Up @@ -220,20 +223,32 @@ Selected.prototype.renderOptions = function() {
};

Selected.prototype.setChecked = function(item) {
var _this = this;
var options = this.options;
var $item = $(item);
var isChecked = $item.hasClass(options.selectedClass);
if (!this.multiple) {
if (!isChecked) {
this.dropdown.close();
this.$shadowOptions.not($item).removeClass(options.selectedClass);
} else {
return;

if (this.multiple) {
// multiple
var checkedLength = this.$list.find('.' + options.selectedClass).length;

if (!isChecked && this.maxChecked <= checkedLength) {
this.$element.trigger('checkedOverflow.selected.amui', {
selected: this
});

return false;
}
} else {
if (isChecked) {
return false;
}

this.dropdown.close();
this.$shadowOptions.not($item).removeClass(options.selectedClass);
}

$item.toggleClass(options.selectedClass);

this.syncData(item);
};

Expand Down

0 comments on commit 819bab4

Please sign in to comment.