DataView Web Part - Datatables JQuery. Removing filtering/sorting from SharePoint

Adam Ehrenworth
Adam Ehrenworth used Ask the Experts™
on
We moved to SharePoint Online and I am trying to use some code I put together to pull in a Dataview web part and then have the table search able using Datatables.js

I was having trouble figuring out how to turn off the filtering and the sorting that is native to the SharePoint webpart so I opted to hide that row and replace it was a new row that is generated by script.

However, it appears that when Datatables is staging the table to display it is ignoring this new row that was added programatically.

I had to find the table based on a custom attribute and then force an ID attribute and that is all working. I just can't get the new row to become part of the DataTable display.

This is the code.  Any suggestions?

$(document).ready(function() 
{

$('table[summary="2CIA4_Rules "]').attr('id','ruletable');
$('#ruletable > tbody').prepend('<tr><td>Rule</td><td>Owner</td><td>Audience</td><td>Curriculum</td><td>Target Area</td><td>Leader</td><td>Leader_WWID</td><td>MRC</td><td>Legal</td><td>JobFunction</td><td>SubFunction</td><td>Exclusions</td><td>Year 4 Notes</td><td>Comments</td><td>ChangeNotes</td></tr>');

$( ".ms-listviewtable" ).DataTable( {
		dom: 'Tf<"clear">rtip',
        "scrollY":        "600px",
		"scrollCollapse": true,
		"paging":         false
} );


var table = $( ".ms-listviewtable" ).DataTable();

//table.column(13).visible( false );
//table.column(14).visible( false );
 
} );

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:
My first guess is that this is a timing thing.  JavaScript does not wait for the prepend to complete before moving on to initializing the datatable.

One way to test is to put an alert('whatever') just after the prepend line.  That will force JS to wait until you click OK before initializing the datatable.

If the new row now appears in the datatable, the permanent solution would be to use a promise on the line that prepends the row.

I'm not sure if this is going down the right track so I'll stop here until you've had a chance to test the theory using an alert.
Adam EhrenworthLead Technology Analyst

Author

Commented:
When the alert triggers I see the column headers, when I click OK datatables loads and the same behavior occurs and the create row disappears. I have some other CSS affecting the display of the original table header from SharePoint but that class should have nothing to do with the newly create row.

Any ideas? Do you need to see anything else from my code?

Regards,

Adam
Top Expert 2010

Commented:
OK, that doesn't sounds like a timing issue then.  I'll play around will what you've posted so far and see if I can come up with any other ideas.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2010

Commented:
New theory.

You are attempting to init the datatable twice, which throws an error.  datatables only allows one init, unless the first table is destroyed before the second one is created.  In the code you posted above, line 7 and 15 both init the datatable.  You can consolidate the two lines by keeping line 7 but add var table = before it.

When I remove the second init on line 15, the added row shows up fine in the table.
Here is a Fiddle Demo.  Click the button to make the magic happen.
Adam EhrenworthLead Technology Analyst

Author

Commented:
zephyr,

Thank you for looking into this further. However, my scenario is a little different as the row that I am appending is actually the header row. I am hiding the SharePoint header row because it has the filtering and sorting function that is competing with DataTables.

I am not sure if this is what is causing my problem because I removed the 2nd init and I am still not getting the new header to persist.


 $(document).ready(function() 
{

$('table[summary="2CIA4_Rules "]').attr('id','ruletable');
$('#ruletable > tbody').prepend('<tr class="ruleheader"><td>Rule</td><td>Owner</td><td>Audience</td><td>Curriculum</td><td>Target Area</td><td>Leader</td><td>Leader_WWID</td><td>MRC</td><td>Legal</td><td>JobFunction</td><td>SubFunction</td><td>Exclusions</td><td>Year 4 Notes</td><td>Comments</td><td>ChangeNotes</td></tr>');

var table = $( ".ms-listviewtable" ).DataTable( {
		dom: 'Tf<"clear">rtip',
        "scrollY":        "600px",
		"scrollCollapse": true,
		"paging":         false
} ); 
} );

Open in new window


I am doing this to hide the SharePoint specific header row.  Should I use JavaScript to kill the row instead of hiding it?

.ms-viewheadertr {

display:none;	
	
}

Open in new window

Adam EhrenworthLead Technology Analyst

Author

Commented:
Actually I just double checked and it prepended the header but it actually is now at the bottom!

Any idea why it is prepending there?
Top Expert 2010
Commented:
DataTables requires proper HTML syntax for the header row:

<table>
   <thead>
      <tr><th>Header</th></tr>
   </thead>
   <tbody>
      <tr><td>Table Row</td></tr>
   </tbody>
</table>

Open in new window


If you want a header row, you should be adding the row to thead, and use <th> tags instead of <td>

Here's an updated Fiddle Demo.
Adam EhrenworthLead Technology Analyst

Author

Commented:
thanks I got this to work by adding the required thead tag back into the prepend.

THANKS!
Top Expert 2010

Commented:
wahoo!  glad you got it working.

you can close the question by selecting the solution that answered you question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial