SlickGrid example 5:带子项的展开收缩

带子项的展开收缩。

代码:

<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>SlickGrid example 5: Collapsing</title>
  <link rel="stylesheet" href="../slick.grid.css" type="text/css"/>
  <link rel="stylesheet" href="../css/smoothness/jquery-ui-1.8.16.custom.css" type="text/css"/>
  <link rel="stylesheet" href="examples.css" type="text/css"/>
  <style>
    .cell-title {
      font-weight: bold;
    }

    .cell-effort-driven {
      text-align: center;
    }

    .toggle {
      height: 9px;
      width: 9px;
      display: inline-block;
    }

    .toggle.expand {
      background: url(../images/expand.gif) no-repeat center center;
    }

    .toggle.collapse {
      background: url(../images/collapse.gif) no-repeat center center;
    }

  </style>
</head>
<body>
<table width="100%">
  <tr>
    <td valign="top" width="50%">
      <div style="border:1px solid gray;background:wheat;padding:6px;">
        <label>Show tasks with % at least: </label>

        <div style="padding:4px;">
          <div style="width:100px;" id="pcSlider"></div>
        </div>
        <br/>
        <label>And title including:</label>
        <input type=text id="txtSearch">
      </div>
      <br/>

      <div id="myGrid" style="width:600px;height:500px;"></div>
    </td>
    <td valign="top">
      <h2>Demonstrates:</h2>
      <ul>
        <li>implementing expand/collapse as a filter for DataView</li>
      </ul>
    </td>
  </tr>
</table>

<script src="../lib/firebugx.js"></script>

<script src="../lib/jquery-1.7.min.js"></script>
<script src="../lib/jquery-ui-1.8.16.custom.min.js"></script>
<script src="../lib/jquery.event.drag-2.0.min.js"></script>

<script src="../slick.core.js"></script>
<script src="../slick.formatters.js"></script>
<script src="../slick.editors.js"></script>
<script src="../slick.grid.js"></script>
<script src="../slick.dataview.js"></script>

<script>
function requiredFieldValidator(value) {
  if (value == null || value == undefined || !value.length) {
    return {valid: false, msg: "This is a required field"};
  } else {
    return {valid: true, msg: null};
  }
}

var TaskNameFormatter = function (row, cell, value, columnDef, dataContext) {
  value = value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
  var spacer = "<span style=‘display:inline-block;height:1px;width:" + (15 。 dataContext["indent"]) + "px‘></span>";
  var idx = dataView.getIdxById(dataContext.id);
  if (data[idx + 1] && data[idx + 1].indent > data[idx].indent) {
    if (dataContext._collapsed) {
      return spacer + " <span class=‘toggle expand‘></span>&nbsp;" + value;
    } else {
      return spacer + " <span class=‘toggle collapse‘></span>&nbsp;" + value;
    }
  } else {
    return spacer + " <span class=‘toggle‘></span>&nbsp;" + value;
  }
};

var dataView;
var grid;
var data = [];
var columns = [
  {id: "title", name: "Title", field: "title", width: 220, cssClass: "cell-title", formatter: TaskNameFormatter, editor: Slick.Editors.Text, validator: requiredFieldValidator},
  {id: "duration", name: "Duration", field: "duration", editor: Slick.Editors.Text},
  {id: "%", name: "% Complete", field: "percentComplete", width: 80, resizable: false, formatter: Slick.Formatters.PercentCompleteBar, editor: Slick.Editors.PercentComplete},
  {id: "start", name: "Start", field: "start", minWidth: 60, editor: Slick.Editors.Date},
  {id: "finish", name: "Finish", field: "finish", minWidth: 60, editor: Slick.Editors.Date},
  {id: "effort-driven", name: "Effort Driven", width: 80, minWidth: 20, maxWidth: 80, cssClass: "cell-effort-driven", field: "effortDriven", formatter: Slick.Formatters.Checkmark, editor: Slick.Editors.Checkbox, cannotTriggerInsert: true}
];

var options = {
  editable: true,
  enableAddRow: true,
  enableCellNavigation: true,
  asyncEditorLoading: false
};

var percentCompleteThreshold = 0;
var searchString = "";

function myFilter(item) {
  if (item["percentComplete"] < percentCompleteThreshold) {
    return false;
  }

  if (searchString != "" && item["title"].indexOf(searchString) == -1) {
    return false;
  }

  if (item.parent != null) {
    var parent = data[item.parent];

    while (parent) {
      if (parent._collapsed || (parent["percentComplete"] < percentCompleteThreshold) || (searchString != "" && parent["title"].indexOf(searchString) == -1)) {
        return false;
      }

      parent = data[parent.parent];
    }
  }

  return true;
}

function percentCompleteSort(a, b) {
  return a["percentComplete"] - b["percentComplete"];
}

$(function () {
  var indent = 0;
  var parents = [];

  // prepare the data
  for (var i = 0; i < 1000; i++) {
    var d = (data[i] = {});
    var parent = null;

    if (Math.random() > 0.8) {
      indent++;
      parent = i - 1;
      parents.push(parent);
    } else if (Math.random() < 0.3 && indent > 0) {
      indent--;
      parent = parents.pop();
    } else if (parents.length > 0) {
      parent = parents[parents.length - 1];
    }

    if (indent == 0) {
      parent = null;
    }

    d["id"] = "id_" + i;
    d["indent"] = indent;
    d["parent"] = parent;
    d["title"] = "Task " + i;
    d["duration"] = "5 days";
    d["percentComplete"] = Math.round(Math.random() * 100);
    d["start"] = "01/01/2009";
    d["finish"] = "01/05/2009";
    d["effortDriven"] = (i % 5 == 0);
  }

  // initialize the model
  dataView = new Slick.Data.DataView({ inlineFilters: true });
  dataView.beginUpdate();
  dataView.setItems(data);
  dataView.setFilter(myFilter);
  dataView.endUpdate();

  // initialize the grid
  grid = new Slick.Grid("#myGrid", dataView, columns, options);

  grid.onCellChange.subscribe(function (e, args) {
    dataView.updateItem(args.item.id, args.item);
  });

  grid.onAddNewRow.subscribe(function (e, args) {
    var item = {
      "id": "new_" + (Math.round(Math.random() * 10000)),
      "indent": 0,
      "title": "New task",
      "duration": "1 day",
      "percentComplete": 0,
      "start": "01/01/2009",
      "finish": "01/01/2009",
      "effortDriven": false};
    $.extend(item, args.item);
    dataView.addItem(item);
  });

  grid.onClick.subscribe(function (e, args) {
    if ($(e.target).hasClass("toggle")) {
      var item = dataView.getItem(args.row);
      if (item) {
        if (!item._collapsed) {
          item._collapsed = true;
        } else {
          item._collapsed = false;
        }

        dataView.updateItem(item.id, item);
      }
      e.stopImmediatePropagation();
    }
  });

  // wire up model events to drive the grid
  dataView.onRowCountChanged.subscribe(function (e, args) {
    grid.updateRowCount();
    grid.render();
  });

  dataView.onRowsChanged.subscribe(function (e, args) {
    grid.invalidateRows(args.rows);
    grid.render();
  });

  var h_runfilters = null;

  // wire up the slider to apply the filter to the model
  $("#pcSlider").slider({
    "range": "min",
    "slide": function (event, ui) {
      Slick.GlobalEditorLock.cancelCurrentEdit();

      if (percentCompleteThreshold != ui.value) {
        window.clearTimeout(h_runfilters);
        h_runfilters = window.setTimeout(dataView.refresh, 10);
        percentCompleteThreshold = ui.value;
      }
    }
  });

  // wire up the search textbox to apply the filter to the model
  $("#txtSearch").keyup(function (e) {
    Slick.GlobalEditorLock.cancelCurrentEdit();

    // clear on Esc
    if (e.which == 27) {
      this.value = "";
    }

    searchString = this.value;
    dataView.refresh();
  })
})
</script>
</body>
</html>
时间: 2024-10-06 12:18:55

SlickGrid example 5:带子项的展开收缩的相关文章

Cell展开&amp;&amp;收缩全垒打

Cell展开&&收缩全垒打 引言 最近想把UITableView(表视图)全面熟悉一遍,接触到两个实例方法 - (void)beginUpdates; - (void)endUpdates; 经过一番研究后发现这两个方法除了用来批量操作Cell,还有改变动态更新行高的作用.官方文档给出了这样的说明: You can also use this method followed by the endUpdates method to animate the change in the row

jquery实现后台系统左侧菜单的点击展开/收缩二级菜单效果

html: <div class="col-sm-3 col-md-2 sidebar"> <div class="totalt"><a>系统管理系统</a></div> <ul class="menu"> <li class="title"> <a class="item item1"><span cla

Android开发之自定义View-可动画展开收缩View的实现

有时候需要点击一个view可以动画展开和收缩折叠一个View这样的效果,这样就可以直接自定义View来实现. 本例中,采用继承FrameLayout来实现自定义的ExpandView.下面将详细介绍各个部分来实现该类以及如何使用该自定义视图. 效果图如下: 未展开效果: 正在向上折叠收缩中的效果: 已经展开效果: 自定义展开类:ExpandView的实现: package com.czm.customview; import android.content.Context; import and

点击UITableView的cell展开收缩

首先要理解UITableView代理方法调用的先后顺序. 当初始化UITableView后,代理回调顺序如下 1://返回cell个数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 2://返回每行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPat

WordPress文章页添加展开/收缩功能

很多时候我们在WordPress上发布一些文章的时候里面都包含了很多的代码,我一般又不喜欢把代码压缩起来而喜欢让代码格式化显示,但是格式化显示通常会让文章内容看起来很多,不便于访问者浏览,所以今天就介绍一种可以展开/收缩文章内容的功能. 方法: 1.在header.php中添加下面的代码,或者也可以单独写进一个js文件中然后在header.php中引入也可以.我是引入的. 1 2 3 4 5 6 7 <script type="text/javascript">     j

Jquery div展开收缩

1 <html> 2 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 3 <head> 4 <script type="text/javascript" src="js/jquery1.3.2.js"></script> 5 <script type="tex

iOS tableview cell 的展开收缩

iOS tableview cell 的展开收缩 #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ NSMutableArray *_allArray;//创建一个数据源数组 NSMutableDictionary *dic;//创建一个字典进行判断收缩还是展开 } @property (nonatomic,strong)UI

展开/收缩效果切换

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>展开/收缩效果切换</title> <style> #box .hid{display:none;} #box .show{display:block;} #tog{cursor: pointer} li{list-style: none} &l

jQuery弹性展开收缩菜单插件gooey.js

分享一款基于jQuery弹性展开收缩菜单插件gooey.js.这是一款基于gooey.js插件实现的弹性菜单特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <header class="plugin-demo-header"> <h1>Gooey Menu</h1> <nav id="gooey-upper"> <input type="checkbox" cla