We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Default for cell as selected that would work on enter click after grid bound

Thread ID:

Created:

Updated:

Platform:

Replies:

147155 Sep 3,2019 10:38 AM UTC Sep 10,2019 09:02 AM UTC JavaScript - EJ 2 5
loading
Tags: Grid
Sandhya Gademsetti
Asked On September 3, 2019 10:38 AM UTC

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>


Dhivya Rajendran [Syncfusion]
Replied On 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'); 



Please get back to us if you need further assistance on this. 

Regards, 
R.Dhivya 


Sandhya Gademsetti
Replied On 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.

Thavasianand Sankaranarayanan [Syncfusion]
Replied On 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.  


Sandhya Gademsetti
Replied On September 9, 2019 06:00 PM UTC

Thank you. This worked

Thavasianand Sankaranarayanan [Syncfusion]
Replied On 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.  


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon