这里实际上用到了Wijmo的Input组件中ListBox组件,结合这个组件的一些特性就可以解决这个问题,就拿这个示例举例:
https://demo.grapecity.com.cn/wi ... ColumnPicker/purejs
原示例直接用grid的columns当listbox的数据源,直接展示和修改了columns的属性,所以这里只要把直接引用columns改为间接引用即可,关键代码如下:
- // 只要在这里把直接引用改为间接引用即可
- var listBoxSource = [];
- theGrid.columns.forEach(function(col){
- // 这里把要隐藏的项目排除出去
- if(col.binding == "id"){return;}
- listBoxSource.push(col);
- });
复制代码
将以下完整代码替换掉示例的app.js即可:
- import 'bootstrap.css';
- import '@grapecity/wijmo.styles/wijmo.css';
- import './styles.css';
- import { showPopup, hidePopup, hasClass } from '@grapecity/wijmo';
- import { ListBox } from '@grapecity/wijmo.input';
- import { FlexGrid } from '@grapecity/wijmo.grid';
- document.readyState === 'complete' ? init() : window.onload = init;
- function init() {
- // generate some random data
- var data = [], countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], products = ['Widget', 'Sprocket', 'Gadget', 'Doohickey'], colors = ['Black', 'White', 'Red', 'Green', 'Blue'], dt = new Date();
- for (var i = 0; i < 100; i++) {
- var date = new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60), countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length);
- var item = {
- id: i,
- start: date,
- end: date,
- country: countries[countryId],
- product: products[productId],
- color: colors[colorId],
- countryId: countryId,
- productId: productId,
- colorId: colorId,
- amount1: Math.random() * 10000 - 5000,
- amount2: Math.random() * 10000 - 5000,
- amount3: Math.random() * 10000 - 5000,
- amount4: Math.random() * 10000 - 5000,
- amount5: Math.random() * 10000 - 5000,
- discount: Math.random() / 4,
- active: i % 4 == 0
- };
- data.push(item);
- }
- // create the grid
- var theGrid = new FlexGrid('#theGrid', {
- itemsSource: data,
- formatItem: function (s, e) {
- if (e.panel == s.topLeftCells) {
- e.cell.innerHTML = '<span class="column-picker-icon glyphicon glyphicon-cog"></span>';
- }
- }
- });
- // 隐藏ID
- theGrid.columns[0].visible = false;
- // 只要在这里把直接引用改为间接引用即可
- var listBoxSource = [];
- theGrid.columns.forEach(function(col){
- // 这里把要隐藏的项目排除出去
- if(col.binding == "id"){return;}
- listBoxSource.push(col);
- });
- // create the column picker
- var theColumnPicker = new ListBox('#theColumnPicker', {
- itemsSource: listBoxSource,
- checkedMemberPath: 'visible',
- displayMemberPath: 'header',
- lostFocus: function () {
- hidePopup(theColumnPicker.hostElement);
- }
- });
- // show the column picker when the user clicks the top-left cell
- var ref = theGrid.hostElement.querySelector('.wj-topleft');
- ref.addEventListener('mousedown', function (e) {
- if (hasClass(e.target, 'column-picker-icon')) {
- showPopup(theColumnPicker.hostElement, ref, false, true, false);
- theColumnPicker.focus();
- e.preventDefault();
- }
- });
- // save/restore layout buttons
- document.getElementById('btnSave').addEventListener('click', function () {
- localStorage.setItem('myLayout', theGrid.columnLayout);
- });
- document.getElementById('btnRestore').addEventListener('click', function () {
- var layout = localStorage.getItem('myLayout');
- if (layout) {
- theGrid.columnLayout = layout;
- }
- });
- }
复制代码 |