Welcome Guest, Not a member yet? Register   Sign In
Ajax if no result return empty
#4

(06-28-2017, 11:20 AM)PaulD Wrote: Oh damn it. I just wrote a long answer and accidentally closed the browser :-(

Anyway, the long and short of it was that I think your each loop might fail depending on how your model returns attachments to your controller.

I would not have any html in my js, I think it is ugly and hard to maintain. Instead, in your controller get your attachments, then load a partial view into a variable called something like 'attachments_tbody' and return that. Then in your js code just output the tbody contents to the page.

In this way you can easily deal within the view of the case of there being no attachments. ie a message like '<tr><td>no attachments to display</td></td>'

In that way your js is clean of html, you can have much easier control over the behaviour and layout from within a view file, and it is much simpler to deal with the case of no attachments.

Alternatively, test to see if there are any attachments returned in your js and output relevant html message from there. But personally I feel that putting html into js becomes messy and unmanageable over time. I try to keep all my html in views or partial views, and not in the controllers or js.

Hope that helps,

Paul.

PS Hopefully I won't delete this before I manage to click the save button this time.

Hi, Just to let you know after working on it for half hour today I have it solved.

I create a get function on view for ajax that just gets the files / attachments

PHP Code:
function getattachments() {
    $.
ajax({
        
url"<?php echo base_url('extensions/attachments/getattachments');?>",
        
type'post',        
        
dataType'json',
        
data: {
            
posthash: $('#posthash').val()
        },
        
success: function(json) {

            
info '';
            
            $.
each(json['attachments'], function( keyvalue ) {
                
info += '<tr>';
                
info += '<td>';
                
info += value['orig_name'];
                
info += '</td>';
                
info += '<td>';
                
info += value['file_size'] + 'KB';
                
info += '</td>';
                
info += '<td class="text-center">';
                
info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                
info += '</td>';
                
info += '</tr>';
            });    

            $(
'#file-attachments tbody').html(info);
            
        },
    });


Then I return the function in success part of this ajax

PHP Code:
$(document).on('click''#delete_button', function (e) {
    
e.preventDefault();

    $.
ajax({
        
url"<?php echo base_url('extensions/attachments/delete');?>",
        
type'post',        
        
dataType'json',
        
data: {
        
attachment_id: $(this).attr("data-id"),
        
posthash: $('#posthash').val()
        },
        
success: function(json) {
                return 
getattachments();
        },
    });
}); 

Of course I have had to make a getattachments function on controller that just gets the attachments for this post.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Messages In This Thread
Ajax if no result return empty - by wolfgang1983 - 06-28-2017, 03:26 AM
RE: Ajax if no result return empty - by PaulD - 06-28-2017, 11:20 AM
RE: Ajax if no result return empty - by wolfgang1983 - 06-29-2017, 03:02 AM
RE: Ajax if no result return empty - by Wouter60 - 06-28-2017, 11:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB