Using plugins in 1.10
Using plugins in 1.10
dylanmac
Posts: 49Questions: 7Answers: 1
I have a table with Japanese characters and wrote a plug-in to handle ordering. It wasn't working and I could not figure out why.
Plugin:
[code]
(function(){
//Is this character a latin or non-Asian character code
function isLatinish(a) {
//Less than code point 0x2E2F
return a <= 11823;
}
//Is this character katakana, hirigana, bopomofo and other phonetic Asian characters
function isKatakanaish(a) {
//Between code points 0x3000 and 0x31EF or between code points 0xFF00 and 0xFFEF
return a >= 12288 && a <= 12783 || a >= 65280 && a <= 65519;
}
//Is this a character or Kanji
function isKanjiish(a) {
//Between code points 0x3400 and 0xFFOO
return a >= 13312 && a < 65280;
}
//The sort ranking for a character
//This is actual magic: Kanji first, katakana second, latin-ish third, other stuff last
function scoreCharForJPSort(a) {
if ( isKanjiish(a) ) {
return 1;
} else if ( isKatakanaish(a) ) {
return 2;
} else if ( isLatinish(a) ) {
return 3;
} else {
return 4;
}
}
//Long winded sort function
function sortJPText(a,b) {
//Check for missing values
if(!a) {
return -1;
}
if (!b) {
return 1;
}
//Get the minimum length to traverse
var alen = a.length;
var blen = b.length;
var mlength = Math.min(alen,blen);
//Compare character by character
for ( var i = 0; i < mlength; i++ ) {
var achar = a.charCodeAt(i);
var bchar = b.charCodeAt(i);
var abucket = scoreCharForJPSort(achar);
var bbucket = scoreCharForJPSort(bchar);
//First sort according to the buckets
if ( abucket != bbucket ) {
return ( abucket - bbucket );
}
//Then sort with in the bucket by Unicode ordering
if ( achar != bchar ) {
return ( achar - bchar );
}
}
return 0;
}
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"jp-string-asc": function ( a, b ) {
console.log('asc');
return sortJPText(a,b);
},
"jp-string-desc": function ( a, b ) {
console.log('desc');
return sortJPText(a,b) * -1;
}
});
})();
[/code]
Function:
[code]
BLK.productList.initDataTable = function () {
var oDefault = {
"columnDefs": [
{
"type": "jp-string",
"targets": ['colJpFundSeriesName']
}
],
"data": dtProdList, //using JS var to load data to improve performance
"deferRender": true,
"ordering": true,
"order": [
[ sortIndex, sortDirection ]
],
"pagingType": "simple_numbers"
};
$productListDataTable.dataTable(oDefault);
};
[/code]
At first I assumed that the plugin code was faulty. But then after adding the console.logs (see above), I discovered that the plugin function wasn't being called at all.
For sake of comparison, I tried switching to the much simpler chinese-string plugin, added console.logs there, too, and still got no response.
Have I configured this wrong?
Dylan
Plugin:
[code]
(function(){
//Is this character a latin or non-Asian character code
function isLatinish(a) {
//Less than code point 0x2E2F
return a <= 11823;
}
//Is this character katakana, hirigana, bopomofo and other phonetic Asian characters
function isKatakanaish(a) {
//Between code points 0x3000 and 0x31EF or between code points 0xFF00 and 0xFFEF
return a >= 12288 && a <= 12783 || a >= 65280 && a <= 65519;
}
//Is this a character or Kanji
function isKanjiish(a) {
//Between code points 0x3400 and 0xFFOO
return a >= 13312 && a < 65280;
}
//The sort ranking for a character
//This is actual magic: Kanji first, katakana second, latin-ish third, other stuff last
function scoreCharForJPSort(a) {
if ( isKanjiish(a) ) {
return 1;
} else if ( isKatakanaish(a) ) {
return 2;
} else if ( isLatinish(a) ) {
return 3;
} else {
return 4;
}
}
//Long winded sort function
function sortJPText(a,b) {
//Check for missing values
if(!a) {
return -1;
}
if (!b) {
return 1;
}
//Get the minimum length to traverse
var alen = a.length;
var blen = b.length;
var mlength = Math.min(alen,blen);
//Compare character by character
for ( var i = 0; i < mlength; i++ ) {
var achar = a.charCodeAt(i);
var bchar = b.charCodeAt(i);
var abucket = scoreCharForJPSort(achar);
var bbucket = scoreCharForJPSort(bchar);
//First sort according to the buckets
if ( abucket != bbucket ) {
return ( abucket - bbucket );
}
//Then sort with in the bucket by Unicode ordering
if ( achar != bchar ) {
return ( achar - bchar );
}
}
return 0;
}
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"jp-string-asc": function ( a, b ) {
console.log('asc');
return sortJPText(a,b);
},
"jp-string-desc": function ( a, b ) {
console.log('desc');
return sortJPText(a,b) * -1;
}
});
})();
[/code]
Function:
[code]
BLK.productList.initDataTable = function () {
var oDefault = {
"columnDefs": [
{
"type": "jp-string",
"targets": ['colJpFundSeriesName']
}
],
"data": dtProdList, //using JS var to load data to improve performance
"deferRender": true,
"ordering": true,
"order": [
[ sortIndex, sortDirection ]
],
"pagingType": "simple_numbers"
};
$productListDataTable.dataTable(oDefault);
};
[/code]
At first I assumed that the plugin code was faulty. But then after adding the console.logs (see above), I discovered that the plugin function wasn't being called at all.
For sake of comparison, I tried switching to the much simpler chinese-string plugin, added console.logs there, too, and still got no response.
Have I configured this wrong?
Dylan
This discussion has been closed.
Replies
Thanks for bringing this up. I don't immediately see anything wrong with your code, so yes, it could very well be an error in 1.10. Could you link me to a test page showing the error so I can debug it please?
Thanks,
Allan
Allan
I just tried the nightly version and still no luck.
Unfortunately I am working in a development environment and cannot share a link. However I have created a single page version which I could share with you. just let me know how you'd like it.
https://dl.dropboxusercontent.com/u/505272/index.html
Allan
The fix is committed here: https://github.com/DataTables/DataTablesSrc/commit/4615e4e248 . And I'll sync it to the build and nightly shortly.
Allan