Google图表表格:编辑单元格

时间:2018-09-21 01:09:39

标签: javascript google-visualization

是否可以编辑Google图表表格单元格?。

我只需要编辑“薪水”列中的单元格。然后确认新值不大于“最高薪金”列中的值。

到目前为止,我只能在单击时获得行和列。

这是示例代码。

google.load('visualization', '1', {
    packages: ['table']
});
google.setOnLoadCallback(drawTable);
var row, col;
function drawTable() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Salary');
    data.addColumn('number', 'Age');
    data.addColumn('number', 'Max Salary');
    data.addRows([
      ['Mike', {
          v: 10000,
          f: '$10,000'
      },
        30,
       50000
      ],
      ['Jim', {
          v: 8000,
          f: '$8,000'
      },
        25,
       50000
      ],
      ['Alice', {
          v: 12500,
          f: '$12,500'
      },
        43,
       50000
      ],
      ['Bob', {
          v: 7000,
          f: '$7,000'
      },
        50,
       50000
      ]
    ]);
    
    var table = new google.visualization.Table(document.getElementById('table_div'));
    google.visualization.events.addListener(table, 'select', function(){
        selectHandler(table);
    });
    
  function selectHandler(table) {
  var selection = table.getSelection();
  if(selection.length === 0)
      return;

  var cell = event.target; //get selected cell
  row = selection[0].row;
  col = cell.cellIndex;
  document.getElementById('output').innerHTML = "Row: " +  selection[0].row + " Column: " +  cell.cellIndex;
  
}
  
    table.draw(data, {
        showRowNumber: false      
    });  
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="table_div"></div>
<div id="output"></div>

1 个答案:

答案 0 :(得分:1)

如果要允许用户编辑单元格,
您可以设置属性contentEditable = true

这将允许用户更改值

然后您可以使用'blur'事件检查最高工资

以下是一个粗略的工作示例...

google.charts.load('current', {
  packages: ['table']
}).then(function() {
  var row, col;
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Name');
  data.addColumn('number', 'Salary');
  data.addColumn('number', 'Age');
  data.addColumn('number', 'Max Salary');
  data.addRows([
    ['Mike', {
        v: 10000,
        f: '$10,000'
      },
      30,
      50000
    ],
    ['Jim', {
        v: 8000,
        f: '$8,000'
      },
      25,
      50000
    ],
    ['Alice', {
        v: 12500,
        f: '$12,500'
      },
      43,
      50000
    ],
    ['Bob', {
        v: 7000,
        f: '$7,000'
      },
      50,
      50000
    ]
  ]);

  var table = new google.visualization.Table(document.getElementById('table_div'));
  google.visualization.events.addListener(table, 'select', function() {
    selectHandler(table);
  });

  var formatSalary = new google.visualization.NumberFormat({
    pattern: '$#,##0'
  });

  function selectHandler(table) {
    var selection = table.getSelection();
    if (selection.length === 0)
      return;

    var cell = event.target; //get selected cell
    row = selection[0].row;
    col = cell.cellIndex;
    if (cell.cellIndex === 1) {
      cell.contentEditable = true;
      cell.addEventListener('blur', checkSalary);
    }
    table.setSelection([]);
  }

  function checkSalary(sender) {
    var rowIndex = sender.target.parentNode.rowIndex - 1;
    var salary = parseFloat(sender.target.innerHTML);
    var maxSalary = data.getValue(rowIndex, 3);
    if (!isNaN(salary)) {
      if (salary <= maxSalary) {
        sender.target.innerHTML = formatSalary.formatValue(salary);
        document.getElementById('output').innerHTML = 'Salary successfully changed.';
        data.setCell(rowIndex, 1, salary, formatSalary.formatValue(salary));
        drawTable();
      } else {
        sender.target.innerHTML = data.getFormattedValue(rowIndex, 1);
        document.getElementById('output').innerHTML = 'Error: Salary exceeded max.';
      }
    } else {
      document.getElementById('output').innerHTML = 'Error: Salary not a number.';
    }
    sender.target.contentEditable = false;
    sender.target.removeEventListener('blur', checkSalary);
  }

  drawTable();
  function drawTable() {
    table.draw(data, {
      showRowNumber: false
    });
  }
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="table_div"></div>
<div id="output"></div>