Solved

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

Posted on 2016-10-10
9
31 Views
Last Modified: 2016-10-15
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

0
Comment
Question by:aehrenwo
  • 5
  • 4
9 Comments
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
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.
0
 

Author Comment

by:aehrenwo
Comment Utility
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
0
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
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.
0
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:aehrenwo
Comment Utility
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

0
 

Author Comment

by:aehrenwo
Comment Utility
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?
0
 
LVL 42

Accepted Solution

by:
zephyr_hex earned 500 total points
Comment Utility
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.
0
 

Author Comment

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

THANKS!
0
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
wahoo!  glad you got it working.

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now