listview.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function ListView(element){
  2. this.element=element;
  3. }
  4. ListView.generateTable=function(collumns){
  5. var html='<table>';
  6. html+='<thead>';
  7. $.each(collumns,function(index,collumn){
  8. html+='<th>'+collumn+'</th>';
  9. });
  10. html+='<thead>';
  11. html+='</head>';
  12. html+='<tbody>';
  13. html+='<tr class="template">';
  14. $.each(collumns,function(index,collumn){
  15. html+='<th class="'+collumn.toLower()+'"</th>';
  16. });
  17. html+='</tr>';
  18. html+='</tbody>';
  19. html='</table>';
  20. return $(html);
  21. };
  22. ListView.prototype={
  23. rows:{},
  24. hoverElements:{},
  25. addRow:function(id,data,extraData){
  26. var tr=this.element.find('tr.template').clone();
  27. tr.removeClass('template');
  28. $.each(data,function(name,value){
  29. tr.children('td.'+name).text(value);
  30. tr.attr('data-'+name,value);
  31. });
  32. $.each(extraData,function(name,value){
  33. tr.attr('data-'+name,value);
  34. });
  35. this.rows[id]=data;
  36. tr.data('id',id);
  37. this.element.children('tbody').append(tr);
  38. },
  39. removeRow:function(id){
  40. this.rows[id].remove();
  41. delete this.rows[id];
  42. },
  43. hoverHandeler:function(tr){
  44. $.each(this.hoverElement,function(index,collumn){
  45. $.each(collumn,function(index,element){
  46. var html='<a href="#" title="'+element.title+'" class="hoverElement"/>';
  47. var element=$(html);
  48. element.append($('<img src="'+element.icon+'"/>'));
  49. element.click(element.callback);
  50. tr.children('td.'+collumn).append(element);
  51. });
  52. });
  53. if(this.deleteCallback){
  54. }
  55. },
  56. hoverHandelerOut:function(tr){
  57. tr.find('*.hoverElement').remove();
  58. },
  59. addHoverElement:function(collumn,icon,title,callback){
  60. if(!this.hoverElements[collumn]){
  61. this.hoverElements[collumn]=[];
  62. }
  63. this.hoverElements[row].push({icon:icon,callback:callback,title:title});
  64. },
  65. empty:function(){
  66. this.element.children('tr:not(.template)').remove();
  67. }
  68. };