Creating a CRUD App with Patterns

WARNING

This document is machine translated.

DuoyunUI's pattern elements and helper modules let you quickly create a CRUD app (example, React example). This article will use:

  • <dy-pat-console> to create the app's basic layout
  • <dy-pat-table> to create the table page
  • helper/store to create a pagination data manager
  • helper/error to display error messages

Step 1: Create the App Framework

The <dy-pat-console> element uses a two‑column layout and fills the entire viewport. Inserting <dy-pat-console> into the body element shows the basic layout.

import { render, html } from '@mantou/gem';
import 'duoyun-ui/patterns/console';

render(html`<dy-pat-console></dy-pat-console>`, document.body);
import { createRoot } from 'react-dom/client';
import { DyPatConsole } from 'duoyun-ui/react/pat-console';

createRoot(document.body).render(<DyPatConsole />);

Define Routes and Sidebar Navigation

<dy-pat-console> uses <gem-route> for routing. Routes are not only used to match and display content, they can also serve as navigation parameters. The sidebar navigation of <dy-pat-console> also supports route format; define them together before rendering:

import { html } from '@mantou/gem';
import type { Routes, NavItems } from 'duoyun-ui/patterns/console';

const routes = {
  home: {
    pattern: '/',
    title: 'Home',
    getContent() {
      return html`Home`;
    },
  },
  item: {
    pattern: '/items/:id',
    title: 'Item Page',
    async getContent(params) {
      return html`<console-page-item>${JSON.stringify(params)}</console-page-item>`;
    },
  },
} satisfies Routes;

const navItems: NavItems = [
  routes.home,
  {
    ...routes.item,
    params: { id: crypto.randomUUID() },
  },
];
import { createRoot } from 'react-dom/client';
import type { Routes, NavItems } from 'duoyun-ui/patterns/console';

const routes = {
  home: {
    pattern: '/',
    title: 'Home',
    getContent(_, ele) {
      ele.react?.unmount();
      ele.react = createRoot(ele);
      ele.react.render(<>Home</>);
    },
  },
  item: {
    pattern: '/items/:id',
    title: 'Item Page',
    async getContent(params, ele) {
      ele.react?.unmount();
      ele.react = createRoot(ele);
      ele.react.render(<>{JSON.stringify(params)}</>);
    },
  },
} satisfies Routes;

const navItems: NavItems = [
  routes.home,
  {
    ...routes.item,
    params: { id: crypto.randomUUID() },
  },
];

WARNING

When rendering pages with React, to better compatible with Gem, you need to first unmount any previously mounted React Root node, then recreate a React Root and render.

Define User Info and Global Menu

Afterwards, you can specify user info to identify the user, and define some global commands, such as switching language or logging out:

import { Toast } from 'duoyun-ui/elements/toast';
import type { ContextMenus, UserInfo } from 'duoyun-ui/patterns/console';

const contextMenus: ContextMenus = [
  {
    text: 'Languages',
    handle: () => Toast.open('info', 'No Implement!'),
  },
  { text: '---' },
  {
    text: 'Logout',
    handle: () => Toast.open('error', 'No Implement!'),
    danger: true,
  },
];

const userInfo: UserInfo = {
  username: 'Mantou',
  org: 'DuoyunUI',
  profile: '/about',
};

Configure <dy-pat-console>

Modify the render function and specify other configuration options:

render(
  html`
    <dy-pat-console
      name="DuoyunUI"
      .logo=${'https://duoyun-ui.gemjs.org/logo.png'}
      .routes=${routes}
      .navItems=${navItems}
      .contextMenus=${contextMenus}
      .userInfo=${userInfo}
      .keyboardAccess=${true}
      .responsive=${true}
    ></dy-pat-console>
  `,
  document.body,
);
createRoot(document.body).render(
  <DyPatConsole
    name="DuoyunUI"
    logo="https://duoyun-ui.gemjs.org/logo.png"
    routes={routes}
    navItems={navItems}
    contextMenus={contextMenus}
    userInfo={userInfo}
    keyboardAccess={true}
    responsive={true}
  />,
);

Step 2: Create the Page

Now implement a table page with CRUD features. First, create a page that shows an empty table (item.ts) and modify the route:

import { html, GemElement, connectStore, customElement } from '@mantou/gem';
import 'duoyun-ui/patterns/table';

@customElement('console-page-item')
export class ConsolePageItemElement extends GemElement {
  render = () => {
    return html`<dy-pat-table></dy-pat-table>`;
  };
}
import { useState, useEffect } from 'react';
import { connect } from '@mantou/gem';
import { DyPatTable } from 'duoyun-ui/react/pat-table';

export function Item() {
  return <DyPatTable></DyPatTable>;
}
{
  item: {
    pattern: '/items/:id',
    title: 'Item Page',
    async getContent() {
+     await import('./item');
      return html`<console-page-item></console-page-item>`;
    },
  }
}
{
  item: {
    pattern: '/items/:id',
    title: 'Item Page',
    async getContent(params, ele) {
-      createRoot(ele).render(<>{JSON.stringify(params)}</>);
+      const { Item } = await import('./item');
+      createRoot(ele).render(<Item />);
    },
  },
}

Fetch List and Render Table

Next, fetch data from the backend and fill the table. URL parameters such as id can be read from locationStore, which is created by <dy-pat-console> to respond to app route updates. It does not get updates from pages that are loading but not yet displayed. You need to bind it to the page so that when id changes the page reacts.

import { html, GemElement, connectStore, customElement, createState, effect } from '@mantou/gem';
import { get } from '@mantou/gem/helper/request';
import { locationStore } from 'duoyun-ui/patterns/console';
import type { PatTableColumn } from 'duoyun-ui/patterns/table';
import 'duoyun-ui/patterns/table';

@customElement('console-page-item')
@connectStore(locationStore)
export class ConsolePageItemElement extends GemElement {
  #state = createState<{ data: any }>({});

  #columns: PatTableColumn<any>[] = [
    {
      title: 'No',
      dataIndex: 'id',
    },
  ];

  @effect((i) => [locationStore.params.id])
  #fetch = async ([id]) => {
    const data = await get(`https://jsonplaceholder.typicode.com/users`);
    this.#state({ data });
  };

  render = () => {
    return html`
      <dy-pat-table filterable .columns=${this.#columns} .data=${this.#state.data}></dy-pat-table>
    `;
  };
}
import { useState, useEffect } from 'react';
import { connect } from '@mantou/gem';
import { get } from '@mantou/gem/helper/request';
import { locationStore } from 'duoyun-ui/patterns/console';
import { DyPatTable, type PatTableColumn } from 'duoyun-ui/react/pat-table';

export function Item() {
  const [_, update] = useState({});
  useEffect(() => connect(locationStore, () => update({})), []);

  const [data, updateData] = useState();
  useEffect(() => {
    // const id = locationStore.params.id;
    get(`https://jsonplaceholder.typicode.com/users`).then(updateData);
  }, [locationStore.params.id]);

  const columns: PatTableColumn<any>[] = [
    {
      title: 'No',
      dataIndex: 'id',
    },
  ];

  return <DyPatTable filterable={true} columns={columns} data={data}></DyPatTable>;
}

Add Delete and Update Commands to Table Rows

Simply add a column with getActions:

import { ContextMenu } from 'duoyun-ui/elements/contextmenu';

const columns: PatTableColumn<any>[] = [
  // ...
  {
    title: '',
    getActions: (r, activeElement) => [
      {
        text: 'Edit',
        handle: () => {
          onUpdate(r);
        },
      },
      {
        text: 'Delete',
        danger: true,
        handle: async () => {
          await ContextMenu.confirm(`Confirm delete ${r.username}?`, {
            activeElement,
            danger: true,
          });
          console.log('Delete: ', r);
        },
      },
    ],
  },
];

Implement Update and Delete

First, define a form just like defining a table:

import type { FormItem } from 'duoyun-ui/patterns/form';

const formItems: FormItem<any>[] = [
  {
    type: 'text',
    field: 'username',
    label: 'Username',
    required: true,
  },
];

Then implement onCreate and onUpdate, and add a Create button to the page:

import { createForm } from 'duoyun-ui/patterns/form';

function onUpdate(r) {
  createForm({
    data: r,
    header: `Update: ${r.id}`,
    formItems: formItems,
    prepareOk: async (data) => {
      console.log(data);
    },
  }).catch((data) => {
    console.log(data);
  });
}

function onCreate() {
  createForm({
    type: 'modal',
    data: {},
    header: `Create`,
    formItems: formItems,
    prepareOk: async (data) => {
      console.log(data);
    },
  }).catch((data) => {
    console.log(data);
  });
}
// ...

html`
  <dy-pat-table filterable .columns=${this.#columns} .data=${this.state.data}>
    <dy-button @click=${onCreate}>Create</dy-button>
  </dy-pat-table>
`;
// ...

<DyPatTable filterable={true} columns={columns} data={data}>
  <DyButton onClick={onCreate}>Create</DyButton>
</DyPatTable>;

Step 3: Server‑Side Pagination (Optional)

So far the app has pagination, search, and filter features, but they are implemented on the client side, meaning you must provide all data to <dy-pat-table> at once. In a real production environment, pagination, search, and filter are usually handled by the backend; only a small change is needed:

// ...

html`
  <dy-pat-table
    filterable
    .columns=${this.#columns}
    .paginationStore=${pagination.store}
    @fetch=${this.#onFetch}
  >
    <dy-button @click=${onCreate}>Create</dy-button>
  </dy-pat-table>
`;
// ...

<DyPatTable
  filterable={true}
  columns={columns}
  paginationStore={pagination.store}
  onfetch={onFetch}
>
  <DyButton onClick={onCreate}>Create</DyButton>
</DyPatTable>;

Here, store is created using createPaginationStore:

import { Time } from 'duoyun-ui/lib/time';
import { createPaginationStore } from 'duoyun-ui/helper/store';
import type { FetchEventDetail } from 'duoyun-ui/patterns/table';

const pagination = createPaginationStore({
  storageKey: 'users',
  cacheItems: true,
  pageContainItem: true,
});

// Simulate a real API
const fetchList = (args: FetchEventDetail) => {
  return get(`https://jsonplaceholder.typicode.com/users`).then((list) => {
    list.forEach((e, i) => {
      e.updated = new Time().subtract(i + 1, 'd').getTime();
      e.id += 10 * (args.page - 1);
    });
    return { list, count: list.length * 3 };
  });
};

const onFetch = ({ detail }: CustomEvent<FetchEventDetail>) => {
  pagination.updatePage(fetchList, detail);
};
Optimize Search Result Display (Optional)

When switching between having a search term and having none, the page cannot instantly switch to the new list. You can assign a separate pagination to the search term:

@customElement('console-page-item')
export class ConsolePageItemElement extends GemElement {
  #state = createState({
    pagination: pagination,
    paginationMap: new Map([['', pagination]]),
  });

  #onFetch = ({ detail }: CustomEvent<FetchEventDetail>) => {
    let pagination = this.#state.paginationMap.get(detail.searchAndFilterKey);
    if (!pagination) {
      pagination = createPaginationStore<Item>({
        cacheItems: true,
        pageContainItem: true,
      });
      this.#state.paginationMap.set(detail.searchAndFilterKey, pagination);
    }
    this.#state({ pagination });
    pagination.updatePage(fetchList, detail);
  };
}

TIP

<dy-pat-table> also supports:

  • Using expandedRowRender to expand rows, and @expand to get the expand event
  • Using selectable to make the table selectable, and using getSelectedActions to add commands for selection

Step 4: Handle Errors

At the top of the main file, import helper/error; it shows error messages via Toast and can also display unhandled rejected Promises:

import 'duoyun-ui/helper/error';

Now this CRUD app should look like this.