Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.00 KB | None | 0 0
  1.  
  2. <!DOCTYPE HTML>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <link rel="shortcut icon" type="image/ico" href="favicon.ico" />
  7. <title>SlickGrid example 5: Collapsing</title>
  8. <link rel="stylesheet" href="../slick.grid.css" type="text/css" />
  9. <link rel="stylesheet" href="../css/smoothness/jquery-ui-1.11.3.custom.css" type="text/css" />
  10. <link rel="stylesheet" href="examples.css" type="text/css" />
  11. <style>
  12. .cell-title {
  13. font-weight: bold;
  14. }
  15.  
  16. .cell-effort-driven {
  17. text-align: center;
  18. }
  19.  
  20. .toggle {
  21. height: 9px;
  22. width: 9px;
  23. display: inline-block;
  24. }
  25.  
  26. .toggle.expand {
  27. background: url(../images/expand.gif) no-repeat center center;
  28. }
  29.  
  30. .toggle.collapse {
  31. background: url(../images/collapse.gif) no-repeat center center;
  32. }
  33.  
  34. .slick-cell.selected {
  35. /*background-color: #B2E9ED;*/
  36. background-color: #CDE6F7;
  37. }
  38.  
  39. .slick-row.active .slick-cell {
  40. /*background-color: #B2E9ED;*/
  41. background-color: #CDE6F7;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <table width="100%">
  47. <tr>
  48. <td valign="top" width="50%">
  49. <div style="border:1px solid gray;background:wheat;padding:6px;">
  50. <label>Show tasks with % at least: </label>
  51.  
  52. <div style="padding:4px;">
  53. <div style="width:100px;" id="pcSlider"></div>
  54. </div>
  55. <br />
  56. <label>And title including:</label>
  57. <input type=text id="txtSearch">
  58. </div>
  59. <br />
  60.  
  61. <div id="myGrid" style="width:600px;height:500px;"></div>
  62. </td>
  63. <td valign="top">
  64. <h2>Demonstrates:</h2>
  65. <ul>
  66. <li>implementing expand/collapse as a filter for DataView</li>
  67. </ul>
  68.  
  69. <h2>View Source:</h2>
  70. <ul>
  71. <li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example5-collapsing.html" target="_sourcewindow"> View the source for this example on Github</A></li>
  72. </ul>
  73. </td>
  74. </tr>
  75. </table>
  76.  
  77. <script src="../lib/firebugx.js"></script>
  78.  
  79. <script src="../lib/jquery-1.11.2.min.js"></script>
  80. <script src="../lib/jquery-ui-1.11.3.min.js"></script>
  81. <script src="../lib/jquery.event.drag-2.3.0.js"></script>
  82.  
  83. <script src="../slick.core.js"></script>
  84. <script src="../slick.formatters.js"></script>
  85. <script src="../slick.editors.js"></script>
  86. <script src="../slick.grid.js"></script>
  87. <script src="../slick.dataview.js"></script>
  88.  
  89. <script src="../plugins/slick.rowselectionmodel.js"></script>
  90. <script>
  91. var dataView;
  92. function requiredFieldValidator(value) {
  93. if (value == null || value == undefined || !value.length) {
  94. return { valid: false, msg: "This is a required field" };
  95. } else {
  96. return { valid: true, msg: null };
  97. }
  98. }
  99.  
  100.  
  101. var TaskNameFormatter = function (row, cell, value, columnDef, dataContext) {
  102. if (value == null || value == undefined || dataContext === undefined) { return ""; }
  103.  
  104. value = value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
  105. var spacer = "<span style='display:inline-block;height:1px;width:" + (15 * dataContext["indent"]) + "px'></span>";
  106. var idx = dataView.getIdxById(dataContext.id);
  107. if (data[idx + 1] && data[idx + 1].indent > data[idx].indent) {
  108. if (dataContext._collapsed) {
  109. return spacer + " <span class='toggle expand'></span>&nbsp;" + value;
  110. } else {
  111. return spacer + " <span class='toggle collapse'></span>&nbsp;" + value;
  112. }
  113. } else {
  114. return spacer + " <span class='toggle'></span>&nbsp;" + value;
  115. }
  116. };
  117. var grid;
  118. var data = [];
  119. var columns = [
  120. { id: "title", name: "Title", field: "title", width: 220, cssClass: "cell-title", formatter: TaskNameFormatter, validator: requiredFieldValidator },
  121. { id: "duration", name: "Duration", field: "duration" },
  122. { id: "%", name: "% Complete", field: "percentComplete", width: 80, resizable: false, formatter: Slick.Formatters.PercentCompleteBar, editor: Slick.Editors.PercentComplete },
  123. { id: "start", name: "Start", field: "start", minWidth: 60 },
  124. { id: "finish", name: "Finish", field: "finish", minWidth: 60 },
  125. { id: "effort-driven", name: "Effort Driven", width: 80, minWidth: 20, maxWidth: 80, cssClass: "cell-effort-driven", field: "effortDriven", formatter: Slick.Formatters.Checkmark, cannotTriggerInsert: true }
  126. ];
  127.  
  128. var options = {
  129. editable: true,
  130. enableAddRow: true,
  131. enableCellNavigation: true,
  132. asyncEditorLoading: false,
  133. multiSelect:false
  134. };
  135.  
  136. var percentCompleteThreshold = 0;
  137. var searchString = "";
  138.  
  139. function myFilter(item) {
  140. if (item["percentComplete"] < percentCompleteThreshold) {
  141. return false;
  142. }
  143.  
  144. if (searchString != "" && item["title"].indexOf(searchString) == -1) {
  145. return false;
  146. }
  147.  
  148. if (item.parent != null) {
  149. var parent = data[item.parent];
  150.  
  151. while (parent) {
  152. if (parent._collapsed || (parent["percentComplete"] < percentCompleteThreshold) || (searchString != "" && parent["title"].indexOf(searchString) == -1)) {
  153. return false;
  154. }
  155.  
  156. parent = data[parent.parent];
  157. }
  158. }
  159.  
  160. return true;
  161. }
  162.  
  163. function percentCompleteSort(a, b) {
  164. return a["percentComplete"] - b["percentComplete"];
  165. }
  166.  
  167. $(function () {
  168. var indent = 0;
  169. var parents = [];
  170.  
  171. // prepare the data
  172. for (var i = 0; i < 1000; i++) {
  173. var d = (data[i] = {});
  174. var parent;
  175.  
  176. if (Math.random() > 0.8 && i > 0) {
  177. indent++;
  178. parents.push(i - 1);
  179. } else if (Math.random() < 0.3 && indent > 0) {
  180. indent--;
  181. parents.pop();
  182. }
  183.  
  184. if (parents.length > 0) {
  185. parent = parents[parents.length - 1];
  186. } else {
  187. parent = null;
  188. }
  189.  
  190. d["id"] = "id_" + i;
  191. d["indent"] = indent;
  192. d["parent"] = parent;
  193. d["title"] = "Task " + i;
  194. d["duration"] = "5 days";
  195. d["percentComplete"] = Math.round(Math.random() * 100);
  196. d["start"] = "01/01/2009";
  197. d["finish"] = "01/05/2009";
  198. d["effortDriven"] = (i % 5 == 0);
  199. }
  200.  
  201.  
  202. // initialize the model
  203. dataView = new Slick.Data.DataView({ inlineFilters: true });
  204. dataView.beginUpdate();
  205. dataView.setItems(data);
  206. dataView.setFilter(myFilter);
  207. dataView.endUpdate();
  208.  
  209.  
  210. // initialize the grid
  211. grid = new Slick.Grid("#myGrid", dataView, columns, options);
  212. grid.setSelectionModel(new Slick.RowSelectionModel({
  213. selectActiveRow: true
  214. }));
  215. grid.setSelectedRows([]);
  216. dataView.syncGridSelection(grid);
  217. grid.onCellChange.subscribe(function (e, args) {
  218. dataView.updateItem(args.item.id, args.item);
  219. });
  220.  
  221. grid.onAddNewRow.subscribe(function (e, args) {
  222. var item = {
  223. "id": "new_" + (Math.round(Math.random() * 10000)),
  224. "indent": 0,
  225. "title": "New task",
  226. "duration": "1 day",
  227. "percentComplete": 0,
  228. "start": "01/01/2009",
  229. "finish": "01/01/2009",
  230. "effortDriven": false
  231. };
  232. $.extend(item, args.item);
  233. dataView.addItem(item);
  234. });
  235.  
  236. grid.onClick.subscribe(function (e, args) {
  237. if ($(e.target).hasClass("toggle")) {
  238. var item = dataView.getItem(args.row);
  239. if (item) {
  240. if (!item._collapsed) {
  241. item._collapsed = true;
  242. } else {
  243. item._collapsed = false;
  244. }
  245.  
  246. dataView.updateItem(item.id, item);
  247. }
  248. e.stopImmediatePropagation();
  249. }
  250. });
  251.  
  252.  
  253. // wire up model events to drive the grid
  254. dataView.onRowCountChanged.subscribe(function (e, args) {
  255. grid.updateRowCount();
  256. grid.render();
  257. });
  258.  
  259. dataView.onRowsChanged.subscribe(function (e, args) {
  260. grid.invalidateRows(args.rows);
  261. grid.render();
  262. });
  263.  
  264.  
  265. var h_runfilters = null;
  266.  
  267. // wire up the slider to apply the filter to the model
  268. $("#pcSlider").slider({
  269. "range": "min",
  270. "slide": function (event, ui) {
  271. Slick.GlobalEditorLock.cancelCurrentEdit();
  272.  
  273. if (percentCompleteThreshold != ui.value) {
  274. window.clearTimeout(h_runfilters);
  275. h_runfilters = window.setTimeout(dataView.refresh, 10);
  276. percentCompleteThreshold = ui.value;
  277. }
  278. }
  279. });
  280.  
  281.  
  282. // wire up the search textbox to apply the filter to the model
  283. $("#txtSearch").keyup(function (e) {
  284. Slick.GlobalEditorLock.cancelCurrentEdit();
  285.  
  286. // clear on Esc
  287. if (e.which == 27) {
  288. this.value = "";
  289. }
  290.  
  291. searchString = this.value;
  292. dataView.refresh();
  293. })
  294. })
  295. function toggleItem(itemId) {
  296. var target = dataView.getItemById(itemId);
  297. console.log(target)
  298. if (target) {
  299. target._collapsed = !target._collapsed;
  300. dataView.updateItem(target.id, target);
  301. }
  302. }
  303. </script>
  304. <button onaction="toggleItem('id_7')">Toggle id_7</button>
  305. </body>
  306. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement