简体   繁体   English

如果前一个单元格具有值 HandsOnTable,如何将单元格更改为非活动状态

[英]How create change cell to inactive if previous cell has value HandsOnTable

I'm new to JavaScript. I creating a table with cells where we can add values and I'm using HandsOnTable.我是 JavaScript 的新手。我创建了一个包含单元格的表格,我们可以在其中添加值,我正在使用 HandsOnTable。 I need to create an inactive cell and we can't set the value in the inactive cell in HandsOnTable if the previous cell has a value.我需要创建一个非活动单元格,如果前一个单元格有值,我们无法在 HandsOnTable 的非活动单元格中设置值。

It's my code:这是我的代码:

<div id="downtimetable"></div>

<script script type="text/javascript" th:inline="javascript">
    let dataFromSpringDown = [[${downtimes}]];
    let dataObjDown = [];
    let temp1 = {
                        name: ' ',
                        town: ' ',
                        tent: ' ',
                        izoterm: ' ',
                        ref: ' '
    }
    for(let obj of dataFromSpringDown){
        let object = {
                        name: obj["name"],
                        town: obj["town"],
                        tent: obj["tent"],
                        izoterm: obj["izoterm"],
                        ref: obj["ref"]
                    };
    dataObjDown.push(object);
    }
    dataObjDown.push(temp);

        let container2 = document.getElementById('downtimetable');
        let hot2 = new Handsontable(container2, {
          data: dataObjDown,
          rowHeaders: true,
          colHeaders: true,
           autoWrapRow: true,
            colHeaders: [
    'Name',
    'Town',
    'Cost'
  ],
          manualRowMove: true,
          manualColumnMove: true,
          contextMenu: true,
          filters: true,
          dropdownMenu: true,
          collapsibleColumns: true,
          nestedHeaders : [
          [
           'Name',
            'Town',
            {
            label: 'Cost',
            colspan: 3
            }
           ],
           [
           '','','Tent','Izo','Ref'
           ]
           ],
           manualColumnResize : true
        });

        function myFunctionDown() {
            var json = JSON.stringify(dataObjDown);
            var xhr = new XMLHttpRequest();
            xhr.open("POST","/downtime_rows_json");
            xhr.setRequestHeader("Content-Type","application/json");
            xhr.send(json);
        }

    </script>

<button onclick="myFunctionDown()" class="btn btn-info">From table</button>

It's a table created with script:这是一个用脚本创建的表:

在此处输入图像描述

I need to change the status to inactive in cell2 if cell1 has a value and vice versa.如果 cell1 有值,我需要在 cell2 中将状态更改为非活动状态,反之亦然。 How I can do that?我该怎么做?

I think we can use this script, but I don't understand how get the previous cell我想我们可以使用这个脚本,但我不明白如何获取前一个单元格

 hot2.updateSettings({
    cells: function (row, col, prop) {
      var cellProperties = {};
  
      if (hot2.getDataAtRowProp(row, prop) === 'Town1') {
        cellProperties.editor = false;
      } else {
        cellProperties.editor = 'text';
      }
  
      return cellProperties;
    }
  })

The code below will disable cell 2 and delete its value if cell 1 has a value and vice versa.如果单元格 1 有值,下面的代码将禁用单元格 2 并删除它的值,反之亦然。 In other words: you can't have values in both column 1 and 2.换句话说:您不能在第 1 列和第 2 列中都有值。

hot2.addHook( 'afterChange', function( changes, src ) {
  [
    [row, prop, oldVal, newVal] 
  ] = changes;
  if ( prop == 0 && hot2.getDataAtRowProp( row, prop + 1 ) && newVal?.length > 0 ) {
    // delete value of cell 2 if cell 1 has a value
    hot2.setDataAtCell( row, prop + 1, '' );
  } else if ( prop == 1 && hot.getDataAtRowProp( row, prop - 1 ) && newVal?.length > 0 ) {
    // delete value of cell 1 if cell 2 has a value
    hot2.setDataAtCell( row, prop -1, '' );
  }
})


hot2.updateSettings( {
   cells: function ( row, col, prop ) {
     cellProperties = {};

     if ( prop == 1 && hot2.getDataAtRowProp( row, prop - 1 ) ) {
       // this disables cell 2 if cell 1 has a value
       cellProperties.readOnly = true;
     } else if ( prop == 0 && hot2.getDataAtRowProp( row, prop + 1 ) ) {
       // this disables cell 1 if cell 2 has a value
       cellProperties.readOnly = true;
     } else {
       cellProperties.readOnly = false;
     }
     return cellProperties;
   }
})

It's working for me:它对我有用:

hot1.addHook('beforeRenderer', function(td, row, col, prop, value, cellProperties) {
            if (prop === 'name') {
               var cellMeta = this.getCellMeta(row, this.propToCol('town'));
               cellMeta.readOnly = (value != ' ' && value != '' && value != null) ? true : false;
           } if (prop === 'town') {
                var cellMeta = this.getCellMeta(row, this.propToCol('name'));
             cellMeta.readOnly = (value != ' ' && value != '' && value != null) ? true : false;
          } 
      });

You can change the columns name, and it's will still working您可以更改列名,它仍然有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Handsontable:如何在渲染功能中更改单元格值 - Handsontable : how to change cell value in render function 如何使jQuery .change()引用Handsontable上的单元格? - How to get jQuery .change() to reference cell on a Handsontable? 如何根据Handsontable中另一个单元格的条件/值将样式应用于单元格 - How to apply style to a cell based on the condition/value of another cell in Handsontable 如何根据Handsontable中的表格单元格值设置表格单元格外观 - How to set table cell appearance depending on table cell value in Handsontable HandsOnTable:动态更改单元格边框 - HandsOnTable: Change cell borders dynamically 使用或不使用 setDataAtCell/getDataAtCell handsontable 更改单元格值 afterChange - change cell value afterChange with or without setDataAtCell/getDataAtCell handsontable 如何在单元格中下拉值的变化中仅在几个列上加载数据? - How to load data only on few columns in handsontable on change in the value of a dropdown in a cell? 如何更改 Handsontable 中已更改单元格的颜色? - How can I change the color of a changed cell in Handsontable? Handsontable-隐藏特定行(按单元格值) - Handsontable - hide specific row ( by cell value) 使用handsontable获取单元格的值-错误:方法不是函数 - Get the value of a cell with handsontable - Error : method is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM