Google Chart Tables: Edit cells

萝らか妹 提交于 2020-03-26 04:59:40

问题


Is there a way to edit a google chart table cell?.

I need to edit the cells at the 'Salary' column only. Then verify the new value it's not greater than the value at 'Max Salary' column.

So far I been able only to get the row and column when clicked.

This is the example code.

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:


if you want to allow the user to edit the cell,
you can set attribute contentEditable = true

this will allow the user to change the value

then you can use the 'blur' event to check against the max salary

following is a rough working example...

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>


来源:https://stackoverflow.com/questions/52435268/google-chart-tables-edit-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!