JointJS动态Inspector列表内when条件失效问题咨询
解决JointJS Inspector
when: 条件不生效的问题 你遇到的问题大概率是when条件的语法用错了——JointJS Inspector的eq运算符并不支持对象形式的写法,而是需要用数组格式来指定属性路径和目标值。
正确的配置示例
针对你的场景,把when条件改成数组形式就能正常生效了:
const inspectorConfig = { inputs: { 'list': { type: 'object-list', itemType: 'object', items: { 'type': { type: 'select-box', options: [ { value: 'rule', label: 'Rule' }, { value: 'script', label: 'Script' } ] }, 'rule': { type: 'text', // 正确的eq条件格式:[属性路径, 目标值] when: { eq: ['type', 'rule'] } }, 'script': { type: 'text-multiline', when: { eq: ['type', 'script'] } } } } } }; // 初始化Inspector const inspector = new joint.ui.Inspector({ cell: yourJointCell, inputs: inspectorConfig.inputs, // 如果需要显示初始为空的字段,加上这个配置 showEmptyFields: true });
额外注意事项
- 确认Cell数据结构:确保你的JointJS Cell的数据结构和Inspector配置匹配,比如
list是一个数组,每个元素包含type、rule、script字段:yourJointCell.set('list', [ { type: 'rule', rule: 'some rule text', script: '' }, // 或者初始空对象 { type: '' } ]); - 显示空字段:如果
rule或script初始值为空或undefined,需要在Inspector初始化时加上showEmptyFields: true,否则Inspector会默认隐藏这些没有值的字段,即使when条件满足。
这样调整后,当你在type下拉框选择rule时,对应的rule输入框会显示;选择script时,script多行输入框会显示,完全符合你的需求。
内容的提问来源于stack exchange,提问作者VitorMM
相关产品推荐
相关产品推荐

