Skip to content

Instantly share code, notes, and snippets.

@ahgood
Created August 16, 2017 02:23
Show Gist options
  • Save ahgood/e9651ab3469ad724f2e44539b67250ef to your computer and use it in GitHub Desktop.
Save ahgood/e9651ab3469ad724f2e44539b67250ef to your computer and use it in GitHub Desktop.
Sorting by Chinese Pinyin in Datatable
/*
pinyin_dict_notone.js and pinyinUtil.js is available in URL below:
https://github.com/sxei/pinyinjs
*/
<script src="pinyin_dict_notone.js"></script>
<script src="pinyinUtil.js"></script>
<script>
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"chinese-string-asc": function(s1, s2) {
s1 = pinyinUtil.getPinyin(s1);
s2 = pinyinUtil.getPinyin(s2);
return s1.localeCompare(s2);
},
"chinese-string-desc": function(s1, s2) {
s1 = pinyinUtil.getPinyin(s1);
s2 = pinyinUtil.getPinyin(s2);
return s2.localeCompare(s1);
}
});
jQuery(document).ready(function() {
jQuery('#mydatatable').dataTable({
"columnDefs": [
{ type: 'chinese-string', targets: 0 }
]
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment