JS Disable the Delete Row Button When Only One Row is Left

  • Last update:  2021-01-22
  • I. Overview

    1.1 Expected effect

    In a form, if only one row of data is left, that row of data cannot be deleted.

    1.gif

     

    1.2 Implementation ideas

    This can be achieved by adding JS events or setting conditional formatting.

    II. Example

    2.1 Prepare template

    Create a new template and add a dataset:

    SELECT * FROM product order by productid limit 1

    The dataset only takes out one row of data from the table:

    2.png

     

    2.2 Add button

    1) Add a button to the cell L3.

    The button type is: insert row, and the specified cell is: B3,  as shown in the figure below:

    3.png

    2) In the same way, add a button to M3.

    The type is: delete row, the specified cell is: B3, the widget name is set to sc1, and the state is not enabled, as shown in the following figure:

    4.png

    Note: If there are other delete row buttons in the template, and they should achieve the same effect, they can be named to sc2, sc3, etc. in turns.

    The final style of the report is shown below:

    5.png


    2.3 Realization effect

    1) Method 1: Add JS event

    Click Template > Web Attributes, select Data Entry Settings, check Individually set for the template, and then add a Loading End event:

    6.png

    JavaScript codes:

    var arr=["sc1"];//sc1 is the widget name
    for(j=0;j<arr.length;j++){ //traverse to get the widgets in the array
    var self=contentPane.getWidgetsByName(arr[j]);
    var flag=0;
    for (i=0;i<self.length;i++){
    var tr=self[i].element.parent().parent().css("display");
    if(tr=="table-row"){
        flag++;
        }
    }
    if(flag==1){ //If there is only one widget left, the button is set to not available
        for (i=0;i<self.length;i++){
          var tr2=self[i].element.parent().parent().css("display");
          if(tr2=="table-row"){
            self[i].setEnable(false);
          }
       }
    }
    else{ //If there is more than one widget, you can delete it
        for(i=0;i<self.length;i++){
            self[i].setEnable(true);
    }
        }
    }

     

    2) Method 2: Add conditional formatting to buttons

    In addition to adding JS events, this effect can also be achieved by setting conditional formatting.

    Change the state of Delete Row button in M3 to available, and add conditional formatting.

    The attribute controlled by the conditional formatting is: Widget. Uncheck Use Widget. The formula condition is: row()=3, which makes the Delete Row button in the third row (the row where the first data is located) can not take effect:

    7.png

     

    2.4 Effect preview

    1) PC terminal

    Save the report, click Data Entry Preview, and the actual results of the two methods are as follows:

    Method 1:

    1 (1).gif

    Method 2:

    2.gif

    2) Mobile terminal:

    Method 1 does not support mobile terminals. The effect of method 2 on the mobile terminal is shown in the figure below:

    3.gif

    III. Completed Template

    Attachment List


    Theme: Data Entry
    • 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