In exjts4, we can lock the header (locking grid. The instance enables the grid to lock the table header. The locked columns are fixed (if there is a horizontal scroll bar ). The locked Column cannot be removed. For convenience, some local data is constructed as test data.
JS Code:
- Ext. Require ([
- 'Ext. Grid .*',
- 'Ext. Data .*',
- 'Ext. util .*',
- 'Ext. state .*'
- ]);
- Ext. onready (function (){
- Ext. quicktips. INIT ();
- // Sample static data for the store
- VaR mydata = [
- ['3 M co', 71.72, 0.02, 0.03, '2017 am '],
- ['Alcoa inc', 29.01, 0.42, 1.47, '2017 am '],
- ['Altria group inc', 83.81, 0.28, 0.34, '2017 am '],
- ['American express company ', 52.55, 0.01, 0.02, '2017 am'],
- ['American International Group, Inc. ', 64.13, 0.31, 0.49, '2017 am'],
- ['At & T Inc. ', 31.61,-0.48,-1.54, '2017 am'],
- ['Boeing Co. ', 75.43, 0.53, 0.71, '2017 am'],
- ['Caterpillar Inc. ', 67.27, 0.92, 1.39, '2017 am'],
- ['Citigroup, Inc. ', 49.37, 0.02, 0.04, '2017 am'],
- ['E. I. Du Pont de Nemours and company', 40.48, 0.51, 1.28, '2017 am '],
- ['Exxon Mobil Corp ', 68.1,-0.43,-0.64, '2017 am'],
- ['General Electric Company ', 34.14,-0.08,-0.23, '2017 am'],
- ['General Motors Corporation ', 30.27, 1.09, 3.74, '2017 am'],
- ['Hewlett-Packard Co. ', 36.53,-0.03,-0.08, '2017 am'],
- ['Honeywell Intl inc', 38.77, 0.05, 0.13, '2017 am '],
- ['Intel Corporation ', 19.88, 0.31, 1.58, '2017 am'],
- ['International business machines ', 81.41, 0.44, 0.54, '2017 am'],
- ['Johnson & Johnson ', 64.72, 0.06, 0.09, '2017 am'],
- ['Jp Morgan & Chase & co', 45.73, 0.07, 0.15, '2017 am '],
- ['Mcdonald \'s Corporation ', 36.76, 0.86, 2.40, '2017 am'],
- ['Merck & Co., Inc. ', 40.96, 0.41, 1.01, '2017 am'],
- ['Microsoft origin', 25.84, 0.14, 0.54, '2017 am '],
- ['Pfizer inc', 27.96, 0.4, 1.45, '2017 am '],
- ['The Coca-Cola Company ', 45.07, 0.26, 0.58, '2017 am'],
- ['The Home Depot, Inc. ', 34.64, 0.35, 1.02, '2017 am'],
- ['The Procter & Gamble Company ', 61.91, 0.01, 0.02, '2017 am'],
- ['United Technologies Corporation ', 63.26, 0.55, 0.88, '2017 am'],
- ['Verizon communications ', 35.57, 0.39, 1.11, '2017 am'],
- ['Wal-Mart stores, Inc. ', 45.45, 0.73, 1.63, '2017 am']
- ];
- /**
- * Custom function used for column Renderer
- * @ Param {object} Val
- */
- Function Change (VAL ){
- If (Val> 0 ){
- Return '<span style = "color: green;">' + Val + '</span> ';
- } Else if (Val <0 ){
- Return '<span style = "color: red;">' + Val + '</span> ';
- }
- Return val;
- }
- /**
- * Custom function used for column Renderer
- * @ Param {object} Val
- */
- Function pctchange (VAL ){
- If (Val> 0 ){
- Return '<span style = "color: green;">' + Val + '% </span> ';
- } Else if (Val <0 ){
- Return '<span style = "color: red;">' + Val + '% </span> ';
- }
- Return val;
- }
- // Create the Data Store
- VaR store = ext. Create ('ext. Data. arraystore ',{
- Fields :[
- {Name: 'company '},
- {Name: 'price', type: 'float '},
- {Name: 'change', type: 'float '},
- {Name: 'pctchang', type: 'float '},
- {Name: 'lastchang', type: 'date', dateformat: 'n'/j h: '}
- ],
- Data: mydata
- });
- // Create the grid
- VaR grid = ext. Create ('ext. Grid. Panel ',{
- Store: store,
- Columnlines: True,
- Columns :[{
- Text: 'company ',
- Locked: True,
- Width: 200,
- Sortable: false,
- Dataindex: 'company'
- },{
- Text: 'price ',
- Width: 125,
- Sortable: True,
- Renderer: 'usmoney ',
- Dataindex: 'price'
- },{
- Text: 'change ',
- Width: 125,
- Sortable: True,
- Renderer: change,
- Dataindex: 'change'
- },{
- Text: '% change ',
- Width: 125,
- Sortable: True,
- Renderer: pctchange,
- Dataindex: 'pctchang'
- },{
- Text: 'Last updated ',
- Width: 135,
- Sortable: True,
- Renderer: Ext. util. format. daterenderer ('m/D/Y '),
- Dataindex: 'lastchange'
- }],
- Height: 350,
- Width: 600,
- Title: 'locking grid column ',
- Renderto: 'grid-example ',
- Viewconfig :{
- Striperows: True
- }
- });
- });
Save this code as: locking-grid.js. Because the Code renders the grid to a container with ID: Grid-example, we need to write a DIV in the HTML page to place the grid.
HTML code:
- <! Doctype HTML public "-// W3C // dtd html 4.01 // en" "http://www.w3.org/TR/html4/strict.dtd">
- <HTML>
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> locking grid column example </title>
- <LINK rel = "stylesheet" type = "text/CSS" href = "../resources/CSS/ext-all.css"/>
- <LINK rel = "stylesheet" type = "text/CSS" href = "../shared/example.css"/>
- <SCRIPT type = "text/JavaScript" src = "..././Bootstrap. js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "locking-grid.js"> </SCRIPT>
- </Head>
- <Body>
- <Div id = "Grid-Example"> </div>
- </Body>
- </Html>
Note that you must pay attention to the path for leading CSS and JS files.