目录

列表控件专题

本文介绍五种经典列表场景使用

  • 场景一:列表到明细页的传值;
  • 场景二:列表的分页效果;
  • 场景三:列表的分组效果;
  • 场景四:列表左滑时出现的button个数和样式;
  • 场景五: 创建列表时动态修改列表里cell的信息

场景一

使用说明:展示列表的绑定数据源和导航到明细页。

步骤

1、新建页面listsingle,把列表控件拖入设计器,设置绑定字段属性为list。分别拖入三个标签控件,绑定字段分别绑定code、name、price。其中list、code、name、price这些字段在下面的js代码中会有定义。如图:

2、列表控件上增加onload事件以便能够加载数据,方法名定为loaddata。如图:

此时生成的DSL为:

  1. <listView id="listviewdefine0" bindfield="list" onload="loaddata()">
  2. <div id="panel0">
  3. <div id="panel1">
  4. <label id="label0" bindfield="code">label</label>
  5. <label id="label1" bindfield="name">label</label>
  6. <label id="label2" bindfield="price">label</label>
  7. </div>
  8. <div id="panel2"/>
  9. </div>
  10. </listView>

3、确定生成代码,在loaddata中加如下代码:

  1. function loaddata(sender, args) {
  2. var list = [];
  3. for(var i = 0; i <= 20; i++) {
  4. var json = {
  5. "code" : i,
  6. "name" : "商品" + i,
  7. "price" : 10 * i
  8. };
  9. list.push(json);
  10. }
  11. var json = {
  12. list : list
  13. };
  14. $ctx.push(json);
  15. }

4、增加明细页面detail。完成文本框的数据绑定code、name、price。如图:

5、在listsingle页面中的列表控件中标签所在的容器上增加onclick事件。Dsl代码如下:

6、在opendetail方法中编写如下代码:

  1. function opendetail(sender, args) {
  2. $view.open({
  3. "viewid" : "com.lpsoft.listdemo.Detail", //目标页面(首字母大写)全名,
  4. "isKeep" : "true", //保留当前页面不关闭
  5. "row" : $id("listviewdefine0").get("row")
  6. });
  7. }

7、在detail页面增加onload事件并增加如下代码:

Binddata方法:

  1. function binddata(sender, args){
  2. var json=$param.getJSONObject("row");
  3. $ctx.push(json);
  4. }

场景二

功能说明:模拟列表分页的效果。
使用说明:在此基础上,修改列表控件load数据的逻辑并且增加列表上下分页的事件。

步骤

1、增加列表下翻事件。如图:

2、增加下翻逻辑。

  1. function down(sender, args) {
  2. var oldjson = $ctx.getJSONArray("list");
  3. var length = oldjson.length;
  4. var json = {
  5. list : oldjson
  6. };
  7. for(var i = length; i < length + 20; i++) {
  8. var line = {};
  9. line["code"] = i;
  10. line["name"] = "商品" + i;
  11. line["price"] = "$" + i;
  12. json.list.push(line);
  13. }
  14. $ctx.push(json);
  15. }

场景三

功能说明:分组效果。
步骤

1、新建一个新的页面mlist。从组合控件中见分组列表控件拖到页面中。设置绑定字段为list。并且增加onload事件。如图:

2、增加onload逻辑。
注意修改listview背景色,不能为白色

  1. function linedelete(sender, args) {
  2. var row = $id("listviewdefine0").get("row");
  3. delteRow($stringToJSON(row));
  4. }
  5. function delteRow(row) {
  6. var sourceR = $ctx.getJSONArray("list");
  7. for(var i = 0; i < sourceR.length; i++) {
  8. var group = sourceR[i];
  9. if (group.id == row.id) {
  10. sourceR.splice(i, 1);
  11. break;
  12. }
  13. }
  14. var json = {};
  15. json["list"] = sourceR;
  16. $ctx.push(json);
  17. }

场景四

功能说明:列表左滑时出现的button个数和样式。

说明:可以随意定义左滑时出现的button个数和样式,imagebutton支持的属性创建button时都支持

设计器上进行如下操作:

  1. <listView id="listviewdefine0" bindfield="source420" linedelshow="true" onload="this.onload()">
  2. <div id="panel0">
  3. <div id="panel1">
  4. <label id="label0" bindfield="row420">label</label>
  5. </div>
  6. <div id="panel2"/>
  7. </div>
  8. </listView>
  1. function com$requirements$SlidingitemController$onload(sender, args) {
  2. var rightflipper = [{
  3. "halign" : "center",
  4. "text-align" : "right",
  5. "width" : "70",
  6. "icon-width" : "12",
  7. "istogglebutton" : "false",
  8. "font-pressed-color" : "#ffffff",
  9. "icon-height" : "12",
  10. "height" : "59", //与列表行高度相同即可
  11. "color" : "#ffffff",
  12. "layout-type" : "linear",
  13. "background" : "#FF615A",
  14. "font-size" : "12",
  15. "icon-background-image" : "audit_refuse.png",
  16. "value" : "拒绝",
  17. "icon-pressed-image" : "audit_refuse.png",
  18. "font-family" : "default",
  19. "valign" : "center",
  20. "checked" : "false",
  21. "icon-text-spacing" : "8",
  22. "onclick" : "a()"
  23. }, {
  24. "halign" : "center",
  25. "text-align" : "right",
  26. "width" : "70",
  27. "icon-width" : "12",
  28. "istogglebutton" : "false",
  29. "font-pressed-color" : "#ffffff",
  30. "icon-height" : "12",
  31. "height" : "59",
  32. "color" : "#ffffff",
  33. "layout-type" : "linear",
  34. "background" : "#15AD64",
  35. "font-size" : "12",
  36. "icon-background-image" : "audit_agree.png",
  37. "value" : "同意",
  38. "icon-pressed-image" : "audit_agree.png",
  39. "font-family" : "default",
  40. "valign" : "center",
  41. "checked" : "false",
  42. "icon-text-spacing" : "8",
  43. "onclick" : "b()"
  44. }];
  45. $id("listviewdefine0").set("slidingitems", rightflipper);
  46. var json = {
  47. source420 : [{
  48. row420 : "支持操纵列表cell"
  49. }, {
  50. row420 : "分组按字母排序"
  51. }, {
  52. row420 : "laber支持html"
  53. }, {
  54. row420 : "获取本地联系人所有信息"
  55. }, {
  56. row420 : "列表左滑用户能自行设置"
  57. }]
  58. };
  59. $ctx.push(json);
  60. }
  61. function a() {
  62. $alert("审核通过");
  63. }
  64. function b() {
  65. $alert("审核不通过");
  66. }

效果如下:

场景五

1、增加onitemcreate事件

  1. <!--列表行创建事件-->
  2. <listView id="listviewdefine0" bindfield="cell" onitemcreate="myRowCreate()">
  3. <div id="panel0">
  4. <div id="panel1">
  5. <label id="label0">label</label>
  6. </div>
  7. <div id="panel2"/>
  8. </div>
  9. </listView>

args结构如下:

  1. function myRowCreate(sender, args) {
  2. args = {
  3. "event":{
  4. "childindex" : 1,
  5. "groupindex" : 2,
  6. "row":[{},{}]//数组,每一数组项是一个JSON,包含了控件,如:{"controlid" : "label0", "color" : "#0000FF", "value" : "绿"}
  7. }
  8. }

2、在onitemcreate事件中对itemsource属性赋值,实现对行内控件赋值*

说明:onitemcreate事件中,对itemsource赋值时

  1. //1、修改指定组的指定行内控件-------分组列表改行
  2. $id("listviewdefine0").initListChildItem({
  3. "groupindex" : groupindex, //指定的分组的索引号
  4. "childindex" : childindex, //当前的child行的索引号
  5. "row" : [{
  6. "controlid" : "label20",
  7. "visible" : "true",
  8. "color" : "#e3e3e3"
  9. //还可以增加类似上述的其他dsl属性key-value值
  10. }]
  11. })
  12. //2、修改指定组内控件-------分组列表改组
  13. $id("listviewdefine0").initListGroupItem({
  14. "groupindex" : groupindex, //指定的分组的索引号
  15. "row" : [{
  16. "controlid" : "label20",
  17. "visible" : "true",
  18. "color" : "#e3e3e3"
  19. //还可以增加类似上述的其他dsl属性key-value值
  20. }]
  21. })
  22. //3、修改指定行内控件--------普通行列表改行
  23. $id("listviewdefine0").initListChildItem({
  24. "childindex" : 1, //当前的child行的索引号,从0开始
  25. "row" : [{
  26. "controlid" : "label0",
  27. "visible" : "false",
  28. "color" : "#e3e3e3"
  29. //还可以增加类似上述的其他dsl属性key-value值
  30. }]
  31. })
  1. function create(sender, args) {
  2. $alert(args);
  3. args.event = $stringToJSON(args.event)
  4. if (args.event.childindex == 0) {
  5. // 您的代码逻辑
  6. }
  7. if (args.event.childindex == 1) {
  8. // 您的代码逻辑
  9. }
  10. }
文档更新时间: 2018-01-15 14:26