GithubHelp home page GithubHelp logo

ko_tree's Introduction

Description

Create a tree view using Knockout. Tree is draggable/draggable and nodes can be deleted. Try it out here: kotree.herokuapp.com

Libraries Required

  • jquery

  • jquery-ui

  • knockout

  • knockout.mapping

JSON Format

[{id: 1, name: item1, projects: [{id: 2, name: item2, projects: []}]}, {id: 3, name: item3, projects: []}]

In Rails, you can check out my example on editing the json format in the projects model.

def as_json(options = {})
{
  id: id,
  name: name,
  projects: self.projects.collect {|n| n.as_json}
}
end

Knockout binding

ko.bindingHandlers.droppable = {
  init: function(element, valueAccessor, allBindingsAccessor, data, context) {
    $(element).droppable({
      drop: function(event, ui) {
        var dropped = $(this).data('id');
        var dragged = $(ui.draggable).data('id');

        $(ui.draggable).remove();
        $.ajax({
          url: 'projects/' + dragged + '.json',
          type: 'PUT',
          data: {'project': {'project_id': dropped}}
        }).done(function() {
          self.loadProjects();
        });
      }
    });

    $(element).draggable({
      revert: true
    });
  }
};

View

<ul class="nav nav-list" data-bind="template: {name: 'projects_template', foreach: projects}">
</ul>
<script type='text/html' id='projects_template'>
  <li data-bind="attr: {'data-id': $data.id, class: 'project-nav-li'}, droppable: $root">
    <span href='#' data-bind='html: "<a href=\"#\">" + name() + "</a>"'></span><span class='pull-right project-delete' data-bind='click: $root.deleteProject'><i class='icon-trash'></i></span>
  </li>
  <ul class="nav nav-list" data-bind="template: {name: 'projects_template', foreach: projects}">
  </ul>
</script>

ko_tree's People

Contributors

davliu avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.