简体   繁体   English

角度事件检测,日期选择器关闭

[英]Angular event detection for datepicker close

I have just started using angular and in general love it. 我刚开始使用有角度的工具,并且通常喜欢它。 I have a UI requirement to show a date as text, then when you click on the text, it shows a date picker instead. 我有一个UI要求,将日期显示为文本,然后单击文本时,它将显示日期选择器。 Then when you select a date it goes back to text. 然后,当您选择一个日期时,它会返回到文本。 Sounds simple. 听起来很简单。

I have tried a few variations on the below code the object being edited holds a value called editdate, clicking on the text box changes this to true and hides the text and shows the editor. 我在下面的代码中尝试了一些变体,正在编辑的对象拥有一个称为editdate的值,单击文本框将其更改为true,然后隐藏文本并显示编辑器。 This works fine, the problem is hiding the date picker. 效果很好,问题在于隐藏了日期选择器。 I tried adding ng-blur to the datepicker to cause the box to hide when it looses focus, but this was very intermittent as to if it worked. 我尝试在日期选择器中添加ng-blur,以使该框在失去焦点时隐藏起来,但这对于它是否起作用是非常断断续续的。 I then tried ng-Mouseleave but the box closed when I moved the mouse pointer into the date picker from the text box. 然后,我尝试了ng-Mouseleave,但是当我将鼠标指针从文本框中移到日期选择器中时,该框关闭了。

<td colspan="7">
    Required Date : <span ng-click="item.editdate=true; item.delayeditclose = true;" ng-show="!item.editdate">{{item.dueDate.toDateString()}}</span>  
    <input type="date" ng-blur="item.editdate=false" ng-click="item.switcheditdate(item.delayeditclose)"  ng-show="item.editdate" class="datepick" ng-model="item.dueDate" ng-focus="selectPart(item.partNumber)" />
</td>

The javascript is initialized as: javascript初始化为:

                for (var i = 0; i < data.items.length; i++) {
                    item = data.items[i];
                    item.showExpanded = false;
                    item.editdate = false;
                    item.delayeditclose = true;
                    item.switcheditdate = function (delayclose) {
                        if (!delayclose) {
                            this.editdate = !this.editdate;
                            this.delayeditclose = true;
                        }
                        this.delayeditclose = false;

                    };
                    items.push(item);
           }

I have now moved onto the above which almost does the job. 我现在转到上面的内容,它几乎可以完成任务。 The new issue being html5 date pickers seem to have ax to close them down. html5日期选择器的新问题似乎是用斧头将其关闭。 This hides the whole box and not just the picker part. 这隐藏了整个盒子,而不仅仅是选择器部分。 If this happens I want to show the text of the date rather than what I get now which is nothing. 如果发生这种情况,我想显示日期的文本,而不是现在得到的内容。 Does anyone know of how I can capture this close event and revert back to the text or suggest a better approach to the problem? 有谁知道我如何捕捉到这个结束事件并恢复为文字,或者提出解决问题的更好方法?

I have lots of items each one is in its own row in a table 我有很多物品,每个物品在一张桌子的单独行中

I am using angular 1.3 beta 8 我正在使用角度1.3 beta 8

thanks for looking Andy 感谢您寻找安迪

The reason why you are getting nothing is because when you click x what happens is that the date value gets cleared so then your date view is an empty string. 您什么都没得到的原因是,当您单击x ,会发生的事情是清除了日期值,因此日期视图为空字符串。 What you can do is set a condition to display a string such as "Click to Edit" when the date is empty. 您可以做的是设置一个条件,以在日期为空时显示一个字符串,例如“单击以编辑”。

Taking just what you have above, one way of doing it is by adding an element and a flag in your date "view". 采取上面的方法,一种方法是在日期“视图”中添加一个元素和一个标志。 So change this: 所以改变这个:

Required Date : <span ng-click="item.editdate=true; item.delayeditclose = true;" ng-show="!item.editdate">{{item.dueDate.toDateString()}}</span>

To: 至:

Required Date : 
<span ng-click="item.editdate=true; item.delayeditclose = true;" ng-show="!item.editdate">
    <span>{{item.dueDate.toDateString()}}</span>
    <span ng-show="!item.dueDate">Click to Edit</span>
</span>

The directive way 指示方式

Now in my opinion I think it's much better to use a directive for this. 现在,我认为最好为此使用指令。 It would simplify your logic and is much easier to re-use. 这将简化您的逻辑,并且易于重用。 I created a plunker which shows my implementation using a directive. 我创建了一个插件 ,使用指令显示了我的实现。

Answer to question in title 标题问题的答案

To answer the question posed by your title, when using the html5 date picker, you can't detect when it is closed (or even when it is opened). 为了回答标题所提出的问题,在使用html5日期选择器时,您无法检测到何时关闭(甚至打开)。 See this answer and the linked Quick FAQs on input[type=date] 请参阅此答案以及有关输入[type = date]的链接的快速常见问题解答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM