I have a DataTable object that contains all the data for a Table visualization.

I want to provide a search facility for users of the table, filtering the rows from the Table based on the search query, and then update the visualization with the rows that matched.

DataTable provides the getFilteredRows method, but this is not going to be flexible enough (AND conditions for each column simply won't work, for example).

What are my options? All the data already exists client-side, so I should be able to manipulate it using JS code. Is there library functionality that will allow me to do this?

有帮助吗?

解决方案

The answer, for now, appears to be no. I came up with this dreadfully-inefficient function as a stop-gap:

  function search(query, table) {

      if(query == '') {
          return table;
      }

      var newTable = table.clone();
          newTable.removeRows(0, table.getNumberOfRows());
      var keepRows = [];
      for(i = 0; i < table.getNumberOfRows(); i++) {
          for (j = 0; j < table.getNumberOfColumns(); j++) {
                if(table.getValue(i, j).toLowerCase().match(query.toLowerCase())) {
                    keepRows.push(i);
                    break;
                }
        }
      }

      for(r = 0; r < keepRows.length; r++) {
            var row = []
          for(c = 0; c < table.getNumberOfColumns(); c++ ) {
              row.push(table.getValue(keepRows[r], c));
          }

          newTable.addRow(row);
      }

      return newTable;
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top