Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

javascript - Trigger ctrl+a event

I have a SAPUI5 SelectDialog control, which contains StandardListItem control inside it to display a list of data, something like this:

enter image description here

I can select each entry one by one.

What I want to achieve, is to provide "SELECT ALL" or "DESELECT ALL" functionality. If I press ctrl+A inside dialog box, it works well & selects all the entries inside the dialog. What I want is to provide a button/checkbox at the top, which user can select to select/deselect all the entries at once. Unlike multicombobox, SelectDialog control does not have inbuilt "Select All" method.

Using tutorial at https://www.w3schools.com/jsref/event_ctrlkey.asp, I tried triggering ctrl+A event using a button, but it seems not working. Any input on this?

Code I tried

<script>event.$(document).ready(function(){
        $("#myButton").click(function () {  
        var triggerEvent = $.Event();
        triggerEvent.buttonevent == 65; //keycode for alphabet A
        triggerEvent.ctrlKey == true;
        $(this).trigger('triggerEvent');
    });
    });</script>



<SelectDialog id="myDialog" growingThreshold="2000" 
        multiSelect="true" noDataText="NoDataFound" liveChange="handleSearchSelectionDialog" items="{model>/data}"
        search="handleSearchSelectionDialog" title="Choose" contentHeight="50%">
        <StandardListItem id="abc" 
            title="SELECT ALL" type="Active"/>
        <StandardListItem id="myId" 
            title="data" type="Active"/>
<button onclick="clicked" id="myButton">SELECT ALL</button>
    </SelectDialog>

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think your solution can be found here: Firing a Keyboard Event in JavaScript

you basically need to trigger two key-events. At first for the ctrl and followed up the 'a'.

var ctrlEvent = new KeyboardEvent("keydown", {bubbles : true, cancelable : true, keyCode : 17, char : 17, shiftKey : true});
var aEvent = new KeyboardEvent("keydown", {bubbles : true, cancelable : true, keyCode : 65, char : 65, shiftKey : true});

document.dispatchEvent(ctrlEvent);
document.dispatchEvent(aEvent);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...