[StackOverflow] Backbone.js and jqGrid

A simple example using Backbone.JS and jqGrid. This example add each model present on the collection to the jqGrid.

by paulyoder

HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script src="http://www.trirand.com/blog/jqgrid/js/jquery.layout.js"></script>
<script src="http://www.trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js"></script>
<script src="http://www.trirand.com/blog/jqgrid/js/ui.multiselect.js"></script>
<script src="http://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.min.js"></script>
<script src="http://www.trirand.com/blog/jqgrid/js/jquery.tablednd.js"></script>
<script src="http://www.trirand.com/blog/jqgrid/js/jquery.contextmenu.js"></script>
<link rel="stylesheet" href="http://www.trirand.com/blog/jqgrid/themes/redmond/jquery-ui-1.8.1.custom.css">
<link rel="stylesheet" href="http://www.trirand.com/blog/jqgrid/themes/ui.jqgrid.css">
<link rel="stylesheet" href="http://www.trirand.com/blog/jqgrid/themes/ui.multiselect.css">
<table id="dogsList"></table>

<pre id="code"></pre>

JavaScript

// Init the collection
dogs = new Backbone.Collection([
    {name:"Jane",breed:"Great Dane"},
    {name:"Rocky",breed:"golden Retriver"},
    {name:"Jim",breed:"Lab"}
]);


$('#code').html('hello');

// Create the table
var dogsTable = jQuery("#dogsList").jqGrid({ 
    datatype: 'json',
    data: {'name':'Paul','breed':'human'},
    width:'100%',
    colNames:['name', 'breed'], 
    colModel:[  {name:'name', align:'left'},
                {name:'breed', align:'left'}],
    loadComplete : function(data) {
        //alert('grid loading completed ' + data);
    },
    loadError : function(xhr, status, error) {
        alert('grid loading error' + error);
    }
});

/*
// For each dog, add a row in the table
dogs.each(function (dog, i) {
    dogsTable.jqGrid('addRowData', (i + 1), dog.toJSON());
});
*/