Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ui/src/views/system/identity/roles/components/RoleMemberList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { computed, ref, useTemplateRef, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type { Dict, OptionItem, RoleItem, RoleMember } from '@/api/types'
import type { TableColumnCtx } from 'element-plus'
import { ROLE_TYPE } from '@/api/enums'
import RoleApi from '@/api/admin/system/role'
import AddMemberDrawer from '../AddMemberDrawer.vue'
Expand Down Expand Up @@ -37,6 +38,28 @@ function handleSearch(query?: Dict<unknown>) {
loadMembers(true)
}

/** 同一用户的选择框、姓名和用户名按连续记录合并。 */
function objectSpanMethod({
column,
row,
rowIndex,
}: {
row: RoleMember
column: TableColumnCtx<RoleMember>
rowIndex: number
columnIndex: number
}) {
const shouldMerge = column.type === 'selection' || column.property === 'nick_name' || column.property === 'username'
if (!shouldMerge) return

const userId = row.user_id
if (members.value[rowIndex - 1]?.user_id === userId) return [0, 0]

let rowspan = 1
while (members.value[rowIndex + rowspan]?.user_id === userId) rowspan += 1
return [rowspan, 1]
}

// 添加成员

const addMemberDrawerRef = useTemplateRef<InstanceType<typeof AddMemberDrawer>>('addMemberDrawerRef')
Expand Down Expand Up @@ -112,6 +135,8 @@ watch(
v-model:pagination-config="paginationConfig"
v-loading="loading"
:data="members"
selection-group-key="user_id"
:span-method="objectSpanMethod"
@current-change="loadMembers()"
@size-change="loadMembers()"
:max-table-height="290"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of the diff

Correctness / potential issues

  1. column.type === 'selection' assumption is fragile. With span-method, Element Plus still renders one selection checkbox per row, but merged rows get [0, 0], so hidden checkboxes disappear. The header "select all" and the internal selection state then no longer map 1:1 to visible checkboxes. Verify that selection-group-key="user_id" + span merging yields the intended UX (e.g., selecting a merged row selects only the first visually rendered row). Relying on column.type === 'selection' instead of column.property is correct for the checkbox column, but confirm it stays stable across Element Plus versions.

  2. Merge requires contiguous, sorted records. objectSpanMethod merges only consecutive rows with the same user_id. If the backend returns users interleaved (e.g., by role), you'll get multiple fragments per user and unstable spans. Ensure data ordering guarantees grouping, or sort client-side before rendering (but beware sorting interacts with server pagination).

  3. Pagination breaks the merge. Merging is computed from members.value only, so a user spanning a page boundary will render as separate groups on each page. Acceptable, but worth documenting.

  4. Whole-table scan is O(n²) in the worst case. For every non-merged row the while loop re-scans forward. On large lists, this is repeated work. A single precomputed pass is cheaper:

    const spanMap = computed(() => {
      const m = new Map<number, [number, number]>()
      const rows = members.value
      for (let i = 0; i < rows.length; ) {
        let j = i + 1
        while (j < rows.length && rows[j].user_id === rows[i].user_id) j++
        m.set(i, [j - i, 1])
        for (let k = i + 1; k < j; k++) m.set(k, [0, 0])
        i = j
      }
      return m
    })

    Then lookup by rowIndex in objectSpanMethod. Note: if you memoize, key it off members reactively (the computed does that) rather than calling inside the render function each time.

  5. Reactivity inside span-method. Reading members.value inside a render-time callback is fine since it's reactive, but the method is invoked frequently during table layout. The precomputed map above avoids repeated array access.

  6. row.user_id may be undefined/null. If user_id is missing, undefined === undefined merges all such rows together incorrectly. Add a guard:

    if (userId == null) return
  7. Signature duplication. The inline parameter type duplicates TableColumnCtx<RoleMember> details; consider using the exported helper type for span-method params (or TableSpanMethod) to avoid drift.

Style / minor

  1. Lines exceed typical line length (the shouldMerge expression). Consider wrapping.
  2. Comment is in Chinese while the request asked for English review — just noting consistency, not a bug.
  3. while (...) rowspan += 1 could use braces for readability.

Summary

Logic is basically sound for contiguous, non-paginated groups. Main risks: interleaved/unordered data, pagination splits, null user_id, and the check-all semantics of merged selection columns. Recommend replacing the per-row forward scan with a precomputed span map and adding guards.

Expand Down
Loading