level_list.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
  2. import { http } from '../http';
  3. import { config } from '../config';
  4. import { level_list_item_data } from '../../data/data';
  5. import { level_list_item } from './level_list_item';
  6. import { tools } from '../tools';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('level_list')
  9. export class level_list extends Component {
  10. @property(Node) content:Node = null;
  11. @property(Prefab) item:Prefab = null;
  12. @property(Node) page_number:Node = null;
  13. @property(Node) btn_previous_page:Node = null;
  14. @property(Node) lab_current_page:Node = null;
  15. @property(Node) btn_next_page:Node = null;
  16. private m_click_callback = null;
  17. private m_cur_page:number = 1;
  18. private m_every_page_count:number = 18;
  19. private m_total_page_count:number = 0;
  20. private m_prefab_node_list:Node[] = [];
  21. protected start(): void {
  22. this.page_number.active = false
  23. let self = this
  24. this.btn_previous_page.on(Node.EventType.TOUCH_END, ()=> {
  25. if(self.m_cur_page == 1) {
  26. tools.showToast("已是第一页")
  27. return
  28. }
  29. self.m_cur_page-=1
  30. self.requestData()
  31. },this)
  32. this.btn_next_page.on(Node.EventType.TOUCH_END, ()=> {
  33. if(self.m_cur_page >= self.m_total_page_count) {
  34. tools.showToast("已是最后一页")
  35. return
  36. }
  37. self.m_cur_page+=1
  38. self.requestData()
  39. },this)
  40. }
  41. public init(call_back){
  42. this.m_click_callback = call_back;
  43. this.requestData()
  44. }
  45. initView(list:level_list_item_data[]){
  46. if(this.m_prefab_node_list.length == 0) {
  47. this.content.removeAllChildren()
  48. for (let index = 0; index < list.length; index++) {
  49. const element = list[index];
  50. let node = instantiate(this.item)
  51. node.parent = this.content;
  52. this.m_prefab_node_list.push(node)
  53. node.getComponent(level_list_item).initView(element,this.m_click_callback)
  54. }
  55. } else {
  56. for(let index = 0; index < this.m_prefab_node_list.length; index++) {
  57. let node = this.m_prefab_node_list[index]
  58. if(index < list.length) {
  59. node.active = true
  60. const element = list[index]
  61. node.getComponent(level_list_item).initView(element,this.m_click_callback)
  62. } else {
  63. node.active = false
  64. }
  65. }
  66. }
  67. }
  68. requestData() {
  69. let request_data = {"page":this.m_cur_page,"limit":this.m_every_page_count}
  70. http.post("/tool/mysnote/levels",request_data,(err,data)=>{
  71. if(!err){
  72. // console.log(data)
  73. let _data = JSON.parse(data);
  74. let __data = _data.content
  75. if(this.m_cur_page == 1) {
  76. this.page_number.active = true
  77. this.m_total_page_count = Math.ceil(__data.tool_number/this.m_every_page_count)
  78. }
  79. this.setupLabNumber()
  80. config.last_id = parseInt(_data.content.zujian_id)
  81. console.log("level_list",config.last_id )
  82. this.initView(__data.list)
  83. }
  84. })
  85. }
  86. setupLabNumber() {
  87. let string = this.m_cur_page + ' / ' + this.m_total_page_count
  88. this.lab_current_page.getComponent(Label).string = string
  89. }
  90. }