Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 231 lines (206 sloc) 6.92 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Grid: master-detail - observable integration</title>
<link rel="stylesheet" href="../themes/base/jquery.ui.all.css">
<link rel="stylesheet" href="grid.css">
<link rel="stylesheet" href="editable.css">
<script src="../jquery-1.5.1.js"></script>
<script src="../external/jquery.tmpl.js"></script>
<script src="jquery.observable.js" type="text/javascript"></script>
<script src="../external/jquery.mousewheel-3.0.4.js"></script>
<script src="../ui/jquery.ui.core.js"></script>
<script src="../ui/jquery.ui.widget.js"></script>
<script src="../ui/jquery.ui.mouse.js"></script>
<script src="../ui/jquery.ui.button.js"></script>
<script src="../ui/jquery.ui.selectable.js"></script>
<script src="../ui/jquery.ui.spinner.js"></script>
<script src="datasource2-observable.js"></script>
<script src="datasource-local.js"></script>
<script src="grid2-observable.js"></script>
<script src="pager.js"></script>
<script>
var localDevelopers;
$.ajax({
dataType: "json",
url: "developers.json",
async: false,
success: function( result ) {
localDevelopers = result;
}
});

$(function() {
var developers = $.ui.localDatasource({
input: localDevelopers,
paging: {
limit: 4
}
});

var developersGrid = $.ui.grid({
source: developers.toArray(),
selectedData: [],
refresh: function() {
$( this ).find( "tbody tr" ).addClass( function() {
if ( $.inArray( $.tmplItem( this ).data, selectedData ) >= 0) {
return "ui-selected";
}
});
}
}, "#developers-grid" );

$( "#developers-pager" ).pager({
source: developers
});

//////////////////// Selection support - to be integrated into grid ////////////////////

var selectedData = developersGrid.options.selectedData;

$( "#developers-grid" ).selectable({
filter: "tbody tr",
start: function( ev, eventArgs ) {
if ( !ev.ctrlKey ) {
// If ctrlKey not true, remove any other items in selectedData - which may be on other pages than the current one.
$.observable( selectedData ).remove( 0, selectedData.length );
}
},
selected: function( ev, eventArgs ) {
var dataItem = $.tmplItem( eventArgs.selected ).data;
index = $.inArray( dataItem, selectedData );
if ( index == -1 ) {
$.observable( selectedData ).insert( selectedData.length, dataItem );
}
},
unselected: function( ev, eventArgs ) {
var index = $.inArray( $.tmplItem( eventArgs.unselected ).data, selectedData );
if ( index >= 0 ) {
$.observable( selectedData ).remove( index );
}
},
stop: function( ev, eventArgs ) {
updateDetailView();
}
});

$( ".clearSelection" ).click( function() {
$.observable( selectedData ).remove( 0, selectedData.length );
developersGrid.element.find( "tbody tr" ).removeClass( "ui-selected" ); // Avoid re-rendering the grid, for better perf, and to maintain state.
updateDetailView();
});

$( ".invertSelection" ).click( function() {
// Clone localDevelopers, and remove items in current selectedData.
var inverted = localDevelopers.slice( 0 ),
l = selectedData.length;
while ( l-- ) {
inverted.splice( $.inArray( selectedData[l], inverted ), 1 );
}
// Use resulting array as new contents of selectedData array.
$.observable( selectedData ).refresh( inverted );
developersGrid.element.find("tbody tr").toggleClass( "ui-selected" );
updateDetailView();
});


//////////////////// Editing actions using observable ////////////////////////////

function updateDetailView() {
$( "#developers-detail" ).empty();
$( "#detail-template" ).tmpl( selectedData )
.appendTo( "#developers-detail" );
}

developers.refresh();

$( ".addDeveloper" ).click( function () {
adding = true;
var l = selectedData.length,
insertAt = l
? $.inArray( selectedData[ l - 1 ], localDevelopers )
: -1,
addedData = {
firstName: " ",
lastName: "",
country: ""
};
$.observable( localDevelopers ).insert( insertAt+1, addedData );
$.observable( selectedData ).refresh( addedData );
developers.refresh();
updateDetailView();
adding = false;
});

$( ".deleteSelected" ).click(function() {
var l = selectedData.length;
while ( l-- ) {
deleteItem( selectedData[l], localDevelopers );
$.observable( selectedData ).remove( l );
}
developers.refresh();
updateDetailView();
});

$( "#developers-detail" )
.delegate( "#okBtn", "click", function () {
// Commit entire form content to data model with one API call.
var tmplItem = $.tmplItem( this );
$.observable( tmplItem.data ).setProperty( $( "input", tmplItem.nodes ).serializeArray() );
deleteItem( tmplItem.data, selectedData );
developersGrid.refresh(); // Only needed if data does not change - in order to reset selected item class on tr.
$( tmplItem.nodes ).remove();
})

.delegate( "#deleteBtn", "click", function () {
var item = $.tmplItem( this ).data;
deleteItem( item, localDevelopers );
deleteItem( item, selectedData );
developers.refresh();
updateDetailView();
});

function deleteItem( item, array ) {
var index = $.inArray( item, array );
if ( index > -1 ) {
$.observable( array ).remove( index );
};
}
});
</script>
<style>
.ui-selecting td { background: #eef; }
.ui-selected td { background: #ddf; }
</style>
</head>
<body>

<h1>Grid master-detail - observable integration</h1>

<p id="developers-pager">
<button data-page="first">First</button>
<button data-page="prev">Prev</button>
<button data-page="prevStep">-1</button>
<span>
Page <input class="current" size="4"/>/<span class="total">0</span>,
Total records <span class="totalRecords">0</span>
</span>
<button data-page="nextStep">+1</button>
<button data-page="next">Next</button>
<button data-page="last">Last</button>
</p>

<p>
<button class="clearSelection">Clear selection</button>
<button class="invertSelection">Invert selection</button>
</p>

<p>
<button class="addDeveloper" >Insert developer</button>
<button class="deleteSelected">Delete selected developers</button>
</p>

<table id="developers-grid">
<thead>
<tr>
<th data-field="firstName">First Name</th>
<th data-field="lastName">Last Name</th>
<th data-field="country">Country</th>
<th data-field="bitcoins" data-type="number">Bitcoins</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

<div id="developers-detail"></div>

<script id="detail-template" type="text/x-jquery-tmpl">
<div>
<div class="title">{{if adding}}Add{{else}}Edit{{/if}} Person</div>
<div><input name="firstName" value="${firstName}"/></div>
<div><input name="lastName" value="${lastName}"/></div>
<div><input name="country" value="${country}"/></div>
<span id="okBtn" >OK</span>
<span id="deleteBtn" >Delete</span>
</div>
</script>

</body>
</html>
Something went wrong with that request. Please try again.