您好,原生Column中对visible属性做了封装,定义了getter和setter方法,不过好在它原生的setter和getter是在__proto__中定义的,因此我们可以在这里找到一些切入点,参考代码示例:
App.js
- import 'bootstrap.css';
- import '@grapecity/wijmo.styles/wijmo.css';
- import { SortDescription, format } from '@grapecity/wijmo';
- import { FlexGrid } from '@grapecity/wijmo.grid';
- document.readyState === 'complete' ? init() : window.onload = init;
- function init() {
- // create some random data
- var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
- var data = [];
- for (var i = 0; i < countries.length; i++) {
- data.push({
- id: i,
- country: countries[i],
- sales: Math.random() * 10000,
- expenses: Math.random() * 5000
- });
- }
- // bind a grid to the raw data
- var theGrid = new FlexGrid('#theGrid', {
- autoGenerateColumns: false,
- columns: [
- { binding: 'country', header: 'Country', width: '2*' },
- { binding: 'sales', header: 'Sales', width: '*', format: 'n2' },
- { binding: 'expenses', header: 'Expenses', width: '*', format: 'n2' }
- ],
- itemsSource: data
- });
- // show the current item
- var selItemElement = document.getElementById('selectedItem');
- function updateCurrentInfo() {
- selItemElement.innerHTML = format('Country: <b>{country}</b>, Sales: <b>{sales:c0}</b> Expenses: <b>{expenses:c0}</b>', theGrid.collectionView.currentItem);
- }
- // update current item now and when the grid selection changes
- updateCurrentInfo();
- theGrid.collectionView.currentChanged.addHandler(updateCurrentInfo);
- // sort the data by country
- var sd = new SortDescription('country', true);
- theGrid.collectionView.sortDescriptions.push(sd);
- theGrid.columns[0].onPropertyChanged(() => {
- console.log(1);
- })
- const originalColumnSetter = Object.getOwnPropertyDescriptor(
- theGrid.columns[0].__proto__.__proto__, "visible").set;
-
- let visible = theGrid.columns[0].visible;
- Object.defineProperty(theGrid.columns[0], "visible", {
- get(){
- return visible
- },
- set(newValue) {
- console.log('newValue, ',newValue);
- originalColumnSetter.call(this, newValue);
- }
- });
- document.getElementById('btn').addEventListener('click', function(){
- console.log(theGrid.columns[0].visible);
- visible = !visible
- theGrid.columns[0].visible = visible;
- });
- }
复制代码
index.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>GrapeCity Wijmo FlexGrid Arrays and CollectionViews Binding</title>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <!-- SystemJS -->
- <script src="node_modules/systemjs/dist/system.src.js"></script>
- <script src="systemjs.config.js"></script>
- <script>
- System.import('./src/app');
- </script>
- </head>
- <body>
- <div class="container-fluid">
- <button id="btn">按钮</button>
- <p id="selectedItem"></p>
- <div id="theGrid"></div>
- </div>
- </body>
- </html>
复制代码
地址:
https://www.grapecity.com/wijmo/ ... nding/Basics/purejs
另补充说明,还是建议能够自己监听到导致visible更改的位置,加入代码逻辑,尽量不要用这种破坏原型的实现方案。 |