mui datatables responsiveness issue in mobile

Viewed 68

I am using mui-datatables@2.15.0 and @mui/material 5.9.1

I'm fixing bugs in existing project(actually code is written by someone else). MUIDataTable works properly in desktop view but in mobile view it breaks down and gives a ugly look

view in desktop

view in mobile

I want to show it in the mobile as it is in the desktop view with a horizontal scrollbar.

My code is here:

<MUIDataTable
            title={"Leave List"}
            data={data}
            columns={columns}
            options={{
              selectableRows: false,
              rowHover: false,
              filter: false,
              print: false,
              download: false,
            }}
          />

Columns data:

const columns = [
    {
      name: "_id",
      label: "Leave Id",
      options: {
        filter: true,
        sort: false,
        display: false,
      },
    },
    {
      name: "first_name",
      label: "First Name",
      options: {
        filter: true,
        sort: false,
      },
    },
    {
      name: "last_name",
      label: "Last Name",
      options: {
        filter: true,
        sort: false,
      },
    },

    {
      name: "leave_type",
      label: "Type",
      options: {
        filter: true,
        sort: false,
      },
    },
    {
      name: "date_start",
      label: "Start Date",
      options: {
        filter: true,
        sort: false,
      },
    },
    {
      name: "date_end",
      label: "End Date",
      options: {
        filter: true,
        sort: false,
      },
    },
    {
      name: "no_of_days",
      label: "No. of Days",
      options: {
        filter: true,
        sort: false,
      },
    },
    {
      name: "date_join",
      label: "Joining Date",
      options: {
        filter: true,
        sort: false,
      },
    },
    {
      name: "status",
      label: "Status",
      options: {
        filter: true,
        sort: false,
      },
    },
    {
      name: "actions",
      label: "Actions",
      options: {
        customBodyRender: (value, tableMeta) => {
          return (
            <>
              <VisibilityIcon
                color="action"
                fontSize="small"
                cursor="pointer"
                onClick={() => visiblityModal(tableMeta.rowData[0])}
              />
              <EditSharpIcon
                color="action"
                fontSize="small"
                cursor="pointer"
                onClick={() => updateModal(tableMeta.rowData[0])}
              />
              <DeleteIcon
                color="action"
                fontSize="small"
                cursor="pointer"
                onClick={() => deleteModal(tableMeta.rowData[0])}
              />
            </>
          );
        },
      },
    },
  ];
1 Answers

You can add responsive: 'scroll' in MUIDataTable options like this:

<MUIDataTable
              title={"Leave List"}
              data={data}
              columns={columns}
              options={{
                selectableRows: false,
                rowHover: false,
                filter: false,
                print: false,
                download: false,
                responsive: "scroll",
              }}
            />

It will add horizontal scrollbar to data table and the view would remain same both in mobile and desktop.

You can get more help from here: https://codesandbox.io/s/wy2rl1nyzl?resolutionWidth=320&resolutionHeight=675

Related