Created
July 7, 2014 13:48
-
-
Save dustingetz/ee926a14a36137a34b2c to your computer and use it in GitHub Desktop.
Kendo Grid Picker using native Kendo selection state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
'underscore.mixed', 'react', 'jquery', 'wingspan-forms' | |
], function (_, React, $, Forms) { | |
'use strict'; | |
var PatientPicker = React.createClass({ | |
displayName: 'PatientPicker', | |
getDefaultProps: function () { | |
return { | |
dataSource: undefined, | |
value: undefined, // selected ids | |
onChange: undefined | |
}; | |
}, | |
render: function () { | |
return Forms.KendoGrid({ | |
selectable: 'multiple', | |
scrollable: true, | |
dataSource: this.props.dataSource, | |
columns: [{ field: 'clientUserName' }], | |
//rowTemplate: '<tr data-uid="#: uid #" role="row"><td role="gridcell">#: label #</td></tr>', | |
// //rowTemplate: '<tr id="#: id #"><span>#: label #</span></tr>', | |
change: this.onKendoChange | |
}); | |
}, | |
componentDidMount: function () { | |
this.syncSelectionWithKendo(); | |
}, | |
componentWillUnmount: function () {}, | |
componentDidUpdate: function () { | |
this.syncSelectionWithKendo(); | |
}, | |
syncSelectionWithKendo: function () { | |
var kendoWidget = $(this.getDOMNode()).data('kendoGrid'); | |
// Figure out which records are in the grid dom right now | |
// We can select all the TRs since the grid rowTemplate is guaranteed to use tr/td | |
var trs = $(kendoWidget.element).find('.k-grid-content tr'); | |
_.each(trs, function (tr) { | |
var dataItem = kendoWidget.dataItem(tr); | |
$(tr).toggleClass('k-state-selected', _.contains(this.props.value, dataItem['clientUserId'])); | |
}.bind(this)); | |
}, | |
onKendoChange: function () { | |
var kendoWidget = $(this.getDOMNode()).data('kendoGrid'); | |
var selectedDomRows = kendoWidget.select(); | |
var nextSelection = _.map(selectedDomRows, function (tr) { | |
return kendoWidget.dataItem(tr).toJSON()['clientUserId']; | |
}); | |
// TODO: un-select it now, wait for next reconciliaton to allow selection | |
this.props.onChange(nextSelection); | |
} | |
}); | |
return PatientPicker; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment