Skip to content

常用脚本方法使用技巧

常用全局组件方法

获取组件参数,拿到的是组件的 props 的值,可以获取值,也可以修改值,这个使用的比较多。

js
console.log(app.getPropsById("form_1742944").value);
app.getPropsById("form_1742944").value = "123";

该页面设置参数,如:当点击列表中某一项拿到 id,可以通过app.setParam("key", id);将 id 设置为参数,然后在其他需要使用的组件中的脚本编辑器中可以通过app.getParam("key")获取到 id。

js
app.setParam("key", 123);
console.log(app.getParam("key")); //123

传递参数给引入低代码组件的地方,参数自定义,可以传任何东西到组件引入的地方,这样可以做到组件和业务项目的交互。

js
app.componentEvent({ type: "push", data: { name: "张三", age: 18 } });

设置组件显示隐藏

js
app.setVisible("form_1742944", false); //隐藏
app.setVisible("form_1742944", false); //显示

脚本中请求方法,和请求适配器中的方法同样,只不过是代码中调用,所有请求 method 方法都有。

js
let res = await app.$http.post("url", {});
//或者用promise
app.$http.post("url", {}).then((res) => {
  console.log(res);
});

confirm 确定

js
app
  .$confirm("是否确定?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
  .then(() => {})
  .catch(() => {});

表单

设置表单类型:新增,修改,查看

js
//Create新增,Update修改,Detail查看
app.setFormType("form_1742944", "Create");

获取表单数据

js
//拿到的是个json对象
app.getWidgetListResultById("form_1742944");

重置表单数据

js
app.resetWidgetListById("form_1742944");

手动提交表单数据,将走表单适配器的配置方法和脚本。

js
app.getMethodById("form_1742944").submit();

手动设置表单数据,传入对象,key 为表单内配置的字段名称,value 为字段值。

js
app.inputWidgetListValueById("form_1742944", {
  name: "张三",
  age: 18,
});

数据表格

获取选中行的数据,如果表格是单选模式则返回一个对象,对象是该选中的条的数据,如果是多选模式,则选中的是一个数组,数组中是每个选中的条的数据。

js
app.getMethodById("datatable_344981").chosen();

刷新表格数据方法

js
app.getMethodById("datatable_344981").refresh();

获取所有列表数据

js
app.getMethodById("datatable_344981").getRows();

设置列表数据

js
app.getMethodById("datatable_344981").setRows([]);

获取表格所有查询参数,通常用于导出参数传递

js
app.getMethodById("datatable_344981").getSearchParams();

树形表格获取所有选中的值

js
app.getMethodById("datatable_344981").allTreeChosen();

树形表格通过 id 设置选中

js
app.getMethodById("datatable_344981").setRowsCheckedByIds([]);

树形表格设置展开/收起

js
//true展开,false收起
app.getMethodById("datatable_344981").setAllTreeExpand(true);

新增一行数据

js
app.getMethodById("datatable_344981").addRow({});

删除一行数据

js
app.getMethodById("datatable_344981").removeRow(row);

折叠表格设置展开收起

js
app.getMethodById("datatable_344981").setAllRowExpand(true);