Pen Settings

JavaScript

Babel includes JSX processing.

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Flutter

              
                import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: DataTableDemo(),
    ),
  );
}
class DataTableDemo extends StatefulWidget {
  @override
  _DataTableDemoState createState() => _DataTableDemoState();
}

class _DataTableDemoState extends State<DataTableDemo> {
  List<Employee> emps;
  List<Employee> selectedEmps;
  int selectedSort = 2;
  String selectedName;
  final nameController = TextEditingController();
  final editNameController = TextEditingController();
  final lastNameController = TextEditingController();
  bool sort = true;
  bool flag = true;
  List<dynamic> labels = [
    {'name': 'First Name', 'num': true, 'col': 'firstName'},
    {'name': 'Last Name', 'num': true, 'col': 'lastName'},
    {'name': 'Id', 'num': true, 'col': 'id'},
    {'name': 'Email Id', 'num': true, 'col': 'email'},
  ];
  @override
  void initState() {
    // sort = false;
    selectedEmps = [];
    emps = Employee.getList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Data Table Demo'),
        actions:[
          IconButton(
            icon: Icon(Icons.delete),
            onPressed: (){
              setState(() {
                selectedEmps.forEach((element) {
                  emps.remove(element);
                });
              });

            },
          )
        ]
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: tableBody(
              context,
            ),
          ),
           ],
      ),
    );
  }

  SingleChildScrollView tableBody(BuildContext ctx) {
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: DataTable(
          dataRowHeight: 50,
          dividerThickness: 5,
          sortAscending: sort,
          sortColumnIndex: selectedSort,
          showCheckboxColumn: true,
          columns: labels
              .map(
                (name) => DataColumn(
                  label: Text(
                    name['name'],
                    style: TextStyle(
                      fontStyle: FontStyle.italic,
                      color: Colors.deepOrange,
                    ),
                  ),
                  numeric: name['num'],
                  onSort: (columnIndex, ascending) {
                     setState(() {
                      sort = !sort;
                      selectedSort = columnIndex;
                    });
                    onSortColumn(columnIndex, ascending);
                  },
                ),
              )
              .toList(),
                rows: emps
              .map(
                (emp) => DataRow(
                    selected: selectedEmps.contains(emp),
                    onSelectChanged: (b) {
                      print("Onselect");
                      onSelectedRow(b, emp);
                    },
                    cells: [
                      DataCell(
                         Text(emp.firstName),
                                 ),
                      DataCell(
                        Text(emp.lastName),
                           ),
                      DataCell(
                        Text('${emp.id}'),
                     
                      ),
                      DataCell(
                        Text(emp.email),
                      ),
                    ]),
              )
              .toList(),
        ),
      ),
    );
  }

  onSortColumn(int columnIndex, bool ascending) {
 
     bool numeric = labels[columnIndex]['num'];
   
    if (numeric != true) {
  
      if (ascending) {
        emps.sort((a, b) => a.firstName.compareTo(b.firstName));
      } else {
        emps.sort((a, b) => b.firstName.compareTo(a.firstName));
      }
    } else {
      if (ascending) {
        Comparator<Employee> idComparator = (a, b) => a.id.compareTo(b.id);
        emps.sort(idComparator);
      } else {
        Comparator<Employee> idComparator = (a, b) => b.id.compareTo(a.id);
        emps.sort(idComparator);
      }
    }
  }

  onSelectedRow(bool selected, Employee emp) async {
    setState(() {
      if (selected) {
        selectedEmps.add(emp);
      } else {
        selectedEmps.remove(emp);
      }
    });
    print(selectedEmps);
  }

}

class Employee {
  String firstName;
  String lastName;
  int id;
  String email;

  Employee({this.firstName, this.lastName, this.id, this.email});

  static List<Employee> getList() {
    return <Employee>[
      Employee(
          firstName: "Shubham",
          lastName: "B",
          id: 2001,
          email: "shubham.b@walkingtree.tech"),
      Employee(
          firstName: "HariKrishna",
          lastName: "S",
          id: 1002,
          email: "harikrishna.s@walkingtree.tech"),
      Employee(
          firstName: "Vishnu",
          lastName: "P",
          id: 2003,
          email: "vishnu.p@walkingtree.tech"),
      Employee(
          firstName: "Suman",
          lastName: "Ravuri",
          id: 1004,
          email: "Suman.ravuri@walkingtree.tech "),
      Employee(
          firstName: "Prashanth",
          lastName: "K",
          id: 1006,
          email: "Prashanth.k@walkingtree.tech "),
      Employee(
          firstName: "Shubham2",
          lastName: "B",
          id: 1007,
          email: "shubham.b@walkingtree.tech"),
      Employee(
          firstName: "HariKrishna2",
          lastName: "S",
          id: 1008,
          email: "harikrishna.s@walkingtree.tech"),
      Employee(
          firstName: "Vishnu2",
          lastName: "P",
          id: 1009,
          email: "vishnu.p@walkingtree.tech"),
      Employee(
          firstName: "Suman2",
          lastName: "Ravuri",
          id: 1011,
          email: "Suman.ravuri@walkingtree.tech "),
      Employee(
          firstName: "Prashanth2",
          lastName: "K",
          id: 1021,
          email: "Prashanth.k@walkingtree.tech "),
      Employee(
          firstName: "Shubham3",
          lastName: "B",
          id: 1022,
          email: "shubham.b@walkingtree.tech"),
      Employee(
          firstName: "HariKrishna3",
          lastName: "S",
          id: 1023,
          email: "harikrishna.s@walkingtree.tech"),
      Employee(
          firstName: "Vishnu3",
          lastName: "P",
          id: 1024,
          email: "vishnu.p@walkingtree.tech"),
      Employee(
          firstName: "Suman3",
          lastName: "Ravuri",
          id: 1025,
          email: "Suman.ravuri@walkingtree.tech "),
      Employee(
          firstName: "Prashanth3",
          lastName: "K",
          id: 1026,
          email: "Prashanth.k@walkingtree.tech "),
      Employee(
          firstName: "Shubham4",
          lastName: "B",
          id: 1027,
          email: "shubham.b@walkingtree.tech"),
      Employee(
          firstName: "HariKrishna4",
          lastName: "S",
          id: 1028,
          email: "harikrishna.s@walkingtree.tech"),
      Employee(
          firstName: "Vishnu4",
          lastName: "P",
          id: 1029,
          email: "vishnu.p@walkingtree.tech"),
      Employee(
          firstName: "Suman4",
          lastName: "Ravuri",
          id: 1031,
          email: "Suman.ravuri@walkingtree.tech "),
      Employee(
          firstName: "Prashanth4",
          lastName: "K",
          id: 1032,
          email: "Prashanth.k@walkingtree.tech ")
    ];
  }
}

              
            
!
999px

Console