# Can't set property to all cells of one table in the doc with more than one tables

**URL:** <https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833>\
**Category:** Uncategorized\
**Created:** [August 17, 2022, 2:38pm UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833 "2022-08-17T14:38:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![canalun](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/canalun/32/3017_2.png) [@canalun](https://discuss.prosemirror.net/u/canalun)\
**Post date:** [August 17, 2022, 2:38pm UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/1 "2022-08-17T14:38:03Z")

</div>

Hi, would be really glad if someone told me how to handle the situation. Many thanks in advance…!

**[situation]**

I’m trying to set property to all cells of one table. ( **Please assume the doc has more than one tables**.) But I’ve never been able to find the way doing so now.

I set a command to my custom table cell as below, and call the command when the bubble menu is clicked with the focus on a cell of the targeted table.

```auto
// In the extension of TableCell...
addCommands() {
  return {
    ...this.parent?.(),
    setBorderStyleToAllTableCells: (tableNode, borderStyle) => ({ tr }) => {
      tableNode.descendants((node, pos) => {
        if (node.type.name === 'tableCell') {
          tr.setNodeMarkup(pos, null, {
            borderStyle: 'border-none',
          })
        }
      })
      return true
    },
  }
}

///////////

// When the bubble menu is clicked with the focus to a cell of the targeted table...
onClick={(borderStyle) => {
const tableNode = findParentNode(
      ({ type }) => type.name === 'table'
    )(editor.state.selection)

    if (tableNode?.node) {
      editor.commands.setAllTableCellsBorderStyle(
        tableNode?.node,
        'none'
      )
    }

    onSetSubContent(null) // just close the bubble menu
}}

```

**[problem]**

But that doesn’t work, because it seems that, in the method `setBorderStyleToAllTableCells`, the method `descendants` gives pos **relative to the parent node** while the method `setNodeMarkup` recognizes the given pos as absolute pos (i.e. pos **relative to the doc** ). I noticed it when debugging.

I also tried to add the pos of the table cell as the offset, but neither did it work…(i couldn’t clarify the reason)

```auto
// in setBorderStyleToAllTableCells
const getNodePos = (node) => {
    let pos = -1
    tr.doc.forEach((childNode, _pos) => {
      if (childNode.eq(node)) {
        pos = _pos
      }
    })
    return pos
}
const tablePos = getNodePos(tableNode)
tableNode.descendants((node, pos) => {
  if (node.type.name === 'tableCell') {
    tr.setNodeMarkup(pos + tablePos, null, {
      borderStyle: 'border-none',
    })
  }
})

```

**[additional info]**

I found a similar post here.

> [@Set property of all table cells](https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591/5):
>
> Fantastic slight_smile This works: this.doc.descendants((node, pos) =\> { if (node.type.name === "table\_cell" && node.attrs.background) { tr.setNodeMarkup(pos, null, setAttr(node.attrs, "background", null)) } })

But it didn’t work.

The problem is that, in my case, **the doc has more than one tables**. That means it’s not helpful to iterate all nodes in the doc and set property if the node is ‘table\_cell’ as introduced in the above post.

really looking forward to someone’s great help…many thanks!

---

<div class="post-metadata">

**Author:** ![shinTSY](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/shintsy/32/2889_2.png) [@shinTSY](https://discuss.prosemirror.net/u/shinTSY)\
**Post date:** [August 18, 2022, 2:10am UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/2 "2022-08-18T02:10:35Z")

</div>

You can use `tr.doc.nodesBetween(tablePos, tablePos + tableNodeSize, (node, pos) => { // your code... })`

---

<div class="post-metadata">

**Author:** ![canalun](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/canalun/32/3017_2.png) [@canalun](https://discuss.prosemirror.net/u/canalun)\
**Post date:** [August 18, 2022, 4:35am UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/3 "2022-08-18T04:35:08Z")

</div>

Hi shinTSY,

That WORKS!! We highly appreciate your prompt response…!!!

For those who have the same situation, the below is where we reached finally. just FYI.

```auto
// In the extension of TableCell...
addCommands() {
    return {
      ...this.parent?.(),
      setBorderStyleToAllTableCells: (borderStyle) => ({ tr }) => {
        const tableNode = findParentNode(({ type }) => type.name === 'table')(
          this.editor.state.selection
        )
        const getNodePos = (node: Node) => {
          let pos = -1
          tr.doc.forEach((childNode, _pos) => {
            if (childNode.eq(node)) {
              pos = _pos
            }
          })
          return pos
        }
        const tablePos = getNodePos(tableNode?.node)

        tr.doc.nodesBetween(
          tablePos,
          tablePos + tableNode?.node.nodeSize,
          (node, pos) => {
            if (node.type.name === 'tableCell') {
              tr.setNodeMarkup(pos, null, {
                borderStyle: `${borderStyle}`,
              })
            }
          }
        )
        return true
      },
    }
  },

///////////

// When the bubble menu is clicked with the focus to a cell of the targeted table...
onClick={(borderStyle) => {
      editor.commands.setBorderStyleToAllTableCells(borderStyle)
      onSetSubContent(null)
}}

```

Many thanks, shinTSY!!

---

<div class="post-metadata">

**Author:** ![shinTSY](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/shintsy/32/2889_2.png) [@shinTSY](https://discuss.prosemirror.net/u/shinTSY)\
**Post date:** [August 18, 2022, 7:04am UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/4 "2022-08-18T07:04:02Z")

</div>

You’re very welcome. HAHAHA 😆

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [August 18, 2022, 10:15am UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/5 "2022-08-18T10:15:49Z")

</div>

Also note that using node object identity like this (`getNodePos`) is very error-prone—it is valid for a the same node object to occur multiple times in the document, and changes to a node’s content will replace it with another object.

---

<div class="post-metadata">

**Author:** ![canalun](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/canalun/32/3017_2.png) [@canalun](https://discuss.prosemirror.net/u/canalun)\
**Post date:** [August 18, 2022, 1:57pm UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/6 "2022-08-18T13:57:41Z")

</div>

Thanks for noting that, marijn!! I got it. So, in this case, could you please also tell me what you think is the best alternative?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [August 18, 2022, 2:20pm UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/7 "2022-08-18T14:20:32Z")

</div>

You’ll have to track nodes by position, not by object.

---

<div class="post-metadata">

**Author:** ![canalun](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/canalun/32/3017_2.png) [@canalun](https://discuss.prosemirror.net/u/canalun)\
**Post date:** [August 18, 2022, 3:21pm UTC](https://discuss.prosemirror.net/t/cant-set-property-to-all-cells-of-one-table-in-the-doc-with-more-than-one-tables/4833/8 "2022-08-18T15:21:42Z")

</div>

ok…but i couldn’t find the way getting the position of the table of the selected cell…

So how should i get the position of the table from the focus on its cell? is there any function returning the position of the node…? 🤔
