Handsontable 篩選事件


  有時候我們需要知道在使用Handsontable時篩選掉了哪些數據,並對這些數據進行處理,可以使用afterFilter事件來進行相關操作。

  Handsontable篩選掉的數據沒有真的被刪除,而是被隱藏了起來,我們需要知道這些被隱藏起來的行號,然后獲得相關數據。

 

  原始數據:

  

 

  相關代碼如下:

<!DOCTYPE html>
<html>
<head>
    <title>handsontable demo</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="handsontable/htstyle.css">
    <link rel="stylesheet" href="handsontable/htstyle-custom.css">
    <script src="handsontable/jquery-1.12.1.js"></script>
    <script src="handsontable/handsontable.min.js"></script>
</head>
<body>
    <div id="example"></div>

    <script>
        var data = [
            { riqi: '2017-01', address: '北京', goods: '冰箱', price: '3399', sales: 530, del: '' },
            { riqi: '2017-01', address: '天津', goods: '空調', price: '4299', sales: 522, del: '' },
            { riqi: '2017-01', address: '上海', goods: '洗衣機', price: '1299', sales: 544, del: '' },
            { riqi: '2017-01', address: '廣州', goods: '彩電', price: '4599', sales: 562, del: '' },
            { riqi: '2017-01', address: '深圳', goods: '熱水器', price: '1099', sales: 430, del: '' },
            { riqi: '2017-02', address: '重慶', goods: '筆記本電腦', price: '4999', sales: 666, del: '' },
            { riqi: '2017-02', address: '廈門', goods: '油煙機', price: '2899', sales: 438, del: '' },
            { riqi: '2017-02', address: '青島', goods: '飲水機', price: '899', sales: 620, del: '' },
            { riqi: '2017-02', address: '大連', goods: '手機', price: '1999', sales: 500, del: '' }
        ];

        function negativeValueRenderer(instance, td, row, col, prop, value, cellProperties) {
            Handsontable.renderers.TextRenderer.apply(this, arguments);
            if (prop == 'address') {
                td.style.color = '#32CD32';
            }
            else if (prop == 'price') {
                //格式化價格數據
                td.innerText = value != null ? numbro(value).format('0.00') : "";
            }
            else if (prop == 'sales') {
                //右對齊
                td.style.textAlign = 'right';
                td.innerText = value != null ? numbro(value).format('0,0.00') : "";
            }
            else if (prop == 'del') {
                //添加自定義的圖片,並給圖片的chick添加事件
                var escaped = Handsontable.helper.stringify(value),
                  imgdel;

                imgdel = document.createElement('IMG');
                imgdel.src = "handsontable/remove.png";
                imgdel.width = 20;
                imgdel.name = escaped;
                imgdel.style = 'cursor:pointer;';//鼠標移上去變手型
                Handsontable.dom.addEvent(imgdel, 'click', function (event) {
                    hot.alter("remove_row", row);//刪除當前行
                });

                Handsontable.dom.empty(td);
                td.appendChild(imgdel);
                td.style.textAlign = 'center';
                return td;
            }
        }
        Handsontable.renderers.registerRenderer('negativeValueRenderer', negativeValueRenderer);

        var hot = new Handsontable(document.getElementById('example'), {
            data: data,
            colHeaders: ['操作', '日期', '地點', '商品', '單價', '銷量'], // 使用自定義列頭
            rowHeaders: true,
            colWidths: 150, // 設置所有列寬為150像素
            filters: true,
            columnSorting: true,
            sortIndicator: true,
            autoColumnSize: true,
            manualColumnResize: true,
            undo: true,
            redo: true,
            wordWrap: true,
            copyable: true,
            mergeCells: false,
            manualRowResize: true,
            manualRowMove: true,
            manualColumnMove: false,
            renderAllRows: true,
            allowInsertRow: true,
            allowInsertColumn: false,
            fixedColumnsLeft: 1,
            columns: [{
                data: 'del',
                type: 'text'
            }, {
                data: 'riqi',
                type: 'date',
                dateFormat: 'YYYY-MM-DD'
            }, {
                data: 'address',
                type: 'text'
            }, {
                data: 'goods',
                type: 'text'
            }, {
                data: 'price',
                type: 'numeric'
            }, {
                data: 'sales',
                type: 'numeric'
            }],
            contextMenu: ['row_above', 'row_below', '---------', 'remove_row', '---------', 'undo', 'redo', '---------', 'make_read_only', '---------', 'alignment'],
            dropdownMenu: ['filter_by_condition', 'filter_by_value', 'filter_action_bar'],
            cells: function (row, col, prop) {
                var cellProperties = {};
                cellProperties.renderer = "negativeValueRenderer";
                return cellProperties;
            },
        });
        //添加篩選成功后的觸發事件
        hot.addHook('afterFilter', function () {
            var plugin = hot.getPlugin('trimRows').trimmedRows;//獲取被篩選掉的行號
            if (plugin.length <= 0) {
                return;
            }
            console.log(plugin);

            var DataArray = new Array();

            for (var i = 0; i < plugin.length; i++) {
                //通過行號獲取數據
                DataArray.push(hot.getSourceDataAtRow(plugin[i]));
            }

            console.log(DataArray);
        });
    </script>
</body>
</html>

 

  在操作中,我篩選掉了地點為北京的那一行,可以發現瀏覽器日志如下:

  

 

  

 

By QJL

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM