JS Change and Restore the Background Color by Clicking

  • Last update:December 16, 2020
  • I. Overview

    1.1 Requirement

    In a report block, click a cell to change the background color and click it again to restore the background color.

     

    1.2 Solution

    The following solution applies:

    JS Check-All Checkbox

    However, the solution cannot restore the cell color. Add an After Initialization event to a report block, use the function setTimeOut to define a click event by setting the background color after clicking on the cell and the restored background color respectively, and set the background color according to the cell color when clicking.

    II. Sample

    2.1 Design a report

    1) Create a new decision-making report and a new dataset ds1: 

    SELECT * FROM Sales_Volume

    2) Drag a report block into the decision-making report, with the report block designed as follows:


    2.2 Add an After Initialization event

    Add an After Initialization event to the report block, as shown below:

    Input the JS code:

    setTimeout(function() {
        //background color variables when selected
        var oldColor = "rgb(255, 255, 255)";
        var newColor = "rgb(255, 0, 0)";
        $('.x-table td').click(function() {
            if($(this).css('background-color') != newColor) {
                $(this).css('background', newColor);
            } else if($(this).css('background-color') == newColor) {
                $(this).css('background', oldColor);
            }
        });
    }, 1000);

    To retain the color of a row, input the following JS code:

    setTimeout(function() {
        //background color variables when selected
        var oldColor = "rgb(255, 255, 255)";
        var newColor = "rgb(255, 0, 0)";
        $('.x-table td').click(function() {
            $(this).css('background', newColor).siblings().css("background",oldColor);
        });
    }, 1000);

    III. Preview

    Save the templates and select [Dashboard Preview]. The preview effect is as shown below:

    IV. Download the template

    Attachment List


    Theme: FineVis Data Visualization
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy