ホームページ  >  記事  >  ウェブフロントエンド  >  iView で編集可能なテーブルを実装する方法の紹介 (コード付き)

iView で編集可能なテーブルを実装する方法の紹介 (コード付き)

不言
不言転載
2019-02-20 14:08:075209ブラウズ

この記事では、iView で編集可能なテーブルを実装する方法を紹介します (コード付き)。必要な方は参考にしていただければ幸いです。

コンポーネント

<i-table highlight-row ref="currentRowTable" :columns="columns" :data="tableData"></i-table>

実装方法:

  • 現在編集する必要がある列の ID (デフォルト) を記録します。空です

  • 編集する必要がある列は、編集する必要がある現在の ID と一致します。成功すると、列は入力タグ コンポーネントとしてレンダリングされ、入力イベントにバインドされます。

データ処理

export default {
    data () {
        return {
            currentId: '',
            currentScore: '',
            columns: [
                { title: '名称', key: 'name', align: 'center' },
                {
                title: '班级',
                align: 'center',
                render: (h, p) => {
                    const { id, score } = p.row
                    const inp = h('input', {
                    style: {
                        width: '30%',
                        padding: '4px 2px',
                        borderRadius: '4px',
                        border: '1px solid #e9eaec',
                        textAlign: 'center'
                    },
                    attrs: {
                        maxlength: 16
                    },
                    domProps: {
                        value: score
                    },
                    on: {
                            input: (event) => {
                            this.currentScore = event.target.value
                        }
                    }
                    })
                    return this.currentId === p.row.id ? inp : h('span', score)
                }
                },
                {
                title: '操作',
                align: 'center',
                render: (h, p) => {
                    const { currentId } = this
                    const { id } = p.row
                    const btnEdit = h('i-button', {
                    on: {
                        click: () => {
                            this.currentId = id
                        }
                    }
                    }, '编辑')

                    const btnSaveCancel = [
                        h('i-button', {
                            on: {
                            click: () => {
                                this.handleSave(id)
                            }
                            }
                        }, '保存'),
                        h('i-button', {
                            on: {
                            click: () => {
                                this.currentId = ''
                                this.currentScore = ''
                            }
                            }
                        }, '取消')]
                    return currentId === id ? h('p', btnSaveCancel) : btnEdit
                }
                }
            ],
            tableData: [
                { id: 1, name: 1, score: 1 },
                { id: 2, name: 2, score: 2 }]
        }
    },

    methods: {
        handleSave (id) {
            const {currentScore, tableData} = this
            this.tableData = tableData.map(v => {
                return v.id === id ? { ...v, score: currentScore } : v
            })
            this.currentId = ''
            this.currentScore = ''
        }
    }
}

注: iView が head タグに導入されている場合、このメソッドは、この方法で開発されたプロジェクトでは無効になります。コンパイルは可能です;

以上がiView で編集可能なテーブルを実装する方法の紹介 (コード付き)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はsegmentfault.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。