- 论坛徽章:
- 0
|
datatable用法(3)
jquerydatatablestruts2json.
再添加一个功能,对各个列进行过滤。
添加js代码:- 1.function fnCreateSelect( aData )
- 2.{
- 3. var r='<select><option value=""></option>', i, iLen=aData.length;
- 4. for ( i=0 ; i<iLen ; i++ )
- 5. {
- 6. r += '<option value="'+aData[i]+'">'+aData[i]+'</option>';
- 7. }
- 8. return r+'</select>';
- 9.}
- 10.
- 11.
- 12.(function($) {
- 13. /*
- 14. * Function: fnGetColumnData
- 15. * Purpose: Return an array of table values from a particular column.
- 16. * Returns: array string: 1d data array
- 17. * Inputs: object:oSettings - dataTable settings object. This is always the last argument past to the function
- 18. * int:iColumn - the id of the column to extract the data from
- 19. * bool:bUnique - optional - if set to false duplicated values are not filtered out
- 20. * bool:bFiltered - optional - if set to false all the table data is used (not only the filtered)
- 21. * bool:bIgnoreEmpty - optional - if set to false empty values are not filtered from the result array
- 22. * Author: Benedikt Forchhammer <b.forchhammer /AT\ mind2.de>
- 23. */
- 24. $.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
- 25. // check that we have a column id
- 26. if ( typeof iColumn == "undefined" ) return new Array();
- 27.
- 28. // by default we only wany unique data
- 29. if ( typeof bUnique == "undefined" ) bUnique = true;
- 30.
- 31. // by default we do want to only look at filtered data
- 32. if ( typeof bFiltered == "undefined" ) bFiltered = true;
- 33.
- 34. // by default we do not wany to include empty values
- 35. if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
- 36.
- 37. // list of rows which we're going to loop through
- 38. var aiRows;
- 39.
- 40. // use only filtered rows
- 41. if (bFiltered == true) aiRows = oSettings.aiDisplay;
- 42. // use all rows
- 43. else aiRows = oSettings.aiDisplayMaster; // all row numbers
- 44.
- 45. // set up data array
- 46. var asResultData = new Array();
- 47.
- 48. for (var i=0,c=aiRows.length; i<c; i++) {
- 49. iRow = aiRows[i];
- 50. var aData = this.fnGetData(iRow);
- 51. var sValue = aData[iColumn];
- 52.
- 53. // ignore empty values?
- 54. if (bIgnoreEmpty == true && sValue.length == 0) continue;
- 55.
- 56. // ignore unique values?
- 57. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
- 58.
- 59. // else push the value onto the result data array
- 60. else asResultData.push(sValue);
- 61. }
- 62.
- 63. return asResultData;
- 64. }}(jQuery));
复制代码 在前面博客中的原来的 sortData()方法中添加js:- 1.$("tfoot th").each( function ( i ) {
- 2. this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
- 3. $('select', this).change( function () {
- 4. oTable.fnFilter( $(this).val(), i );
- 5. } );
- 6. } );
-
复制代码 在页面table中添加一个:- 1.<tfoot>
- 2. <tr>
- 3. <th width ='10%' align='center' class='w_01' ></th>
- 4. <th width ='10%' align='center' class='w_01' ></th>
- 5. <th width ='10%' align='center' class='w_01' ></th>
- 6. <th width ='10%' align='center' class='w_01' ></th>
- 7. <th width ='10%' align='center' class='w_01' ></th>
- 8. <th width ='10%' align='center' class='w_01' ></th>
- 9. <th width ='10%' align='center' class='w_01' ></th>
- 10. <th width ='10%' align='center' class='w_01' ></th>
- 11. </tr>
- 12. </tfoot>
复制代码 最后别忘了,引入datatable的js:
jquery.dataTables.js,
jquery.dataTables.css//是他自带css,要是想改式样的,就直接修改里面的css。省省不少使呢。。。。
还有,最好的学习方法是直接看官方文档,不过英文的,很难消化。
以上,完毕。
|
|