- Home
- Forum
- JavaScript - EJ 2
- Default for cell as selected that would work on enter click after grid bound
Default for cell as selected that would work on enter click after grid bound
I have a grid with first cell hyperlinks. on loading grid, want the first cell to be selected by default so that user can directly hit enter to perform click event of hyperlink.
Please find below code:
var patientSearchResultsGridColumns = [
{
field: 'displayName', headerText: 'Name', allowEditing: false, allowResizing: false, width: 55, template: '#patientNameTemplate'
},
{
field: 'programName', headerText: 'Program Name', allowEditing: false, allowResizing: false, width: 36
}
]
var patientSearchResultsGrid = new ej.grids.Grid({
dataSource: tempdata,
allowResizing: true,
allowPaging: true,
allowTextWrap: true,
allowFiltering: true,
pageSettings: { pageSize: 7 },
textWrapSettings: { wrapMode: 'Both' },
load: CreateFilterBarTemplate,
columns: patientSearchResultsGridColumns,
selectionSettings: { cellSelectionMode: 'Box', type: 'Multiple', mode: 'Cell' },
rowDataBound: rowBound,
dataBound: () => {
patientSearchResultsGrid.editModule.editCell(0, "displayName");
}
});
patientSearchResultsGrid.appendTo('#PatientSearchResultsGrid');
<script id="patientNameTemplate" type="text/x-template">
<div>
<a rel='nofollow' href="#" pat-id=${patientID} id="pat_${patientID}" onclick='viewPatientDetail(this)'>${displayName}</a>
</div>
</script>
SIGN IN To post a reply.
5 Replies
DR
Dhivya Rajendran
Syncfusion Team
September 4, 2019 10:29 AM UTC
Hi Sandhya,
Greeting from Syncfusion.
Query: want the first cell to be selected by default so that user can directly hit enter to perform click event of hyperlink.
By default we have provide selectCell method in Grid which helps to select the cell programmatically. In the below code example, we have bind dataBound event for grid and select the first cell using selectCell method.
Please refer the below code example for more information.
|
var grid = new ej.grids.Grid({
dataSource: window.employeeData,
allowSelection: true,
selectionSettings: { cellSelectionMode: 'Box', type: 'Multiple', mode: 'Cell' },
dataBound:(args)=>{
grid.selectCell({ rowIndex: 0, cellIndex: 0 });
},
enableHover: false,
columns: [
{ field: 'EmployeeID', headerText: 'Employee ID', textAlign: 'Right', width: 135 },
]
});
grid.appendTo('#Grid');
|
Help documentation: https://ej2.syncfusion.com/javascript/documentation/api/grid/#selectcell
Please get back to us if you need further assistance on this.
Regards,
R.Dhivya
SG
Sandhya Gademsetti
September 4, 2019 10:52 AM UTC
This works fine and the first cell getting selected.
But I have anchor tag in the first cell that should be focused.
var patientSearchResultsGrid = new ej.grids.Grid({
dataSource: response.patientSearchResults,
allowResizing: true,
allowPaging: true,
allowTextWrap: true,
allowFiltering: true,
pageSettings: { pageSize: 7 },
textWrapSettings: { wrapMode: 'Both' },
load: CreateFilterBarTemplate,
columns: patientSearchResultsGridColumns,
selectionSettings: { cellSelectionMode: 'Box', type: 'Multiple', mode: 'Cell' },
rowDataBound: rowBound,
dataBound: () => {
patientSearchResultsGrid.selectCell({ rowIndex: 0, cellIndex: 0 });
var patid = response.patientSearchResults[0].patientID;
$('#pat_' + patid).focus();
}
});
Tried focusing element manually but no luck.
TS
Thavasianand Sankaranarayanan
Syncfusion Team
September 5, 2019 01:13 PM UTC
Hi Sandhya,
We have analyzed your requirement and we suggest you to use the below way to achieve this. In the below sample, we have bind the keydown event for Grid and based on the condition(its template element and keycode as 13) we have programmatically click the corresponding anchor element.
Please refer the below code example and sample for more information.
|
Index.html
<script id="template" type="text/x-template">
<a id="temp" rel='nofollow' href="#" class='customTemplate' onclick='viewPatientDetail(this)'>${EmployeeID} </a>
</script>
<script>
function viewPatientDetail(args){
alert('Hi')
}
</script>
var grid = new ej.grids.Grid({
selectionSettings: { cellSelectionMode: 'Box', type: 'Multiple', mode: 'Cell' },
dataBound: (args) => {
grid.selectCell({ rowIndex: 0, cellIndex: 0 });
},
columns: [
{
headerText: 'Employee Image', textAlign: 'Center',
template: '#template', width: 150
}],
width: 'auto',
height: 359
});
grid.appendTo('#Grid');
document.getElementById('Grid').addEventListener('keydown', function (args) {
if (args.target.classList.contains('customTemplate') && args.keyCode === 13) {
args.target.click()
}
})
|
Regards,
Thavasianand S.
SG
Sandhya Gademsetti
September 9, 2019 06:00 PM UTC
Thank you. This worked
TS
Thavasianand Sankaranarayanan
Syncfusion Team
September 10, 2019 09:02 AM UTC
Hi Sandhya,
Thanks for your update.
We are happy that the problem has been resolved at your end.
Regards,
Thavasianand S.
SIGN IN To post a reply.
- 5 Replies
- 3 Participants
-
SG Sandhya Gademsetti
- Sep 3, 2019 10:38 AM UTC
- Sep 10, 2019 09:02 AM UTC